#!/bin/bash
#Edit the highlight colors of MX-Linux themes
#for MX-Linux 25 by Melber
#License: GPL-3.0+
export mxtc_version='26.03.01'

export TEXTDOMAINDIR=/usr/share/locale
export TEXTDOMAIN="mx-theme-colors"
source gettext.sh

#variables

#TRANSLATORS App Name
export title="$(gettext 'MX Theme Colors')"
export class="mx-theme-colors"
export iconpath="/usr/share/icons/hicolor/scalable/apps/mx-theme-colors.svg"

#check for wayland
if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
#TRANSLATORS warning message that tool doesn't work with wayland
    wayland_text_1="$(gettext 'Sorry, this tool is not available when running a wayland session.')"

    yad --title="$title" --class="$class" --window-icon="$iconpath" \
    --borders=10 --center --width=300 --height=100 \
    --text="\n$wayland_text_1\n" --text-align=center \
    --button=yad-close \

    exit
fi

#check for themes
if [ -d /usr/share/themes/mx-matcha ]; then
    found_matcha='mx-matcha!'
fi

if [ -d /usr/share/themes/mx-matcha-dark ]; then
    found_matchadark='mx-matcha-dark!'
fi

if [ -d /usr/share/themes/mx-ease ]; then
    found_ease='mx-ease!'
fi

if [ -d /usr/share/themes/mx-ease-Dark ]; then
    found_easedark='mx-ease-Dark'
fi

found_themes="$found_matcha$found_matchadark$found_ease$found_easedark"

if [ -z "$found_themes" ]; then
#TRANSLATORS warning message if themes are not installed line 1
    nothemes_text_1="$(gettext 'Themes not found!')"
#TRANSLATORS warning message if themes are not installed line 2
    nothemes_text_2="$(gettext 'This tool requires the MX-Matcha and MX-Ease themes to be installed.')"

    yad --title="$title" --class="$class" --window-icon="$iconpath" \
    --borders=10 --center --width=300 --height=100 \
    --text="\n\n<b>$nothemes_text_1</b>\n\n$nothemes_text_2\n" --text-align=center \
    --button=yad-close \
    
    exit
    
fi


# color picker function for main window

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


# help function for main window

mxtc-help() {
#TRANSLATORS Help window title
text_helptitle="$(gettext 'MX Theme Colors Help')"
#TRANSLATORS Help window text line 0
help_text0="$(gettext 'To create a new theme:')"
#TRANSLATORS Help window text line 1
help_text1="$(gettext 'Select theme to be edited from the drop-down list')"
#TRANSLATORS Help window text line 2
help_text2="$(gettext 'Select new main color with the color palette or picker')"
#TRANSLATORS Help window text line 3
help_text3="$(gettext 'Select new secondary color with the color palette or picker')"
#TRANSLATORS Help window text line 4
help_text4="$(gettext 'Update the preview image with the current selections')"
#TRANSLATORS Help window text line 4a
help_text4a="$(gettext 'Repeat steps 1-4 until you are satisfied with the selections')"
#TRANSLATORS Help window text line 5
help_text5="$(gettext 'Enter name for new theme')"
#TRANSLATORS Help window text line 5a
help_text5a="$(gettext '(name is appended to base theme name)')"
#TRANSLATORS Help window text line 7
help_text6="$(gettext 'Confirm if hidpi and xhidpi versions should be created')"
#TRANSLATORS Help window text line 6
help_text7="$(gettext 'Click to create theme')"
#TRANSLATORS Help window text line 7
help_text8="$(gettext 'Note: themes are saved in ~/.themes')"


printf "$help_text0

1. $help_text1
2. $help_text2
3. $help_text3
4. $help_text4
   $help_text4a
5. $help_text5
   $help_text5a
6. $help_text6
7. $help_text7

$help_text8" > /tmp/mxtc-help.txt

help_imagepath=/usr/share/mx-theme-colors/mxtc-help.png
hkey=$RANDOM

yad --plug=$hkey --tabnum=1 \
--image="$help_imagepath" \
> /dev/null &

yad --plug=$hkey --tabnum=2 \
--text-info --wrap < /tmp/mxtc-help.txt \
--margins=5 \
> /dev/null &

yad --paned --key=$hkey --orient=horiz --splitter=350 \
--title="$text_helptitle" --class="$class" --window-icon="$iconpath" \
--borders=10 --center --width=1050 --height=300 --text-align=left \
--button="yad-close" \

rm /tmp/mxtc-help.txt

}


