#!/bin/bash
#MXFB-Look is a tool to save and restore desktop settings in MX-Linux Fluxbox by Melber
VERSION=2509-1
#License: GPL-3.0+

##Changelog##
#reworked for MXFB25, replaced .look with .look2 files

# add locale
export TEXTDOMAINDIR=/usr/share/locale 
export TEXTDOMAIN="mxfb-accessories"
source gettext.sh

#define some variables
#export TITLE="$(gettext 'MXFB-Look')"
export TITLE='MXFB-Look'
export CLASS="mxfb-look"
export MXLKPATH="$HOME/.config/MX-Linux/mxfb-look"
export ICONPATH="/usr/share/pixmaps/mxfb-look.svg"

# buttons
export BTN_CLOSE=$(gettext "Close") ; BTN_CLOSE+='!window-close'
export BTN_CANCEL=$(gettext "Cancel") ; BTN_CANCEL+='!window-close'
export BTN_SAVE=$(gettext "Save Current Look") ; BTN_SAVE+='!document-save'
export BTN_OK=$(gettext "OK") ; BTN_OK+='!window-ok'
export BTN_RESTORE=$(gettext "Restore Look") ; BTN_RESTORE+='!reload'
export BTN_HELP=$(gettext "Help") ; BTN_HELP+='!help-about'
export BTN_ABOUT=$(gettext "About MXFB-Look") ; BTN_ABOUT+='!help-about'


#delete stylelist and tempclr after use
trap "rm -f "/tmp/lookselection.txt" " EXIT

#create path
if [ ! -d $MXLKPATH ]; then
    mkdir $MXLKPATH
fi

#create looks folder
if [ ! -d $MXLKPATH/looks ]; then
    mkdir -p $MXLKPATH/looks
fi

#create backup folder
if [ ! -d $HOME/.restore/mxfb-look ]; then
    mkdir -p $HOME/.restore/mxfb-look
fi

#remove old Default looks
if [ -f $MXLKPATH/looks/MXFB-default.look ]; then
    rm $MXLKPATH/looks/MXFB-default.look $MXLKPATH/looks/MXFB-default.png
fi

if [ -f $MXLKPATH/looks/MX23-default.look ]; then
    mv $MXLKPATH/looks/MX23-default.look  $HOME/.restore/mxfb-look/MX23-default.look
fi

if [ -f $MXLKPATH/looks/torre-di-libretti.look ]; then
    mv $MXLKPATH/looks/torre-di-libretti.look  $HOME/.restore/mxfb-look/torre-di-libretti.look
fi

#make checkfile
if [ ! -f $MXLKPATH/checkfile.conf ]; then
    echo 'look_version=xxx' > $MXLKPATH/checkfile.conf
fi


#make MX25 Default look2 file
source $MXLKPATH/checkfile.conf

if [ "$look_version" != "$VERSION" ]; then

printf 'look_fluxstyle=/usr/share/mxflux/styles/MX-Matcha
look_theme=mx-matcha
look_icon=Papirus-mxblue
look_font="Noto Sans-11"
look_cursor=DMZ-Black
look_tintnum=1
look_tint1=$HOME/.config/tint2/tint2rc
look_tint2=None
look_rofi=$HOME/.config/rofi/themes/MX-ease-dark.rasi
look_checkfeh=
look_wallpaper=/usr/share/backgrounds/Maturity.png
look_conky1=/usr/share/mx-conky-data/themes/MX-Infinity
look_conky2=/usr/share/mx-conky-data/themes/MX-Infinity/MX-Infinity-conkyrc
look_conkystate=false' > $MXLKPATH/looks/MX25-default.look2

echo "look_version=$VERSION" > $MXLKPATH/checkfile.conf

fi

#copy image for MX25 Default look
if [ ! -f $MXLKPATH/looks/MX25-default.png ]; then
    cp /usr/share/pixmaps/MX25-default.png $MXLKPATH/looks/
fi


#check firsttext
if [ ! -f $MXLKPATH/looks/firsttext.txt ]; then
    echo '
A look is comprised of the following elements

 - Fluxbox Style
 - Wallpaper
 - Gtk-Theme
 - Icon Theme
 - Font
 - Cursor
 - Tint2 Panel (max. 2)
 - Rofi Theme
 - Conky (max. 1)' > $MXLKPATH/looks/firsttext.txt
fi


#################################
#convert .look to .look2

