#!/bin/bash
#mxfb-comfort is an app to change the colors of the MX-comfort themes in MX-Linux Fluxbox
#V0.10 by Melber April 2022
#thanks to fehlix, i_ri, Girafenaine, purplemoon and Jerry3904 @forum.mxlinux.org for help, tips and encouragement
#License: GPL-3.0+
#mxcr-icon based on yad icon from papirus icon set

TEXTDOMAINDIR=/usr/share/locale
export TEXTDOMAIN="mxfb-accessories"

#define some variables

TITLE="MXFB-comfort"
CLASS="mxcr"
ICONPATH="/usr/share/pixmaps/mxcr-icon.png"


#delete stylelist and tempclr after use
trap "rm -r "~/.fluxbox/scripts/tempdir" " EXIT


# Check if colors.txt exists in MX-comfort and MX-comfort-dark, create if false

if [ ! -f ~/.fluxbox/styles/MX-comfort/mxcr.txt ]; then

printf "#0f56d9 #1d2324 #ffffff #ffffff #0f56d9 #CACDD0 #1d2324 #CACDD0 #c6c8c8 #1D2324 #EEEFF4 #ffffff #1D2324 #ffffff MX-comfort yes no" > ~/.fluxbox/styles/MX-comfort/mxcr.txt

fi

if [ ! -f ~/.fluxbox/styles/MX-comfort-dark/mxcr.txt ]; then

printf "#0f56d9 #1d2324 #ffffff #ffffff #0f56d9 #CACDD0 #1d2324 #CACDD0 #c6c8c8 #1D2324 #1D2324 #ffffff #DEDFE3 #ffffff MX-comfort-dark yes no" > ~/.fluxbox/styles/MX-comfort-dark/mxcr.txt

fi

#create tempdir
if [ ! -d ~/.fluxbox/scripts/tempdir ]; then

mkdir -p ~/.fluxbox/scripts/tempdir

fi