# about window
mxtc-about() {

#TRANSLATORS Text in About window
text_about=$(gettext "Edit the highlight colors of MX themes.")

iconpath96="/usr/share/icons/hicolor/scalable/apps/mx-theme-colors96.svg"

yad --about --class="$class" --window-icon="$iconpath" \
--image="$iconpath96" \
--pname="$title" \
--pversion="$mxtc_version" \
--comments="$text_about" \
--website-label="https://mxlinux.org/" \
--website="https://mxlinux.org/" \
--copyright="Copyright (c) 2026 Melber" \
--authors="Melber,MX Devs" \
--license="GPL3" \
--center
}


# color_change function called with yad button
color_change () {

    echo -e "\f"
    local theme_selection=$1 color_main=$2 color_secondary=$3
    
    case $theme_selection in
    mx-ease )
        preview_image=ease-preview.svg
        cp /usr/share/mx-theme-colors/$preview_image /tmp/preview-template.svg

        sed -i "s/#0f56d9/$color_main/g" /tmp/preview-template.svg
        sed -i "s/#c3d5f6/$color_secondary/g" /tmp/preview-template.svg
    ;;
    
    mx-ease-Dark )
        preview_image=ease-dark-preview.svg
        cp /usr/share/mx-theme-colors/$preview_image /tmp/preview-template.svg

        sed -i "s/#2e63ba/$color_main/g" /tmp/preview-template.svg
        sed -i "s/#a3b5d9/$color_secondary/g" /tmp/preview-template.svg
    ;;
    
    mx-matcha )
        preview_image=matcha-preview.svg
        cp /usr/share/mx-theme-colors/$preview_image /tmp/preview-template.svg

        sed -i "s/#0f56d9/$color_main/g" /tmp/preview-template.svg
        sed -i "s/#7cb7f0/$color_secondary/g" /tmp/preview-template.svg
    ;;
    
    mx-matcha-dark )
        preview_image=matcha-dark-preview.svg
        cp /usr/share/mx-theme-colors/$preview_image /tmp/preview-template.svg

        sed -i "s/#0f56d9/$color_main/g" /tmp/preview-template.svg
        sed -i "s/#7cb7f0/$color_secondary/g" /tmp/preview-template.svg
    ;;
    esac
    
echo "/tmp/preview-template.svg"

}