if compgen -G "$MXLKPATH/looks/*.look" > /dev/null; then

    look_convertlist=$(ls -f $HOME/.config/MX-Linux/mxfb-look/looks/*.look)
    look_convertarray=($look_convertlist)

    for i in "${look_convertarray[@]}"; do

        look_convertname=$(basename $i | sed -e 's/.look/.look2/' )
        look_backupname=$(basename $i)

        mapfile look_convertitems < $i

        look_fluxstyle="${look_convertitems[6]}"
        look_fluxstyle="$(echo $look_fluxstyle | sed -e "s/session\.styleFile\://" | sed -e "s/ //g" )"
        look_theme="${look_convertitems[0]}"
        look_theme="$(echo $look_theme | sed -e "s/gtk-theme-name\=//")"
        look_icon="${look_convertitems[1]}"
        look_icon="$(echo $look_icon | sed -e "s/gtk-icon-theme-name\=//")"
        look_font="${look_convertitems[2]}"
        look_font="$(echo $look_font | sed -e "s/gtk-font-name\=//" | sed -e 's/ /-/g')"
        look_cursor='DMZ-Black'
        look_tintnum="${look_convertitems[7]}"
        look_tint1="${look_convertitems[8]}"
        look_tint2="${look_convertitems[9]}"
        look_rofi="${look_convertitems[10]}"
        look_rofi="$(echo $look_rofi | sed -e "s/\@theme\ //" | sed -e "s/\"//g")"
        look_checkfeh=
        look_wallpaper="${look_convertitems[11]}"
        look_wallpaper="$(echo $look_wallpaper | sed -e "s/file\=//g")"
        look_conky1="${look_convertitems[12]}"
        look_conky1="$(echo $look_conky1 | sed -e "s/cd\ //" | sed -e "s/\"//g")"
        look_conky2="${look_convertitems[13]}"
        look_conky2="$(echo $look_conky2 | sed -e "s/conky \-c\ \"//" | sed -e "s/\"\ \&//")"
        look_conkystate="${look_convertitems[14]}"
        look_conkystate="$(echo $look_conkystate | sed -e 's/Hidden=//')"

        echo "
look_fluxstyle="$look_fluxstyle"
look_theme="$look_theme"
look_icon="$look_icon"
look_font="$look_font"
look_cursor="$look_cursor"
look_tintnum="$look_tintnum"
look_tint1="$look_tint1"
look_tint2="$look_tint2"
look_rofi="$look_rofi"
look_checkfeh=
look_wallpaper="$look_wallpaper"
look_conky1="$look_conky1"
look_conky2="$look_conky2"
look_conkystate="$look_conkystate"
" > $MXLKPATH/looks/"$look_convertname"

        mv $i $HOME/.restore/mxfb-look/"$look_backupname"
        
        
        path_check=($look_fluxstyle $look_wallpaper $look_rofi)
        
        for i in $path_check; do
            if [[  $i =~ "/home/" ]]; then
                oldusername="$(echo "$i" | cut -d/ -f3)"
                sed -i "s|/home/$oldusername/|\$HOME/|g" $MXLKPATH/looks/"$look_convertname"
                break
            fi
        done       
    done
fi


#################################
# function save look

save_look() {

#define some variables
source gettext.sh

#read current look components

#fluxbox style
look_fluxstyle="$( grep "styleFile" $HOME/.fluxbox/init | sed -e "s/session\.styleFile\://" | sed -e "s/ //g" )"

#theme and icons
look_theme="$(grep "gtk-theme-name" $HOME/.config/gtk-3.0/settings.ini | sed -e "s/gtk-theme-name\=//")"
look_icon="$(grep "gtk-icon-theme-name" $HOME/.config/gtk-3.0/settings.ini | sed -e "s/gtk-icon-theme-name\=//")"
look_font="$(grep "gtk-font-name" $HOME/.config/gtk-3.0/settings.ini | sed -e "s/gtk-font-name\=//" | sed -e 's/ /-/g')"

#cursor
look_cursor=$(grep "gtk-cursor-theme-name" $HOME/.config/gtk-3.0/settings.ini | sed -e 's/gtk-cursor-theme-name=//' )

#tint2
look_tintnum="$(wc -l < $HOME/.config/tint2/tint2-sessionfile)"
look_tint1="$(grep "tint2" $HOME/.config/tint2/tint2-sessionfile | sed -n 1p)"
look_tint1=$(echo "${look_tint1}" | sed -e 's/$HOME/\$HOME/g')
look_tint2="$(grep "tint2" $HOME/.config/tint2/tint2-sessionfile | sed -n 2p)"
look_tint2=$(echo "${look_tint2}" | sed -e 's/$HOME/\$HOME/g')

if [ $look_tintnum = 0 ]; then
    look_tint1="None"
    look_tint2="None"
fi

if [ $look_tintnum = 1 ]; then
    look_tint2="None"
fi

#rofi
look_rofi="$(grep '@theme' $HOME/.config/rofi/config.rasi | sed -e "s/\@theme\ //" | sed -e 's/"//g')"

#wallpaper
look_checkfeh=$(grep '#nitrogen' $HOME/.fluxbox/startup)

if [ -z "$look_checkfeh" ]; then

    look_wallpaper="$(grep "file=" $HOME/.config/nitrogen/bg-saved.cfg | sed -n 2p | sed -e "s/file\=//g")"

    if [ -z "$look_wallpaper" ]; then
        look_wallpaper="$(grep "file=" $HOME/.config/nitrogen/bg-saved.cfg | sed -e "s/file\=//g")"
    fi

else

    look_wallpaper=$(grep "feh" $HOME/.fehbg)
    look_wallpaper=$(echo "${look_wallpaper}" | sed -e "s/'/|/g")
    look_wallpaper=$(echo "$look_wallpaper"  | cut -d '|' -f 2)

fi


#conky
look_conky1="$(grep "conky" $HOME/.conky/conky-startup.sh | sed -n 2p | sed -e 's/cd //' | sed -e 's/"//g')"
look_conky2="$(grep "conky" $HOME/.conky/conky-startup.sh | sed -n 3p | sed -e 's/conky -c "//' | sed -e 's/" &//')"
look_conkystate="$(grep "Hidden" "$HOME/.config/autostart/conky.desktop" | sed -e 's/Hidden=//')"


#MAINSAVE

MAINSAVE=(yad --title="$TITLE" --class="$CLASS" --icon="$ICONPATH" \
--borders=20 --center --width=600 --height=200 --fixed \
--form --columns=1 --align=left --separator="" \
--button="yad-quit":1 --button="$(gettext 'Save Look')":0 \
--field="$(eval_gettext 'Save Look as')" $"New-Look-Name"\
)

NEWNAME=$("${MAINSAVE[@]}")
(($?==0)) && echo "$NEWNAME" > /tmp/look_tempname.txt

GETOUT=$(echo "$?")

if [ "$GETOUT" != 0 ]; then
    exit
elif [ "$GETOUT" = 0 ]; then
    read NEWNAME < /tmp/look_tempname.txt
    unset IFS
fi

NEWNAME=$(echo "$NEWNAME" | sed -e 's/ /-/g')


#check new Look name is not empty and forbid MXFB-default overwrite

while [ -z "$NEWNAME" -o "$NEWNAME" = 'MXFB-default' ]; do

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

    GETOUT=$(echo "$?")

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

        exit

    fi

done


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

cd $MXLKPATH/looks

while [ -f $NEWNAME.look2 ]; do
    message="$(eval_gettext 'A Look with this name already exists.\n\nDo you want to overwrite $NEWNAME or save as a new name?\n')"

    yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" \
    --text-align=center --center --borders=20 \
    --width=400 --fixed --text="$message" \
    --button="$(eval_gettext 'Overwrite $NEWNAME')":2 --button="$(gettext 'Save as different name')":3 


    case $? in

    2) rm -r $MXLKPATH/looks/$NEWNAME.look2 ;;

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

    252) exit 0    ;;

    esac
done

#make look file

echo "
look_fluxstyle="$look_fluxstyle"
look_theme="$look_theme"
look_icon="$look_icon"
look_font="$look_font"
look_cursor="$look_cursor"
look_tintnum="$look_tintnum"
look_tint1="$look_tint1"
look_tint2="$look_tint2"
look_rofi="$look_rofi"
look_checkfeh=
look_wallpaper="$look_wallpaper"
look_conky1="$look_conky1"
look_conky2="$look_conky2"
look_conkystate="$look_conkystate"
" > $MXLKPATH/looks/"$NEWNAME".look2


sed -i 's/= /=/g' $MXLKPATH/looks/"$NEWNAME".look2
sed -i "s|$HOME|\$HOME|g" $MXLKPATH/looks/"$NEWNAME".look2

#make preview image

WINLIST=$(wmctrl -l | grep -v tint2 | grep -v conky | awk '{ print$1 }')

for i in $WINLIST;
do
    WINNUM=$i
    wmctrl -i -r "$i" -b add,hidden
done

sleep 0.5
scrot $MXLKPATH/looks/"$NEWNAME".png -b -o -t 20
rm $MXLKPATH/looks/"$NEWNAME".png
mv $MXLKPATH/looks/"$NEWNAME"-thumb.png $MXLKPATH/looks/"$NEWNAME".png
sleep 0.5


for i in $WINLIST;
do
    WINNUM=$i
    wmctrl -i -r "$i" -b toggle,hidden
done


#all done
ALLDONE="$(eval_gettext 'All done!\n\nLook has been saved as <b>$NEWNAME</b>')"

yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" \
--width=400 --height=250 --fixed --center --borders=20 \
--text="$ALLDONE" --text-align=center --button="$BTN_OK":9

}



#################################
#function restore look

restore_look() {
#define some variables
source gettext.sh

#read Settings for new look
if [ -f /tmp/lookselection.txt ]; then

touch /tmp/look_tempname.txt

look_to_restore=$(cat /tmp/lookselection.txt )

source "$look_to_restore"

##################
#check if files exist

if [ ! -d "$look_fluxstyle" ] && [ ! -f "$look_fluxstyle" ]; then
    missinglist+=("Fluxbox Style- $look_fluxstyle")
    missing_fbstyle=yes
	
fi

while [ ! -f "$look_wallpaper" ]; do
  
    text_missing_wp1="$(eval_gettext 'not found!')"
	text_missing_wp2="$(eval_gettext 'Select an alternative wallpaper.')"

    cd /usr/share/backgrounds
	
	look_wallpaper=$(yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" \
    --center --width=800  --height=200 --borders=10 --image="dialog-error" --separator="" \
    --text="\n<b>$look_wallpaper $text_missing_wp1\n\n$text_missing_wp2</b>\n" --text-align=center \
	--file --image-filter --add-preview --large-preview \
    --button="${BTN_CANCEL}":31 --button="${BTN_OK}":32 )
    
    case $? in
    31 | 252 )
        look_wallpaper=/usr/share/backgrounds/THE_Dark_Metal.png
        exit
    ;;
    esac
    
done


if [ ! -d /usr/share/themes/$look_theme ] && [ ! -d $HOME/.themes/$look_theme ] && [ ! -d $HOME/.local/share/themes/$look_theme ]; then
    missinglist+=("Gtk Theme: $look_theme")
	missing_theme=yes
fi

if [ ! -d /usr/share/icons/$look_icon ] && [ ! -d $HOME/.icons/$look_icon ] && [ ! -d $HOME/.local/share/icons/$look_icon ]; then
    missinglist+=("Icon Theme: $look_icon")
	missing_icon=yes
fi

look_fontcheck=$(echo $look_font | sed -e 's/-/ /g' | sed -e 's/[0-9]//g' | sed -e 's/Condensed//')
look_fontcheck=$(echo $look_fontcheck)
look_fontcheck2=$(fc-list | grep -i "$look_fontcheck")
if [ -z "$look_fontcheck2" ]; then
    missinglist+=("Font: $look_font")
	missing_font=yes
fi

if [ ! -d /usr/share/icons/$look_cursor ] && [ ! -d $HOME/.icons/$look_cursor ] && [ ! -d $HOME/.local/share/icons/$look_cursor ]; then
    missinglist+=("Cursor: $look_cursor")
	missing_cursor=yes
fi

if [ ! -f "$look_tint1" ]; then
    missinglist+=("Tint2 Panel: $look_tint1")
	missing_tint1=yes
fi

if [ "$look_tint2" != "None" ]; then
    if [ ! -f "$look_tint2" ]; then
        missinglist+=("Tint2 Panel: $look_tint2")
		missing_tint2=yes
    fi
fi

if [ ! -f "$look_rofi" ]; then
    missinglist+=("Rofi: $look_rofi")
	missing_rofi=yes
fi

if [ ! -f "$look_conky2" ]; then
    missinglist+=("Conky: $look_conky2")
	missing_conky=yes
fi


apply_restore=yes


if [ ! -z "$missinglist" ]; then
    printf '%s\n' "${missinglist[@]}" > /tmp/look_missinglist.txt

    text_missing_1="$(gettext 'The following elements cannot be found and will not be restored.')"
    text_missing_2="$(gettext 'Continue anyway?')"
    BTN_CANCEL=$(gettext "Cancel") ; BTN_CANCEL+='!window-close'

    yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" \
    --center --width=600  --height=200 --borders=10 --image="dialog-error" \
    --text="$text_missing_1\n$text_missing_2" --text-align=center \
    --buttons-layout=spread \
    --button="${BTN_RESTORE}":21 \
    --button="${BTN_CANCEL}":22 \
    --text-info --wrap --margins=10 < /tmp/look_missinglist.txt

    case $? in

        22 | 252 )
            apply_restore=no
        ;;
        
        21 )
            apply_restore=yes
        ;;

    esac

fi


###################

look_fluxstyle="session.styleFile: $look_fluxstyle"
look_fluxstyle=$( echo "$look_fluxstyle" | sed -e 's/[]$\/*[\^]/\\&/g' )

look_theme3="gtk-theme-name=$look_theme"
look_icon3="gtk-icon-theme-name=$look_icon"
look_font=$(echo "$look_font" | sed -e 's/-/ /g')
look_font3="gtk-font-name=$look_font"
look_theme2="gtk-theme-name=\"$look_theme\""
look_icon2="gtk-icon-theme-name=\"$look_icon\""
look_font2="gtk-font-name=\"$look_font\""

look_cursor1="gtk-cursor-theme-name=$look_cursor"
look_cursor1=$( echo "$look_cursor1" | sed -e 's/[]$\/*[\^]/\\&/g' )
look_cursor2="gtk-cursor-theme-name=\"$look_cursor\""
look_cursor2=$( echo "$look_cursor2" | sed -e 's/[]$\/*[\^]/\\&/g' )

if [ -f $HOME/.icons/default/index.theme ]; then
    look_cursor3="Inherits=$look_cursor"
    look_cursor3=$( echo "$look_cursor3" | sed -e 's/[]$\/*[\^]/\\&/g' )
fi

look_tint1=$( echo "$look_tint1" | sed -e 's/[]$\/*[\^]/\\&/g' )

if [ "$look_tint2" != "None" ]; then
    look_tint2=$( echo "$look_tint2" | sed -e 's/[]$\/*[\^]/\\&/g' )
fi

look_rofi="@theme \"$look_rofi\""
look_rofi=$( echo "$look_rofi" | sed -e 's/[]$\/*[\^]/\\&/g' )

look_checkfeh=
look_wallpaper1="file=$look_wallpaper"
look_wallpaper1=$( echo "$look_wallpaper1" | sed -e 's/[]$\/*[\^]/\\&/g' )

look_conky1="cd \"$look_conky1\""
look_conky2="conky -c \"$look_conky2\" &"

#read current look components

#fluxbox style
look_fluxstyle_old="$(grep "styleFile" $HOME/.fluxbox/init)"
look_fluxstyle_old=$(echo "${look_fluxstyle_old}" | sed -e 's/[]$\/*[\^]/\\&/g' )

#theme and icons
look_theme_old3="$(grep "gtk-theme-name" $HOME/.config/gtk-3.0/settings.ini)"
look_icon_old3="$(grep "gtk-icon-theme-name" $HOME/.config/gtk-3.0/settings.ini)"
look_font_old3="$(grep "gtk-font-name" $HOME/.config/gtk-3.0/settings.ini)"

look_theme_old2="$(grep "gtk-theme-name" $HOME/.gtkrc-2.0)"
look_icon_old2="$(grep "gtk-icon-theme-name" $HOME/.gtkrc-2.0)"
look_font_old2="$(grep "gtk-font-name" $HOME/.gtkrc-2.0)"

look_theme_old3=$(echo "${look_theme_old3}" | sed -e 's/[]$ .*[\^]/\\&/g' )
look_icon_old3=$(echo "${look_icon_old3}" | sed -e 's/[]$ .*[\^]/\\&/g' )
look_font_old3=$(echo "${look_font_old3}" | sed -e 's/[]$ .*[\^]/\\&/g' )
look_theme_old2=$(echo "${look_theme_old2}" | sed -e 's/[]$ .*[\^]/\\&/g' )
look_icon_old2=$(echo "${look_icon_old2}" | sed -e 's/[]$ .*[\^]/\\&/g' )
look_font_old2=$(echo "${look_font_old2}" | sed -e 's/[]$ .*[\^]/\\&/g' )

#cursor
look_cursor_old1=$(grep "gtk-cursor-theme-name" $HOME/.config/gtk-3.0/settings.ini )
look_cursor_old1=$(echo "${look_cursor_old1}" | sed -e 's/[]$ .*[\^]/\\&/g' )

look_cursor_old2=$(grep "gtk-cursor-theme-name" $HOME/.gtkrc-2.0 )
look_cursor_old2=$(echo "${look_cursor_old2}" | sed -e 's/[]$ .*[\^]/\\&/g' )

if [ -f $HOME/.icons/default/index.theme ]; then
    look_cursor_old3=$(grep "Inherits" $HOME/.icons/default/index.theme)
    look_cursor_old3=$( echo "$look_cursor_old3" | sed -e 's/[]$\/*[\^]/\\&/g' )
fi

#tint2
look_tint1_old="$(grep "tint2" $HOME/.config/tint2/tint2-sessionfile | sed -n 1p)"
look_tint2_old="$(grep "tint2" $HOME/.config/tint2/tint2-sessionfile | sed -n 2p)"

#rofi
look_rofi_old="$(grep '@theme' $HOME/.config/rofi/config.rasi | tail -1)"
look_rofi_old=$(echo "${look_rofi_old}" | sed -e 's/[]$\/*[\^]/\\&/g' )

#wallpaper
look_checkfeh=$(grep '#nitrogen' "$HOME"/.fluxbox/startup)

if [ -z "$look_checkfeh" ]; then

    look_wallpaper_old="$(grep "file=" $HOME/.config/nitrogen/bg-saved.cfg | sed -n 2p)"
    look_wallpaper_old=$(echo "${look_wallpaper_old}" | sed -e 's/[]$\/*[\^]/\\&/g' )

    if [ -z "$look_wallpaper_old" ]; then
        look_wallpaper_old="$(grep "file=" $HOME/.config/nitrogen/bg-saved.cfg)"
        look_wallpaper_old=$(echo "${look_wallpaper_old}" | sed -e 's/[]$\/*[\^]/\\&/g' )
    fi
fi

#conky
look_conkystate_old="$(grep "Hidden" $HOME/.config/autostart/conky.desktop | sed -e 's/Hidden=//')"


#################################
#Edit Config files

if [ "$apply_restore" = "yes" ]; then

#fluxbox
    if [ "$missing_fbstyle" != "yes" ]; then
        cd $HOME/.fluxbox
        sed -i -e "s/${look_fluxstyle_old}/${look_fluxstyle}/"  ~/.fluxbox/init
        fluxbox-remote restart
    fi


#gtk
    if [ "$missing_theme" != "yes" ]; then
        sed -i -e "s/${look_theme_old3}/${look_theme3}/" $HOME/.config/gtk-3.0/settings.ini
        sed -i -e "s/${look_theme_old2}/${look_theme2}/" $HOME/.gtkrc-2.0
    fi

    if [ "$missing_icon" != "yes" ]; then
        sed -i -e "s/${look_icon_old3}/${look_icon3}/" $HOME/.config/gtk-3.0/settings.ini
        sed -i -e "s/${look_icon_old2}/${look_icon2}/" $HOME/.gtkrc-2.0
    fi

    if [ "$missing_font" != "yes" ]; then
        sed -i -e "s/${look_font_old3}/${look_font3}/" $HOME/.config/gtk-3.0/settings.ini
        sed -i -e "s/${look_font_old2}/${look_font2}/" $HOME/.gtkrc-2.0
    fi

#cursor
    if [ "$missing_cursor" != "yes" ]; then
        sed -i -e "s/${look_cursor_old1}/${look_cursor1}/" $HOME/.config/gtk-3.0/settings.ini
        sed -i -e "s/${look_cursor_old2}/${look_cursor2}/" $HOME/.gtkrc-2.0

        if [ -f $HOME/.icons/default/index.theme ]; then
            if [ "$look_cursor" = "default" ]; then
                rm -rf $HOME/.icons/default
            fi
    
            if [ "$look_cursor" != "default" ]; then    
                sed -i -e "s/${look_cursor_old3}/${look_cursor3}/" $HOME/.icons/default/index.theme
            fi
        fi


        if [ ! -f $HOME/.icons/default/index.theme ] && [ "$look_cursor" != "default" ]; then
            mkdir -p $HOME/.icons/default
            printf "[Icon Theme]
Name=Default
Comment=Default Cursor Theme
Inherits=$look_cursor" > $HOME/.icons/default/index.theme
        fi

    fi

#gtk bash
function reload_gtk_theme() {
  theme=$(gsettings get org.gnome.desktop.interface gtk-theme)
  gsettings set org.gnome.desktop.interface gtk-theme ''
  sleep 1
  gsettings set org.gnome.desktop.interface gtk-theme "$theme"
}

reload_gtk_theme


#tint
    cd $HOME/.config/tint2

    if [ "$look_tintnum" = 2 ]; then
	
    	if [ "$missing_tint1" != "yes" ] && [ "$missing_tint2" != "yes" ]; then

        rm tint2-sessionfile
        killall tint2
        look_tintNEWA=$(basename "$look_tint1")
        look_tintNEWB=$(basename "$look_tint2")
        echo "$look_tint1
$look_tint2" >> ~/.config/tint2/tint2-sessionfile
        tint2 -c $HOME/.config/tint2/"$look_tintNEWA" >/dev/null 2>&1 &  disown
        tint2 -c $HOME/.config/tint2/"$look_tintNEWB" >/dev/null 2>&1 &  disown

        fi
    fi

    if  [ "$look_tintnum" = 1 ]; then

        if [ "$missing_tint1" != "yes" ]; then

        rm tint2-sessionfile
        killall tint2
        look_tintNEWA=$(basename "$look_tint1")
        echo "$HOME/.config/tint2/$look_tintNEWA" >> ~/.config/tint2/tint2-sessionfile
        tint2 -c "$HOME"/.config/tint2/"$look_tintNEWA" >/dev/null 2>&1 &  disown

        fi
    fi

    if  [ "$look_tintnumNEW" = 0 ]; then

        rm tint2-sessionfile
        killall tint2
        echo "" >> ~/.config/tint2/tint2-sessionfile

    fi


#rofi
    if [ "$missing_rofi" != "yes" ]; then
        sed -i "s/$look_rofi_old/$look_rofi/g" $HOME/.config/rofi/config.rasi
    fi

#wallpaper

    if [ -z "$look_checkfeh" ]; then

        cd $HOME/.config/nitrogen
        sed -i -e "s/${look_wallpaper_old}/${look_wallpaper1}/g" bg-saved.cfg
        feh --bg-scale $look_wallpaper
    else
        feh --bg-scale $look_wallpaper
    fi

#conky
    if [ "$missing_conky" != "yes" ]; then

        printf "#!/bin/sh
killall conky
sleep 5s
$look_conky1
$look_conky2
exit 0" > $HOME/.conky/conky-startup.sh

        if [ "$look_conkystate" = "false" ]; then
            killall conky
            exec sh $HOME/.conky/conky-startup.sh &
            sed -i 's/Hidden=.*/Hidden=false/' $HOME/.config/autostart/conky.desktop
        else
            killall conky
            sed -i 's/Hidden=.*/Hidden=true/' $HOME/.config/autostart/conky.desktop
        fi

        conky_checkstart=$(grep 'conkystart' $HOME/.fluxbox/startup)

        if [ "$conky_checkstart" = "#conkystart" ] && [ "$look_conkystate" = "false" ]; then
            sed -i 's/#conkystart/conkystart/' $HOME/.fluxbox/startup
        fi

        if [ "$conky_checkstart" = "conkystart" ] && [ "$look_conkystate" = "true" ]; then
            sed -i 's/conkystart/#conkystart/' $HOME/.fluxbox/startup
        fi

    fi

fi

unset missinglist apply_restore missing_fbstyle missing_theme missing_icon missing_font missing_cursor missing_tint1 missing_tint2 missing_rofi missing_conky

rm -f /tmp/look_missinglist.txt

fi

}

