#!/bin/bash
#mxrr MX-comfort-rofi-recolor
#Tool to edit colors in MX-comfort rofi theme
#V.0.5b by Melber and MX Devs, 21 August 2022
#License: GPL-3.0+

#define some variables

TITLE="MX-rofi-recolor"
CLASS="mxrr"
ROFIPATH="$HOME/.config/rofi/themes"
ICONPATH="/usr/share/pixmaps/mxrr-icon.png"
#HELPPATH="/usr/share/pixmaps/mxrr-help.png"


#delete stylelist and tempclr after use
trap "rm -r "$ROFIPATH/tempdir" " EXIT

#
if [ ! -d $ROFIPATH ]; then

mkdir $ROFIPATH

fi

#create tempdir

if [ ! -d $ROFIPATH/tempdir ]; then

  mkdir -p $ROFIPATH/tempdir

fi

# notify user and back up config.rasi if mxrr has not already been launched

if [ ! -f $ROFIPATH/MX-comfort-rr ]; then

NOW=$(date +%Y"%m%d")

MESSAGE=$"This tool edits the rofi config file.\n\nThe existing file will be backed up as \n\n<b>$HOME/.restore/rofi/config.rasi_$NOW</b>"

##begin message box
yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=350 --height=200 --text-align=center --text="\n\n$MESSAGE\n\n" --text-align=center --center --borders=20 --button="Cancel":7 --button=$"OK to proceed?":6


if [ ! "$?" = "6" ]; then
    exit 0

else

mkdir -p $HOME/.restore/rofi/
cp $HOME/.config/rofi/config.rasi $HOME/.restore/rofi/config.rasi_$(date -d "today" +"%Y%m%d")
cp /usr/share/rofi/config.rasi $HOME/.config/rofi/
cp /usr/share/rofi/rofi-help $HOME/.config/rofi/
cp /usr/share/rofi/calc-help $HOME/.config/rofi/
cp /usr/share/mxrr/template.rasi $HOME/.config/rofi/themes/
cp /usr/share/mxrr/MX-comfort.rasi $HOME/.config/rofi/themes/

fi
fi


# Check if MX-comfort-rr exists, create if false

if [ ! -f $ROFIPATH/MX-comfort-rr ]; then


  printf "#1D2324 #CACDD0 #29353B #ffffff #29353B #CACDD0 #1D2324 #ffffff #0f56d9 #0f56d9 #ffffff MX-comfort yes" > $ROFIPATH/MX-comfort-rr

fi


#read available styles

cd $ROFIPATH
ls -f *-rr > $ROFIPATH/tempdir/themelist.txt
sed -i 's|-rr||g' $ROFIPATH/tempdir/themelist.txt


#select starting style

BASESTYLE=$( yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --text=$"\n<b> Choose which theme to use as a starting point</b>\n" --width=350 --height=300 --center --list --no-headers --separator="" --column=Style --text-align=center < $ROFIPATH/tempdir/themelist.txt )

GETOUT=$(echo "$?")

if [ "$GETOUT" != 0 ]; then

  exit

fi


if [ -z "$BASESTYLE" ]; then

  yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --text=$"No theme selected.\n\n<b>MX-comfort</b> will be used as the starting theme." --button=OK --text-align=center --center --width=400 --height=200 --borders=20 \

  BASESTYLE=MX-comfort

fi


#read colors

cd $ROFIPATH

read -r -a NEWCOLORS < "$BASESTYLE"-rr

COL01="${NEWCOLORS[0]}"
COL02="${NEWCOLORS[1]}"
COL03="${NEWCOLORS[2]}"
COL04="${NEWCOLORS[3]}"
COL05="${NEWCOLORS[4]}"
COL06="${NEWCOLORS[5]}"
COL07="${NEWCOLORS[6]}"
COL08="${NEWCOLORS[7]}"
COL09="${NEWCOLORS[8]}"
COL10="${NEWCOLORS[9]}"
COL11="${NEWCOLORS[10]}"
NEWNAME="${NEWCOLORS[11]}"
MAKECUR="${NEWCOLORS[12]}"


# function to be called with yad button