### main function
create_theme () {

#text variables

#TRANSLATORS Main window label
text_mainitem0="$(gettext 'Base Theme')"
#TRANSLATORS Main window label
text_mainitem1="$(gettext 'Main Color')"
#TRANSLATORS Main window label
text_mainitem2="$(gettext 'Secondary Color')"
#TRANSLATORS Main window label
text_mainitem3="$(gettext 'Update Preview')"
#TRANSLATORS Main window label
text_mainname1="$(gettext 'Name for new theme')"
#TRANSLATORS Main window label
text_mainname2="$(gettext '(name is appended to base theme)')"
#TRANSLATORS Main window label
text_maincurrent="$(gettext 'Make new theme current')"
#TRANSLATORS Main window label
text_mainhidpi="$(gettext 'Create hidpi and xhidpi versions')"

#buttons
#TRANSLATORS Button text
BTN_CLOSE="$(gettext 'Close')" ; BTN_CLOSE+='!window-close!'
#TRANSLATORS Button text
BTN_CREATE="$(gettext 'Create Theme')" ; BTN_CREATE+='!gtk-ok!'
#TRANSLATORS Button text
BTN_HELP="$(gettext 'Help')" ; BTN_HELP+='!help!'
#TRANSLATORS Button text
BTN_ABOUT="$(gettext 'About')" ; BTN_ABOUT+='!help-about!'
#TRANSLATORS Button text for color picker
btn_picker="$(gettext 'Custom Color Picker')"

#pipe
export fpipe="$(mktemp -u --tmpdir mxthemeeditor.XXXXXX)"
mkfifo "$fpipe"
trap "rm "$fpipe"" EXIT
exec 3<> "$fpipe"

rkey=$RANDOM

echo "/usr/share/mx-theme-colors/matcha-preview.svg">$fpipe


# main window

yad --plug=$rkey --tabnum=2 \
--form --columns=1 --align=left --separator="|" \
--field="<b>$text_mainitem0</b>":LBL " " \
--field="":CB "$found_themes" \
--field=" ":LBL " " \
--field="<b>$text_mainitem1</b>":LBL " " \
--field=" ":CLR "#0f56d9" \
--field="$btn_picker!color-select!":BTN '@bash -c "color_picker 5 %5;"' \
--field=" ":LBL " " \
--field="<b>$text_mainitem2</b>":LBL " " \
--field=" ":CLR "#c3d5f6" \
--field="$btn_picker!color-select!":BTN '@bash -c "color_picker 9 %9;"' \
--field=" ":LBL " " \
--field="$text_mainitem3!reload!":BTN '@bash -c "color_change %2 %5 %9 >$fpipe"' \
--field=" ":LBL " " \
--field="<b>$text_mainname1</b>":LBL " " \
--field=" " "new" \
--field="  $text_mainname2":LBL " " \
--field=" ":LBL " " \
--field="$text_mainhidpi":CHK "TRUE" \
>/tmp/mxtc-output.txt &


#TRANSLATORS Main window label
text_previewbox="$(gettext 'Preview')"

yad --plug=$rkey --tabnum=1 \
--list --no-headers --no-selection \
--text="<b>$text_previewbox</b>" --text-align=left \
--listen --cycle-read \
--column=":IMG" <&3  &

#TRANSLATORS Main window text line 1
TEXT1="$(gettext 'Edit the highlight colors of the mx-matcha and mx-ease themes.')"
#TRANSLATORS Main window text line 2
TEXT2="$(gettext 'Select base theme, main and secondary colors.')"
#TRANSLATORS Main window text line 3
TEXT3="$(gettext 'Update preview, name and create new theme.')"

yad --paned --key=$rkey \
--orient=horiz --splitter=300 \
--title="$title" --class="$class" --window-icon="$iconpath" \
--borders=10 --center --width=700 --height=550 --buttons-layout=spread \
--text="<b>$TEXT1</b>\n$TEXT2\n$TEXT3" --text-align=center \
--button="${BTN_ABOUT}":"bash -c mxtc-about" \
--button="${BTN_HELP}":"bash -c mxtc-help" \
--button="${BTN_CLOSE}":2 \
--button="${BTN_CREATE}":4 --align-buttons \

ret=$?

case $ret in
    2 | 252 )
      alldone="yep"

    ;;

    4 )
        #read colors and theme name
        sed -i 's/ /-/g' /tmp/mxtc-output.txt
        sed -i 's/|/ /g' /tmp/mxtc-output.txt
        
        read -a yadarray < /tmp/mxtc-output.txt
        
        theme_name_base=${yadarray[0]}
        theme_name_new=${yadarray[0]}-${yadarray[3]} 
        color_main_new=${yadarray[1]}
        color_secondary_new=${yadarray[2]}
        theme_hidpi_new=${yadarray[4]}
        
        theme_path_new=$HOME/.themes/$theme_name_new
        
        
        #pre copy setup
        if [ ! -d "$HOME/.themes" ]; then
            mkdir -p $HOME/.themes
        fi
        
        
        #check if style name exists

        while [ -d $theme_path_new ]; do
    
            #TRANSLATORS Existing theme name window text line 1
    	    text_overwrite1="$(gettext 'A theme with this name already exists.')"
            #TRANSLATORS Existing theme name window text line 2
    	    text_overwrite2="$(gettext 'Do you want to overwrite or save as a different theme name')"
    	    #TRANSLATORS Existing theme name button
    	    btn_overwrite="$(gettext 'Overwrite theme')"
    	    #TRANSLATORS Existing theme name button
        	btn_savediff="$(gettext 'Save as different name')"

            yad --title="$TITLE" --class="$CLASS" --window-icon="$iconpath" \
            --width=400 --fixed \
            --text="$text_overwrite1\n\n$text_overwrite2\n" \
            --text-align=center --center --borders=20 \
            --button="$btn_overwrite":2 --button="$btn_savediff":3

            case $? in

            2) rm -r $theme_path_new   ;;

            3) 
            theme_name_new=$(yad --title="$title" --class="$class" --window-icon="$iconpath" \
            --width=400 --height=200 --fixed --form --center --borders=20 --separator="" \
            --field="$text_mainname" "$theme_name_new" )    
            
            theme_path_new=$HOME/.themes/$theme_name_new
            
            ;;
            
            252) exit 0    ;;

            esac
        done
        

        # start progress yad
        
        #TRANSLATORS Progress window text
        progress_text="$(gettext 'Creating theme')" 
        {
        waittime=0
        echo $waittime
        echo "# $((waittime))%"

        
        #copy theme        
        cp -r /usr/share/themes/$theme_name_base $theme_path_new
        sed -i "s/$theme_name_base/$theme_name_new/g" $theme_path_new/index.theme
        
        #matcha pre-fixes
        if [ $theme_name_base = mx-matcha ] || [ $theme_name_base = mx-matcha-dark ]; then
            sed -i "s/0F56D9/0f56d9/g" $theme_path_new/gtk-2.0/gtkrc
            sed -i "s/7CB7F0/7cb7f0/g" $theme_path_new/gtk-2.0/gtkrc
            sed -i "s/C0C6CA/7cb7f0/g" $theme_path_new/gtk-2.0/gtkrc        

            sed -i "s/0F56D9/0f56d9/g" $theme_path_new/gtk-3.0/gtk.css
            sed -i "s/7CB7F0/7cb7f0/g" $theme_path_new/gtk-3.0/gtk.css
            sed -i "s/449AED/7cb7f0/g" $theme_path_new/gtk-3.0/gtk.css
            sed -i '95 s/C0C6CA/7cb7f0/' $theme_path_new/gtk-3.0/gtk.css
            sed -i '95 s/383D44/7cb7f0/' $theme_path_new/gtk-3.0/gtk.css            
            sed -i 's/rgba(124, 183, 240, 0.35)/#7cb7f0/g' $theme_path_new/gtk-3.0/gtk.css
            sed -i 's/rgba(52, 152, 219, 0.55)/#7cb7f0/g' $theme_path_new/gtk-3.0/gtk.css


            sed -i "s/0F56D9/0f56d9/g" $theme_path_new/gtk-4.0/gtk.css
            sed -i "s/7CB7F0/7cb7f0/g" $theme_path_new/gtk-4.0/gtk.css
            sed -i "s/449AED/7cb7f0/g" $theme_path_new/gtk-4.0/gtk.css
            sed -i '95 s/C0C6CA/7cb7f0/' $theme_path_new/gtk-4.0/gtk.css
            sed -i '95 s/383D44/7cb7f0/' $theme_path_new/gtk-4.0/gtk.css            
            sed -i 's/rgba(124, 183, 240, 0.35)/#7cb7f0/g' $theme_path_new/gtk-4.0/gtk.css

        fi

        #make conf
        printf "#mx theme colors config file - do not delete
theme_name_base=$theme_name_base
theme_name_new=$theme_name_new
color_main_new=$color_main_new
color_secondary_new=$color_secondary_new" > $theme_path_new/mx-theme-colors.conf
        
        #edit colors  
    
        case $theme_name_base in     
            mx-ease )
                color_main_old=#0f56d9
                color_secondary_old=#c3d5f6
            ;;
            mx-ease-Dark )
                color_main_old=#2e63ba
                color_secondary_old=#a3b5d9
            ;;
            mx-matcha )
                color_main_old=#0f56d9
                color_secondary_old=#7cb7f0
            ;;
            mx-matcha-dark )
                color_main_old=#0f56d9
                color_secondary_old=#7cb7f0
            ;;
        esac


        #cinnamon
        if [ -d $theme_path_new/cinnamon ]; then
        
            sed -i "s/0F56D9/0f56d9/g" $theme_path_new/cinnamon/cinnamon.css
            sed -i "s/7CB7F0/7cb7f0/g" $theme_path_new/cinnamon/cinnamon.css
            sed -i 's/C0C6CA/7cb7f0/' $theme_path_new/cinnamon/cinnamon.css
            sed -i 's/3774E4/0f56d9/' $theme_path_new/cinnamon/cinnamon.css
            sed -i 's/rgba(124, 183, 240, 0.35)/#7CB7F0/g' $theme_path_new/cinnamon/cinnamon.css
            sed -i 's/rgba(15, 86, 217, 0.25)/#7CB7F0/g' $theme_path_new/cinnamon/cinnamon.css

            sed -i "s/$color_main_old/$color_main_new/g" $theme_path_new/cinnamon/cinnamon.css
            sed -i "s/$color_secondary_old/$color_secondary_new/g" $theme_path_new/cinnamon/cinnamon.css

            cinnamon_index=$(ls $theme_path_new/cinnamon/*/*/*.svg)
            cinnamon_index=($cinnamon_index)

            for i in "${cinnamon_index[@]}"; do
                sed -i "s/0F56D9/0f56d9/g" $i
                sed -i "s/$color_main_old/$color_main_new/g" $i
            done

        fi


        #gnome-shell
        if [ -d $theme_path_new/gnome-shell ]; then
        
            sed -i "s/0F56D9/0f56d9/g" $theme_path_new/gnome-shell/gnome-shell.css
            sed -i "s/7CB7F0/7cb7f0/g" $theme_path_new/gnome-shell/gnome-shell.css
            sed -i 's/C0C6CA/7cb7f0/' $theme_path_new/gnome-shell/gnome-shell.css
            sed -i 's/3774E4/0f56d9/' $theme_path_new/gnome-shell/gnome-shell.css
            sed -i 's/rgba(124, 183, 240, 0.35)/#7CB7F0/g' $theme_path_new/gnome-shell/gnome-shell.css
            sed -i 's/rgba(15, 86, 217, 0.25)/#7CB7F0/g' $theme_path_new/gnome-shell/gnome-shell.css
            
            sed -i "s/$color_main_old/$color_main_new/g" $theme_path_new/gnome-shell/gnome-shell.css
            sed -i "s/$color_secondary_old/$color_secondary_new/g" $theme_path_new/gnome-shell/gnome-shell.css

            gnomeshell_index=$(ls $theme_path_new/gnome-shell/assets/*.svg)
            gnomeshell_index=($gnomeshell_index)

            for i in "${gnomeshell_index[@]}"; do
                sed -i "s/0F56D9/0f56d9/g" $i
                sed -i "s/$color_main_old/$color_main_new/g" $i
            done

        fi
        
        
        #templates
        template_path=/usr/share/mx-theme-colors/templates  
        
        #progress yad 25%
        waittime=25
        echo $waittime
        echo "# $((waittime))%"
    
        
        #xfwm
        cp -r $template_path/$theme_name_base/xfwm /tmp/temp-xfwm
        cd /tmp/temp-xfwm
        
        xfwm_index=$(ls $template_path/$theme_name_base/xfwm/*.svg)
        xfwm_index=($xfwm_index)
        
        for i in "${xfwm_index[@]}"; do
            icon_name=$(basename $i | sed -e 's/.svg//g')
            sed -i "s/$color_main_old/$color_main_new/g" $icon_name.svg
            sed -i "s/$color_secondary_old/$color_secondary_new/g" $icon_name.svg
            rsvg-convert $icon_name.svg > $theme_path_new/xfwm4/$icon_name.png
        done
        
        
        #hidpi 
        
        if [ $theme_hidpi_new = TRUE ]; then

            if [ $theme_name_base = mx-ease-Dark ]; then
                cp -r /usr/share/themes/mx-ease-hidpi-Dark $theme_path_new-hidpi
                sed -i "s/mx-ease-hidpi-Dark/$theme_name_new-hidpi/g" $theme_path_new-hidpi/index.theme            
            else        
                cp -r /usr/share/themes/$theme_name_base-hidpi $theme_path_new-hidpi
                sed -i "s/$theme_name_base/$theme_name_new/g" $theme_path_new-hidpi/index.theme
            fi

            if [ $theme_name_base = mx-ease ] || [ $theme_name_base = mx-ease-Dark ]; then

            for i in "${xfwm_index[@]}"; do
                icon_name=$(basename $i | sed -e 's/.svg//g')
                rsvg-convert -w 48 -h 48 -a $icon_name.svg > $theme_path_new-hidpi/xfwm4/$icon_name.png
            done

            elif [ $theme_name_base = mx-matcha ] || [ $theme_name_base = mx-matcha-dark ]; then

            for i in "${xfwm_index[@]}"; do
                icon_name=$(basename $i | sed -e 's/.svg//g')
                rsvg-convert -z 1.5 $icon_name.svg > $theme_path_new-hidpi/xfwm4/$icon_name.png
            done
            
            fi


        #xhidpi

            if [ $theme_name_base = mx-ease-Dark ]; then
                cp -r /usr/share/themes/mx-ease-xhidpi-Dark $theme_path_new-xhidpi
                sed -i "s/mx-ease-xhidpi-Dark/$theme_name_new-xhidpi/g" $theme_path_new-xhidpi/index.theme            
            else        
                cp -r /usr/share/themes/$theme_name_base-xhidpi $theme_path_new-xhidpi
                sed -i "s/$theme_name_base/$theme_name_new/g" $theme_path_new-xhidpi/index.theme
            fi

            if [ $theme_name_base = mx-ease ] || [ $theme_name_base = mx-ease-Dark ]; then

            for i in "${xfwm_index[@]}"; do
                icon_name=$(basename $i | sed -e 's/.svg//g')
                rsvg-convert -w 64 -h 64 -a $icon_name.svg > $theme_path_new-xhidpi/xfwm4/$icon_name.png
            done

            elif [ $theme_name_base = mx-matcha ] || [ $theme_name_base = mx-matcha-dark ]; then

            for i in "${xfwm_index[@]}"; do
                icon_name=$(basename $i | sed -e 's/.svg//g')
                rsvg-convert -z 2.0 $icon_name.svg > $theme_path_new-xhidpi/xfwm4/$icon_name.png
            done

            fi
        
        fi


        #progress yad 50%
        waittime=50
        echo $waittime
        echo "# $((waittime))%"

          
        #gtk2
        sed -i "s/$color_main_old/$color_main_new/g" $theme_path_new/gtk-2.0/gtkrc
        sed -i "s/$color_secondary_old/$color_secondary_new/g" $theme_path_new/gtk-2.0/gtkrc
                           
        cp -r $template_path/$theme_name_base/gtk2 /tmp/temp-gtk2
        cd /tmp/temp-gtk2
        
        gtk2_index=$(ls $template_path/$theme_name_base/gtk2/*.svg)
        gtk2_index=($gtk2_index)
        
        for i in "${gtk2_index[@]}"; do
            icon_name=$(basename $i | sed -e 's/.svg//g')
            sed -i "s/$color_main_old/$color_main_new/g" $icon_name.svg
            sed -i "s/$color_secondary_old/$color_secondary_new/g" $icon_name.svg
            rsvg-convert $icon_name.svg > $theme_path_new/gtk-2.0/assets/$icon_name.png
        done

   
        #progress yad 75%
        waittime=75
        echo $waittime
        echo "# $((waittime))%"

         
        #gtk3
        sed -i "s/$color_main_old/$color_main_new/g" $theme_path_new/gtk-3.0/gtk.css
        sed -i "s/$color_secondary_old/$color_secondary_new/g" $theme_path_new/gtk-3.0/gtk.css
        
        if [ $theme_name_base = mx-ease ] || [ $theme_name_base = mx-ease-Dark ]; then
            sed -i "s/#A3B5D9/$color_main_new/g" $theme_path_new/gtk-3.0/gtk.css
            sed -i "s/#A3B5D9/$color_secondary_new/g" $theme_path_new/gtk-3.0/gtk.css
            sed -i "s/$color_main_old/$color_main_new/g" $theme_path_new/gtk-3.0/extra-tweaks.css
            sed -i "s/$color_secondary_old/$color_secondary_new/g" $theme_path_new/gtk-3.0/extra-tweaks.css
        fi
                             
        cp -r $template_path/$theme_name_base/gtk3 /tmp/temp-gtk3
        cd /tmp/temp-gtk3

        gtk3_index=$(ls $template_path/$theme_name_base/gtk3/*.svg)
        gtk3_index=($gtk3_index)

        for i in "${gtk3_index[@]}"; do
            icon_name=$(basename $i | sed -e 's/.svg//g')
            sed -i "s/$color_main_old/$color_main_new/g" $icon_name.svg
            sed -i "s/$color_secondary_old/$color_secondary_new/g" $icon_name.svg
            rsvg-convert $icon_name.svg > $theme_path_new/gtk-3.0/assets/$icon_name.png
            rsvg-convert -x 2.0 -y 2.0 $icon_name.svg > $theme_path_new/gtk-3.0/assets/$icon_name@2.png
        done


        #gtk4
        cp -rf $theme_path_new/gtk-3.0/assets $theme_path_new/gtk-4.0
 
        sed -i "s/$color_main_old/$color_main_new/g" $theme_path_new/gtk-4.0/gtk.css       
        sed -i "s/$color_secondary_old/$color_secondary_new/g" $theme_path_new/gtk-4.0/gtk.css
                             
        # close progress yad
        waittime=100
        echo $waittime
        echo "# $((waittime))%"
        progress_text="$(gettext 'Creating theme')"

        } | yad --progress --title="$title" --class="$class" \
        --borders=10 --center --width=400 \
        --text="<b>$progress_text</b>" --text-align=center \
        --auto-close --auto-kill
    
        
        #all done
        #TRANSLATORS Task Completed window text 1
        text_finished1="$(gettext 'All done!')"
        #TRANSLATORS Task Completed window text 2, text is preceeded by theme name and followed by path
        text_finished2="$(gettext 'has been saved in')"

        text_finished3="$HOME/.themes"
        #TRANSLATORS Task Completed window text 3
        text_finished4="$(gettext 'Change themes with MX-Tweak')"

        yad --title="$title" --class="$class" --window-icon="$iconpath" \
        --width=400 --height=200 --fixed --center --borders=20 --text-align=center \
        --text="$text_finished1\n\n<b>$theme_name_new</b> $text_finished2 $text_finished3\n\n$text_finished4" \
        --button=yad-ok
        
        alldone="nope"

        rm "$fpipe"
    ;;

esac

exec 3>&-

#clean up temp files
rm -rf /tmp/preview-template.svg /tmp/temp-xfwm /tmp/temp-gtk2 /tmp/temp-gtk3 /tmp/mxtc-output.txt

}

export -f color_picker mxtc-help mxtc-about color_change create_theme


until [ "$alldone" = "yep" ]; do
    create_theme
done


exit 0