#################################
#function about_look

about_look() {

#set some variables
source gettext.sh

#button label
BTN_LICENSE=$(gettext "License")
# button label
BTN_CHANGELOG=$(gettext "Changelog"); BTN_CHANGELOG+='!preferences-system-time-symbolic'

#about window app name
ABOUTTEXT_TOP="$(gettext 'MXFB Look')"
#about window text
ABOUTTEXT_1="$(gettext 'Version')"
ABOUTTEXT_2="$(gettext 'Save and restore desktop settings for MX-Linux Fluxbox')"
ABOUTTEXT_4="<b>mxlinux.org</b>"
ABOUTTEXT_5="Copyright (c) Melber - MX Linux"

until [ "$about_loop" = "closed" ]; do

yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" \
--borders=10 --center --width=300 --height=500 --fixed \
--image="/usr/share/pixmaps/mxfb-look-96.svg" \
--buttons-layout=spread --button="${BTN_LICENSE}":12 --button="${BTN_CHANGELOG}":10 --button="${BTN_CLOSE}":1 \
--form --columns=1 --align=center \
--field="<b><big>$ABOUTTEXT_TOP</big></b>":LBL " " \
--field=" ":LBL " " \
--field="$ABOUTTEXT_1 $VERSION":LBL " " \
--field=" ":LBL " " \
--field="<big>$ABOUTTEXT_2</big>":LBL " " \
--field=" ":LBL " " \
--field=" ":LBL " " \
--field="$ABOUTTEXT_4":BTN 'sensible-browser https://mxlinux.org' \
--field=" ":LBL " " \
--field="$ABOUTTEXT_5":LBL " " \
> /dev/null


case $? in

    1 | 252 )
        about_loop=closed
    ;;

    10 )
        printf "$(zcat /usr/share/doc/mxfb-accessories/changelog.gz)" > /tmp/look-clog.txt
        
        yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" \
        --borders=10 --center --width=400 --height=500 --fixed \
        --text-info < /tmp/look-clog.txt --wrap --show-uri \
        --button="${BTN_CLOSE}":1 \
        > /dev/null
        
        rm /tmp/look-clog.txt
    ;;
    
    12 )             
        yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" \
        --borders=10 --center --width=400 --height=500 --fixed \
        --text-info < /usr/share/doc/mxfb-accessories/copyright --wrap --show-uri \
        --button="${BTN_CLOSE}":1 \
        > /dev/null
    ;;

