#!/bin/bash

# bl-urxlx

# Naive Xresources to Lxterminal color config converter  

# Copyright (C) 2019 brontosaurusrex
# License: GPLv3 or later.

# help
help () { 
cat << EOF
    bl-urxlx is naive Xresources to Lxterminal color config converter
    
Options:
    -h --help   show this message

Examples:
    .Xresources > tmp.txt
    monokai.txt | xclip
    
    # then paste the results to strategic location in 
    # ~/.config/lxterminal/lxterminal.conf 
    # and win.
EOF
}

if [ "$1" == "--help" ] || [ "$1" == "-h" ] || [ $# -eq 0 ]; then
    help && exit
fi

# ffffff to rgb(255,255,255)
2rgb() {
    hex="$1"
    printf "rgb(%d,%d,%d)\n" 0x"${hex:0:2}" 0x"${hex:2:2}" 0x"${hex:4:2}"

}

# main
for file in "$@" ; do
    echo # empty line
    grep -E --color=never 'color[0-9]|background:|foreground:' "$file" | while read -r color hex; do

        if [[ $color =~ [!0-9] ]]; then 
            #echo "$color has number"
            color="palette_color_${color//[!0-9]}"
        else
            [[ $color == *"background"* ]] && color="bgcolor"
            [[ $color == *"foreground"* ]] && color="fgcolor"
        fi

        hex="${hex//#}" # remove #
        
        echo "$color=$(2rgb "$hex")"

    done
done