#read available styles
ls -d ~/.fluxbox/styles/*/mxcr.txt | cut -d '/' -f6- > ~/.fluxbox/scripts/tempdir/stylelist.txt
sed -i 's|/mxcr.txt||g' ~/.fluxbox/scripts/tempdir/stylelist.txt

#select starting style

BASESTYLE=$( yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --text="\n<b>Choose which style to use as a starting point</b>\n" --center --width=400 --height=300 --center --list --no-headers --separator=" " --column=Style < ~/.fluxbox/scripts/tempdir/stylelist.txt )

GETOUT=$(echo "$?")

if [ "$GETOUT" = 1 ]; then

exit

fi

if [ -z "$BASESTYLE" ]; then

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

BASESTYLE=MX-comfort

fi


#read colors

cd ~/.fluxbox/styles/$BASESTYLE

read -r -a NEWCOLORS < mxcr.txt

ACHL="${NEWCOLORS[0]}"
ACTI="${NEWCOLORS[1]}"
ACTXCOL="${NEWCOLORS[2]}"
ACSY="${NEWCOLORS[3]}"
ACGP="${NEWCOLORS[4]}"
INHL="${NEWCOLORS[5]}"
INTI="${NEWCOLORS[6]}"
INTXCOL="${NEWCOLORS[7]}"
INSY="${NEWCOLORS[8]}"
INGP="${NEWCOLORS[9]}"
MEBG="${NEWCOLORS[10]}"
METXTI="${NEWCOLORS[11]}"
METX="${NEWCOLORS[12]}"
METXHL="${NEWCOLORS[13]}"
NEWNAME="${NEWCOLORS[14]}"
MAKECUR="${NEWCOLORS[15]}"
MAKET2="${NEWCOLORS[16]}"
PLUS="${NEWCOLORS[17]}"


# function to be called with yad button

color_picker() {
    local N=$1 C="$2"
    local YAD=(
            yad
            --title="mxcr-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

mxcrhelp() {
yad --title="MXFB-comfort help" --window-icon=/usr/share/pixmaps/mxcr-icon.png --borders=20 --center --width=700 --height=400 --image /usr/share/doc/mxfb-comfort/help/mxcr-help.png --size=fit --button="Return to Main Window":1 \

}

export -f color_picker mxcrhelp

#color selection dialogue

MAIN=(yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --borders=20 --center --width=900 --height=400 --fixed --form --columns=7 --align=right --separator=" " --button="gtk-help":"bash -c mxcrhelp" --button="gtk-quit":1 --button="gtk-ok":0 \
--text="<b>\nEdit MX-comfort colors and create a new fluxbox style.\n\n</b>" \
--field="<b>Active Window</b>":LBL " " \
--field="Window Highlight":CLR "$ACHL" \
--field="Window Title":CLR "$ACTI" \
--field="Title Text":CLR "$ACTXCOL" \
--field="Window Symbol":CLR "$ACSY" \
--field="Window Grips":CLR "$ACGP" \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field=" ":LBL " " \
--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="<b>Inctive Window</b>":LBL " " \
--field="Window Highlight":CLR "$INHL" \
--field="Window Title":CLR "$INTI" \
--field="Title Text":CLR "$INTXCOL" \
--field="Window Symbol":CLR "$INSY" \
--field="Window Grips":CLR "$INGP" \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field=" ":LBL " " \
--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='!color-select':BTN '@bash -c "color_picker 24 %24;"'
--field=" ":LBL " " \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field="<b>Fluxbox Menu</b>":LBL " " \
--field="Menu Background":CLR "$MEBG" \
--field="Menu Title Text":CLR "$METXTI" \
--field="Menu Text":CLR "$METX" \
--field="Menu HL Text":CLR "$METXHL" \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field='!color-select':BTN '@bash -c "color_picker 38 %38;"'
--field='!color-select':BTN '@bash -c "color_picker 39 %39;"'
--field='!color-select':BTN '@bash -c "color_picker 40 %40;"'
--field='!color-select':BTN '@bash -c "color_picker 41 %41;"'
--field=" ":LBL " " \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field="<b>  Name for new style        </b>":LBL " " \
--field=" " ""$NEWNAME"" \
--field="<b>  Make new style current?</b>":LBL " " \
--field=" ":CB "yes!no" \
--field="<b>  Create matching panel?</b>":LBL " " \
--field=" ":CB "no!yes" \
)

COLORS=$("${MAIN[@]}")
    (($?==0)) && echo "$COLORS" > ~/.fluxbox/scripts/tempdir/tempmain.txt

GETOUT=$(echo "$?")

if [ "$GETOUT" = 1 ]; then

exit

elif [ "$GETOUT" = 0 ]; then

cd ~/.fluxbox/scripts/tempdir

read -r -a NEWCOLORS < tempmain.txt

ACHL="${NEWCOLORS[0]}"
ACTI="${NEWCOLORS[1]}"
ACTXCOL="${NEWCOLORS[2]}"
ACSY="${NEWCOLORS[3]}"
ACGP="${NEWCOLORS[4]}"
INHL="${NEWCOLORS[5]}"
INTI="${NEWCOLORS[6]}"
INTXCOL="${NEWCOLORS[7]}"
INSY="${NEWCOLORS[8]}"
INGP="${NEWCOLORS[9]}"
MEBG="${NEWCOLORS[10]}"
METXTI="${NEWCOLORS[11]}"
METX="${NEWCOLORS[12]}"
METXHL="${NEWCOLORS[13]}"
NEWNAME="${NEWCOLORS[14]}"
MAKECUR="${NEWCOLORS[15]}"
MAKET2="${NEWCOLORS[16]}"

fi

#check style names yes no

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

  MAKET2="$MAKECUR"
  MAKECUR="$NEWNAME"  

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

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

    exit

    fi

fi

#check new style 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="Style 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 style name exists, if yes ask if overwrite or different style name

while [ -d ~/.fluxbox/styles/$NEWNAME ]; do

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

case $? in

2) rm -r ~/.fluxbox/styles/$NEWNAME   ;;

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 Style</b>":LBL " " \
--field=" " "$NEWNAME-new" )    ;;

252) exit 0    ;;

esac
done

#tint2 warning

if [ "$MAKET2" = yes ]; then

  yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=400 --fixed --text="\nA copy of the default tint2 panel <b>tint2rc</b> will be created with a matching highlight colour.\n\nCreating a matching panel may have unexpected results if you have edited or overwritten the tint2rc file.\n\n<b>Please confirm that you want to create a matching tint2 panel</b>\n" --button="Create matching tint2 panel":2 --button="Do not create matching tint2 panel":3 --text-align=center --center --borders=20\

    case $? in

    2) MAKET2="yes"   ;;

    3) MAKET2="no"    ;;

    252) exit 0    ;;

  esac
  
fi


#Copy MX-comfort to new style name

if [[ $PLUS = plus ]]; then

  cp -r ~/.fluxbox/styles/MX-comfort-plus ~/.fluxbox/styles/$NEWNAME
  
else

  cp -r ~/.fluxbox/styles/MX-comfort ~/.fluxbox/styles/$NEWNAME

fi


#move to new style directory and amend name and grips in theme config file

cd ~/.fluxbox/styles/$NEWNAME

echo ""$ACHL" "$ACTI" "$ACTXCOL" "$ACSY" "$ACGP" "$INHL" "$INTI" "$INTXCOL" "$INSY" "$INGP" "$MEBG" "$METXTI" "$METX" "$METXHL" "$NEWNAME" "$MAKECUR" "$MAKET2" "$PLUS"" > ~/.fluxbox/styles/$NEWNAME/mxcr.txt

sed -i "s/mx-comfort/$NEWNAME/g" theme.cfg
sed -i "s/menu.frame.color:                    #EEEFF4/menu.frame.color:                    $MEBG/g" theme.cfg
sed -i "s/menu.title.color:                    #1D2324/menu.title.color:                    $ACTI/g" theme.cfg
sed -i "s/menu.hilite.color:                   #0f56d9/menu.hilite.color:                   $ACHL/g" theme.cfg
sed -i "s/menu.frame.disableColor:             #0f56d9/menu.frame.disableColor:             $ACHL/g" theme.cfg
sed -i "s/menu.frame.textColor:                #1D2324/menu.frame.textColor:                   $METX/g" theme.cfg
sed -i "s/menu.title.textColor:                #ffffff/menu.title.textColor:                   $METXTI/g" theme.cfg
sed -i "s/menu.hilite.textColor:               #ffffff/menu.hilite.textColor:                   $METXHL/g" theme.cfg
sed -i "s/window.label.focus.textColor:        #ffffff/window.label.focus.textColor:                   $ACTXCOL/g" theme.cfg
sed -i "s/window.label.unfocus.textColor:      #CACDD0/window.label.unfocus.textColor:                   $INTXCOL/g" theme.cfg
sed -i "s/window.grip.focus.color:             #0f56d9/window.grip.focus.color:             $ACGP/g" theme.cfg
sed -i "s/window.grip.unfocus.color:           #1D2324/window.grip.unfocus.color:           $INGP/g" theme.cfg
sed -i "s/window.handle.focus.color:           #1D2324/window.handle.focus.color:           $ACTI/g" theme.cfg
sed -i "s/window.handle.unfocus.color:         #1D2324/window.handle.unfocus.color:         $INTI/g" theme.cfg


#move to pixmap directory and amend colors

cd ~/.fluxbox/styles/$NEWNAME/pixmaps

sed -i "s/#0f56d9/$ACHL/g" *
sed -i "s/#1d2324/$ACTI/g" *
sed -i "s/#ffffff/$ACSY/g" *
sed -i "s/#CACDD0/$INHL/g" *-inactive.xpm
sed -i "s/#1D2324/$INTI/g" *-inactive.xpm
sed -i "s/#c6c8c8/$INSY/g" *-inactive.xpm


#copy and amend tint2 panel

if [ "$MAKET2" = yes ]; then

cd ~/.config/tint2
cp -r ~/.config/tint2/tint2rc ~/.config/tint2/$NEWNAME
sed -i "s/#0f56d9/$ACHL/g" $NEWNAME
rm tint2-sessionfile
echo '$HOME'"/.config/tint2/$NEWNAME" >> tint2-sessionfile
killall tint2
tint2 -c ~/.config/tint2/$NEWNAME >/dev/null 2>&1 & disown

fi

#set new style as current

if [ "$MAKECUR" = yes ]; then

sed -i -r "/^(session.styleFile:).*/s::\1    ~/.fluxbox/styles/$NEWNAME:"  ~/.fluxbox/init

fluxbox-remote restart

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 style." --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> style has been created.\n\nYou can change styles with \n<b>Fluxbox menu  >  Appearance  >  Style</b> \n\nand refresh with \n<b>Fluxbox menu  >  Leave  >  Refresh</b> " --text-align=center --button="OK" \

fi

exit