color_picker() {
    local N=$1 C="$2"
    local YAD=(
            yad
            --title=$"mxrr-color-selector"
            --class=color-select
            --window-icon=color-select
            --posx=200
            --posy=200
            --color
            --init-color="${C:=white}"
            )
    C=$("${YAD[@]}")
    (($?==0)) && echo "$N:$C"
}


# function to be called with help button

mxrrhelp() {
yad --title=$"MX-Rofi-recolor help"  --class=mxrrhelp --window-icon="$ICONPATH" --borders=20 --fixed --center --width=600 --height=400 --image /usr/share/pixmaps/mxrr-help.png --button=$"Return to Main Window":1 \

}

export -f color_picker mxrrhelp

#MAIN
#color selection dialogue

MAIN=(yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --borders=20 --center --width=600 --height=400 --fixed --form --columns=5 --align=right --separator=" " --button="gtk-help":"bash -c mxrrhelp" --button="gtk-quit":1 --button="gtk-ok":0 \
--text=$"<b>\nEdit colors and create a new rofi theme.\n\n</b>" \
--field=$"Title Background":CLR "$COL01" \
--field=$"Title Text":CLR "$COL02" \
--field=$"Searchbar Background":CLR "$COL03" \
--field=$"Searchbar Text":CLR "$COL04" \
--field=$"Inactive Tab Background":CLR "$COL05" \
--field=$"Inactive Tab Text":CLR "$COL06" \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field='!color-select':BTN '@bash -c "color_picker 1 %1;"'
--field='!color-select':BTN '@bash -c "color_picker 2 %2;"'
--field='!color-select':BTN '@bash -c "color_picker 3 %3;"'
--field='!color-select':BTN '@bash -c "color_picker 4 %4;"'
--field='!color-select':BTN '@bash -c "color_picker 5 %5;"'
--field='!color-select':BTN '@bash -c "color_picker 6 %6;"'
--field=" ":LBL " " \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field=$"Active Tab Background":CLR "$COL07" \
--field=$"Active Tab Text":CLR "$COL08" \
--field=$"Active Tab Highlight":CLR "$COL09" \
--field=$"Selection Background":CLR "$COL10" \
--field=$"Selection Text":CLR "$COL11" \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field='!color-select':BTN '@bash -c "color_picker 19 %19;"'
--field='!color-select':BTN '@bash -c "color_picker 20 %20;"'
--field='!color-select':BTN '@bash -c "color_picker 21 %21;"'
--field='!color-select':BTN '@bash -c "color_picker 22 %22;"'
--field='!color-select':BTN '@bash -c "color_picker 23 %23;"'
--field=" ":LBL " " \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field="<b>  Name for new theme        </b>":LBL " " \
--field=" " ""$NEWNAME"" \
--field="<b>  Make new theme current?</b>":LBL " " \
--field=" ":CB "yes!no" \
--field=" ":LBL " " \
--field=" ":LBL " " \
)

COLOURS=$("${MAIN[@]}")
    (($?==0)) && echo "$COLOURS" > $ROFIPATH/tempdir/tempmain.txt

GETOUT=$(echo "$?")

if [ "$GETOUT" != 0 ]; then

  exit

elif [ "$GETOUT" = 0 ]; then

  cd $ROFIPATH/tempdir

  read -r -a NEWCOLORS < tempmain.txt

  COL01="${NEWCOLORS[0]}"
  COL02="${NEWCOLORS[1]}"
  COL03="${NEWCOLORS[2]}"
  COL04="${NEWCOLORS[3]}"
  COL05="${NEWCOLORS[4]}"
  COL06="${NEWCOLORS[5]}"
  COL07="${NEWCOLORS[6]}"
  COL08="${NEWCOLORS[7]}"
  COL09="${NEWCOLORS[8]}"
  COL10="${NEWCOLORS[9]}"
  COL11="${NEWCOLORS[10]}"
  NEWNAME="${NEWCOLORS[11]}"
  MAKECUR="${NEWCOLORS[12]}"

fi


#check theme names yes no

if [ "$NEWNAME" = 'yes' -o "$NEWNAME" = 'no' ]; then

  MAKECUR="$NEWNAME"  

  NEWNAME=$(yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=400 --height=200 --fixed --text=$"No Theme name!" --form --center --borders=20 --separator="" \
  --field=$"<b>Name for new theme</b>":LBL " " \
  --field=$" " "New theme name" )
    
    GETOUT=$(echo "$?")

    if [ "$GETOUT" != 0 ]; then

    exit

    fi