esac

done

}


#################################
#MAIN LOOK

#Set up pipe
export fpipe="$(mktemp -u --tmpdir looktemp.XXXXXXXX)"
mkfifo "$fpipe"
trap "rm "$fpipe"" EXIT
exec 3<> "$fpipe"


until [ "$look_status" = "finished" ]; do

    unset look_list look_array images

    look_list=$(ls -f $HOME/.config/MX-Linux/mxfb-look/looks/*.look2)
    look_array=($look_list)

    for i in "${look_array[@]}"; do
        j=$(echo $i | sed -e 's/\.look2/\.png/')
        k=$(basename $i | sed -e 's/\.look2//')
        image_add=$(echo "$i $k $j")
        images+=($image_add)
    done


look_infotext () {
echo -e "\f"

printf $1 > /tmp/lookselection.txt

source $1

if [ "$look_tintnum" = "1" ]; then
    look_tint0=$(basename "$look_tint1")
else
    look_tint1=$(basename "$look_tint1")
    look_tint2=$(basename "$look_tint2")
    look_tint0="$look_tint1
Tint2 Panel: $look_tint2"
fi

look_title=$(basename $1 | sed -e 's/\.look2//')
look_fluxstyle=$(basename "$look_fluxstyle")
look_title=$(basename $1 | sed -e 's/\.look2//')
look_tint0=$(basename "$look_tint0")
look_font=$(echo $look_font | sed -e 's/ /-/')
look_cursor=$(echo $look_cursor | sed -e 's/ /-/')
look_rofi0=$(basename "$look_rofi" | sed -e 's/\.rasi//' )
look_wallpaper=$(basename "$look_wallpaper")
look_conkyname=$(echo $look_conky1 | sed -e 's/\/usr\/share\/mx-conky-data\/themes\///')

if [ "$look_conkystate" = "false" ]; then
    look_conkystate='Active'
else
    look_conkystate='Inactive'
fi

echo "
$look_title

Fluxbox Style: $look_fluxstyle
Wallpaper: $look_wallpaper
Gtk-Theme: $look_theme
Icon Theme: $look_icon
Font: $look_font
Cursor: $look_cursor
Tint2 Panel: $look_tint0
Rofi Theme: $look_rofi0
Conky: $look_conkyname
Conky Status: $look_conkystate
"
}

export -f look_infotext


look_boxtextstart=$(cat "$MXLKPATH/looks/firsttext.txt")

echo "$look_boxtextstart" > $fpipe

key=$RANDOM


#define some variables
TEXTMAIN="$(gettext 'Select a saved look to restore or save the current setup as a new look')"

#MAIN YAD WINDOW

yad --plug=$key --tabnum=1 \
--select-action='bash -c "look_infotext %s >$fpipe"' \
--list --no-headers --hide-column=1 --print-column=3 --grid-lines=hor \
--column=Path --column=Name --column=":IMG" \
${images[@]} >/dev/null &


yad --plug=$key --tabnum=2  --listen --cycle-read \
--wrap --margins=10 --text-info= <&3  &


yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" \
--paned --key=$key --orient=Horizontal --splitter=400 \
--center --width=600 --height=450 --borders=10 \
--text="\n<b>$TEXTMAIN</b>\n" --text-align=center --buttons-layout=spread \
--button="${BTN_ABOUT}":2 \
--button="${BTN_RESTORE}":5 \
--button="${BTN_SAVE}":4 \
--button="${BTN_CLOSE}":3 \


case $? in

    2 )
        about_look
        look_status="notyet"
    ;;

    3 | 252 )
        look_status="finished"
    ;;

    4 )
        save_look
        look_status="notyet"
    ;;

    5 )
        restore_look
        look_status="notyet"
    ;;
    
esac

done

rm -f /tmp/lookselection.txt /tmp/look_tempname.txt

exit

#################################
# export functions
export -f save_look restore_look about_look