fi


#check new theme name is not empty and forbid MX-comfort overwrite

while [ -z "$NEWNAME" -o "$NEWNAME" = 'MX-comfort' -o "$NEWNAME" = 'MX-comfort-dark' -o "$NEWNAME" = 'MX-comfort-plus' -o "$NEWNAME" = 'MX-comfort-plus-dark' ]; do

  NEWNAME=$(yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=400 --height=200 --fixed --text=$"Theme name "$NEWNAME" is protected. Choose another name." --form --center --borders=20 --separator="" \
  --field=$"<b>Name for new style</b>":LBL " " \
  --field=$" " "$NEWNAME-new" )

    GETOUT=$(echo "$?")

    if [ "$GETOUT" != 0 ]; then

    exit

    fi

done


#check if new theme name exists, if yes ask if overwrite or different style name

cd $ROFIPATH

while [ -f $NEWNAME.rasi ]; do

  yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=400 --fixed --text=$"A Theme with this name already exists. \n\nDo you want to overwrite <b>"$NEWNAME"</b>\or save as a different theme name?\n" --button=$"Overwrite $NEWNAME":2 --button=$"Save as different theme name":3 --text-align=center --center --borders=20\

    case $? in

    2) rm -r $ROFIPATH/$NEWNAME.rasi   ;;

    3) NEWNAME=$(yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=400 --height=200 --fixed --form --center --borders=20 --separator="" \
    --field=$"<b>Name for new theme</b>":LBL " " \
    --field=$" " "$NEWNAME-new" )    ;;

    252) exit 0    ;;

  esac
  
done


#create -rr file, copy and amend .rasi file

cd $ROFIPATH

echo ""$COL01" "$COL02" "$COL03" "$COL04" "$COL05" "$COL06" "$COL07" "$COL08" "$COL09" "$COL10" "$COL11" "$NEWNAME" "$MAKECUR"" > $ROFIPATH/$NEWNAME-rr

cp -r $ROFIPATH/template.rasi $ROFIPATH/$NEWNAME.rasi

sed -i "s/NEWNAME/$NEWNAME/g" $NEWNAME.rasi
sed -i "s/xyz001/$COL01/g" $NEWNAME.rasi
sed -i "s/xyz002/$COL02/g" $NEWNAME.rasi
sed -i "s/xyz003/$COL03/g" $NEWNAME.rasi
sed -i "s/xyz004/$COL04/g" $NEWNAME.rasi
sed -i "s/xyz005/$COL05/g" $NEWNAME.rasi
sed -i "s/xyz006/$COL06/g" $NEWNAME.rasi
sed -i "s/xyz007/$COL07/g" $NEWNAME.rasi
sed -i "s/xyz008/$COL08/g" $NEWNAME.rasi
sed -i "s/xyz009/$COL09/g" $NEWNAME.rasi
sed -i "s/xyz010/$COL10/g" $NEWNAME.rasi
sed -i "s/xyz011/$COL11/g" $NEWNAME.rasi


#set new theme as current

if [ "$MAKECUR" = yes ]; then

cd $HOME/.config/rofi/

ROFIOLD="$(grep "themes" $HOME/.config/rofi/config.rasi)"
ROFIOLD=$(echo "${ROFIOLD}" | sed -e 's/[]$\/*[\^]/\\&/g' )

ROFINEW="@import \""$ROFIPATH"/"$NEWNAME".rasi"\"
ROFINEW=$(echo "${ROFINEW}" | sed -e 's/[]$\/*[\^]/\\&/g' )

sed -i -e "s/${ROFIOLD}/${ROFINEW}/g" config.rasi


  yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=400 --height=200 --fixed --center --borders=20 --text=$"All done!\n<b>"$NEWNAME"</b> has been set as the current rofi theme.\n\nYou can change themes with <b>rofi-theme-selector</b>" --text-align=center --button="OK" \

fi


if [ "$MAKECUR" = no ]; then

  yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=400 --height=250 --fixed --center --borders=20 --text=$"All done!\n<b>"$NEWNAME"</b> theme has been created.\n\nYou can change themes with <b>rofi-theme-selector</b>" --text-align=center --button="OK" \
  
fi

exit
