#!/bin/bash
#MX-Rofi-Manager - A tool to manage rofi settings
#Version 24.01.01 by Melber and MX Devs
#License: GPL-3.0+

#set up translation
source gettext.sh
export TEXTDOMAINDIR=/usr/share/locale 
export TEXTDOMAIN="mx-rofi-manager"

#define some variables
ROFIPATH="$HOME/.config/rofi/themes"
ICONPATH="/usr/share/pixmaps/mxrm-icon.svg"

#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

#create mxrm-settings
if [ ! -d $ROFIPATH/mxrm-settings ]; then
	mkdir -p $ROFIPATH/mxrm-settings
fi
	
if [ ! -f $ROFIPATH/mxrm-settings/mxrm-settings.rasi ]; then
	echo "/**
 * ROFI mxrm-settings by Melber & MX-Devs
 */
 
 * {
font: \"Noto Sans 12\";
}

window {
anchor: center;
location: center;
y-offset: 0;
x-offset: 0;
width: 30%;
}

listview {
lines: 12;
}" > $ROFIPATH/mxrm-settings/mxrm-settings.rasi

fi


# notify user and back up config.rasi if mx-rofi-manager has not already been launched

if [ ! -f $HOME/.config/rofi/mxrm-check ]; then
    NOW=$(date +%Y"%m%d")
    MESSAGE="$(eval_gettext '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>')"
    
    TITLE="$(gettext 'MX-Rofi-Manager')"
    CLASS="mx-rofi-manager"
    ICONPATH="/usr/share/pixmaps/mxrm-icon.svg"

##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="gtk-cancel":4 --button="$(gettext 'OK to proceed?')":6

    case $? in

    4 | 252 )
	    exit 0
	;;
	
    6 )
        mkdir -p $HOME/.restore/rofi/
        cp $HOME/.config/rofi/config.rasi $HOME/.restore/rofi/config.rasi_$(date -d "today" +"%Y%m%d")
        cp $ROFIPATH/MX-comfort.rasi $HOME/.restore/rofi/MX-comfort.rasi_$(date -d "today" +"%Y%m%d")
        cp -f /usr/share/rofi/config.rasi $HOME/.config/rofi/
        #cp -f /usr/share/mxrr/MX-*.rasi $HOME/.config/rofi/themes/
        cp -f /usr/share/mxrr/*-rr $HOME/.config/rofi/themes/
        cp -f /usr/share/mxrr/template*.rasi $HOME/.config/rofi/themes/mxrm-settings/
        cp -f /usr/share/mxrr/display* $HOME/.config/rofi/themes/mxrm-settings/
        cp -f /usr/share/rofi/themes/README $HOME/.config/rofi/themes/README

        if [ "${DESKTOP_SESSION##*/}" != "fluxbox" ]; then
            cp -f /usr/share/mxrr/MX-*.rasi $HOME/.config/rofi/themes/
        fi

        echo '#mxrm-check
#do not delete, file used to set up MX-rofi-manager' > $HOME/.config/rofi/mxrm-check
    ;;
    esac
fi


#v0.11 edit config.rasi

RASI_CHECK2="$(grep "mxrm-settings" $HOME/.config/rofi/config.rasi)"

if [ -z "$RASI_CHECK2" ]; then
	RASINEWLINE="@import \"/mxrm-settings/mxrm-settings.rasi\""
	echo $RASINEWLINE >> $HOME/.config/rofi/config.rasi
fi

FA_CHECK="$(fc-list | grep font-awesome)"
RASI_CHECK="$(grep "display0" $HOME/.config/rofi/config.rasi )"

if [ -z "$FA_CHECK" ]; then
    if [ "$RASI_CHECK" != '//@import "/mxrm-settings/display0.rasi"' ]; then
	    DISPLAYON='@import "/mxrm-settings/display0.rasi"'
        DISPLAYOFF='//@import "/mxrm-settings/display0.rasi"'
	    sed -i -e "s/${DISPLAYON}/${DISPLAYOFF}/g" $HOME/.config/rofi/config.rasi
    fi
fi


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

# function rofi_theme

rofi_theme() {

TITLE="$(gettext 'Rofi-Theme')"
CLASS="mx-rofi-manager"
ROFIPATH="$HOME/.config/rofi/themes"
ROFIPATHUSR="/usr/share/rofi/themes"
ROFIPATHEXTRA="/usr/share/rofi/themes/extra-themes"
ICONPATH="/usr/share/pixmaps/mxrm-icon.svg"
source gettext.sh

#################################
#fix rasi

cd $HOME/.config/rofi


#################################
#define function rofi_preview
rofi_preview() {

TEMPTHEME=$1

if [ "$TEMPTHEME" = '<b>---User---</b>' -o "$TEMPTHEME" = '<b>---Extras---</b>' -o "$TEMPTHEME" = '<b>---System---</b>' ]; then

	exit

fi


###TEXTREQAWESOME=' (fonts-font-awesome recommended for tab icons)'
###TEMPTHEME=$(echo "${TEMPTHEME}" | sed -e "s/$TEXTREQAWESOME//g" )

ROFIPATH="$HOME/.config/rofi/themes"
ROFIPATHUSR="/usr/share/rofi/themes"
ROFIPATHEXTRA="/usr/share/rofi/themes/extra-themes"

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

if [ -f $ROFIPATHUSR/$TEMPTHEME.rasi ]; then

	ROFITEMP="@theme \""$ROFIPATHUSR"/"$TEMPTHEME".rasi"\"
	ROFITEMP=$(echo "${ROFITEMP}" | sed -e 's/[]$\/*[\^]/\\&/g' )
	
	elif
	
	[ -f $ROFIPATHEXTRA/$TEMPTHEME.rasi ]; then

	ROFITEMP="@theme \""$ROFIPATHEXTRA"/"$TEMPTHEME".rasi"\"
	ROFITEMP=$(echo "${ROFITEMP}" | sed -e 's/[]$\/*[\^]/\\&/g' )
	
	elif
	
	[ -f $ROFIPATH/$TEMPTHEME.rasi ]; then

	ROFITEMP="@theme \""$ROFIPATH"/"$TEMPTHEME".rasi"\"
	ROFITEMP=$(echo "${ROFITEMP}" | sed -e 's/[]$\/*[\^]/\\&/g' )
	
fi


cd $HOME/.config/rofi/

cp config.rasi config-temp.rasi

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

	DISPLAYOFF='//@import "/mxrm-settings/display0.rasi"'
	DISPLAYOFF=$(echo "${DISPLAYOFF}" | sed -e 's/[]$\/*[\^]/\\&/g' )
	
	DISPLAYON='@import "/mxrm-settings/display0.rasi"'
	DISPLAYON=$(echo "${DISPLAYON}" | sed -e 's/[]$\/*[\^]/\\&/g' )

	sed -i -e "s/${DISPLAYON}/${DISPLAYOFF}/g" $HOME/.config/rofi/config.rasi

rofi -show

mv -f config-temp.rasi config.rasi

exit

}

export -f rofi_preview


###################################################################
# function rofi_help-theme

rofi_help-theme() {

TITLE="$(gettext 'MX-Rofi-Help')"
CLASS="mx-rofi-manager"
ICONPATH="/usr/share/pixmaps/mxrm-icon.svg"

HELPTEXTTHEME="$(gettext '
SPECIFIC AND GLOBAL SETTINGS

Settings for position, size and font can have two formats:

--Specific: settings come directly from the <some name.rasi> file with its particular values. Rofi manager can edit the specific settings of User themes (~/.config/rofi/themes/<some name.rasi>) but those of Extra and System themes can only be changed with a text editor.

--Global: settings come from a separate file (~/.config/rofi/themes/mxrm-settings/mxrm-settings.rasi) and are applied to all themes. Rofi manager can edit these by changing the current theme to that file.')"

yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" \
--borders=20 --width=600 --height=400 --center --fixed --text-info<<<"$HELPTEXTTHEME" --wrap --margins=20 --button="gtk-close" \
	
}

export -f rofi_help-theme

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

#read available themes

cd $ROFIPATH
echo "<b>---User---</b>" > $ROFIPATH/tempdir/rasilist.txt
ls -f *rasi >> $ROFIPATH/tempdir/rasilist.txt

cd $ROFIPATHEXTRA
echo "<b>---Extras---</b>" >> $ROFIPATH/tempdir/rasilist.txt
ls -f *rasi >> $ROFIPATH/tempdir/rasilist.txt

cd $ROFIPATHUSR
echo "<b>---System---</b>" >> $ROFIPATH/tempdir/rasilist.txt
ls -f *rasi >> $ROFIPATH/tempdir/rasilist.txt

sed -i 's/.rasi//g' $ROFIPATH/tempdir/rasilist.txt
sed -i '/template/d' $ROFIPATH/tempdir/rasilist.txt


#add requires font-awesome

###declare -a REQAWESOME=(MX-iconic)

###for i in "${REQAWESOME[@]}"

###do

###	TEXTREQAWESOME=' (fonts-font-awesome recommended for tab icons)'
	###sed -i "s/$i/$i$TEXTREQAWESOME/g" $ROFIPATH/tempdir/rasilist.txt

###done


#select theme

ROFICURRENT="$(grep '@theme' $HOME/.config/rofi/config.rasi | tail -1)"

ROFICURRENT=$(echo "${ROFICURRENT}" | sed -e 's/\"//g' | sed -e 's/@theme//g' | sed -e 's/ //g' | sed -e 's/.rasi//g' )

ROFICURRENT=$(basename "${ROFICURRENT}")

TOGGLECURRENT="$(grep '//@import "/mxrm-settings/mxrm-settings.rasi"' $HOME/.config/rofi/config.rasi)"

if [ "$TOGGLECURRENT" = "" ]; then
	
	TOGGLETEXT='global'
	#TOGGLETEXTTRANS="$(gettext 'global')"
	 
	else
	  
	TOGGLETEXT='specific'
	#TOGGLETEXTTRANS="$(gettext 'specific')"
	
fi


TEXTTHEME1="$(gettext '<b>Select appearance settings and theme to use</b> (double-click for preview).')"

TEXTTHEME3="$(eval_gettext 'The current theme is <b>$ROFICURRENT</b> and its settings are <b>$TOGGLETEXT</b>.')"

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

CBCURRENT="$TOGGLETEXT"

if [ "$CBCURRENT" = global ]; then

	CBOTHER='specific'
	
	else
	
	CBOTHER='global'
	
fi


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


fkey=$RANDOM

yad --plug="$fkey" --tabnum=1 --form --text-align=left --separator="" \
--field="$(gettext 'Settings')":CB "$CBCURRENT!$CBOTHER" \
--field=" ":LBL " " \ > $ROFIPATH/tempdir/themetog.txt &


yad --plug="$fkey" --tabnum=2 --list --no-headers --column=Style --text-align=left --separator="" --dclick-action='@bash -c "rofi_preview %s;"' < $ROFIPATH/tempdir/rasilist.txt > $ROFIPATH/tempdir/themesel.txt &


yad --paned --key="$fkey" --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --text="$TEXTTHEME1\n$TEXTTHEME3\n" --width=600 --height=400 --borders=20 --center --fixed --button="gtk-help":"bash -c rofi_help-theme" --button="gtk-cancel":1 --button="gtk-ok":0


GETOUT=$(echo "$?")


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

	  exit

fi


read -r -a THEMESEL < $ROFIPATH/tempdir/themesel.txt
read -r -a TOGGLESEL < $ROFIPATH/tempdir/themetog.txt

	  NEWTHEME="${THEMESEL[0]}"
	  TOGGLE="${TOGGLESEL[0]}"

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

if [ -z "$NEWTHEME" -o "$NEWTHEME" = '<b>---User---</b>' -o "$NEWTHEME" = '<b>---Extras---</b>' -o "$TEMPTHEME" = '<b>---System---</b>' ]; then

	TEXTTHEME3="$(gettext 'No theme selected.')"

	yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --text="$TEXTTHEME3" --button=gtk-ok --text-align=center --center --width=400 --height=200 --borders=20 \

	exit

fi

###################
#remove font-awesome

###	NEWTHEME=$(echo "${NEWTHEME}" | sed -e "s/$TEXTREQAWESOME//g" )

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

#check change and make current

if [ -f $ROFIPATHUSR/$NEWTHEME.rasi ]; then

	ROFINEW="@theme \""$ROFIPATHUSR"/"$NEWTHEME".rasi"\"
###	CHECKAWESOME="$(grep "flag-for-font-awesome" $ROFIPATHUSR/$NEWTHEME.rasi)"
	
	elif
	
	[ -f $ROFIPATHEXTRA/$NEWTHEME.rasi ]; then

	ROFINEW="@theme \""$ROFIPATHEXTRA"/"$NEWTHEME".rasi"\"
###	CHECKAWESOME="$(grep "flag-for-font-awesome" $ROFIPATHEXTRA/$NEWTHEME.rasi)"
	
	elif
	
	[ -f $ROFIPATH/$NEWTHEME.rasi ]; then

	ROFINEW="@theme \""$ROFIPATH"/"$NEWTHEME".rasi"\"
###	CHECKAWESOME="$(grep "flag-for-font-awesome" $ROFIPATH/$NEWTHEME.rasi)"
	
fi



ROFIOLD="$(grep "@theme" $HOME/.config/rofi/config.rasi | tail -1)"


if [ "$TOGGLETEXT" = global ]; then

	TOGGLECHECK=global
	
else

	TOGGLECHECK=specific

fi

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


if [ "$ROFINEW" = "$ROFIOLD" ]; then

	TEXTROFINOCHANGE="$(eval_gettext 'No change to current settings!\n')"
	
	if [ "$TOGGLECHECK" = "$TOGGLE" ]; then

	yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=400 --height=200 --fixed --center --borders=20 --text="$TEXTROFINOCHANGE" --text-align=center --button="gtk-ok" \
	
	exit
	
	fi
	
fi
	
###################	

#check font-awesome



###if [ "$CHECKAWESOME" = "" ]; then

###	CHECKDISPLAY="$(grep "display0" $HOME/.config/rofi/config.rasi )"
	
###	if [ "$CHECKDISPLAY" != '//@import "/mxrm-settings/display0.rasi"' ]; then
	
###	DISPLAYOFF='//@import "/mxrm-settings/display0.rasi"'
###	DISPLAYOFF=$(echo "${DISPLAYOFF}" | sed -e 's/[]$\/*[\^]/\\&/g' )
	
###	DISPLAYON='@import "/mxrm-settings/display0.rasi"'
###	DISPLAYON=$(echo "${DISPLAYON}" | sed -e 's/[]$\/*[\^]/\\&/g' )

###	sed -i -e "s/${DISPLAYON}/${DISPLAYOFF}/g" $HOME/.config/rofi/config.rasi
	
###	fi

###fi

###if [ "$CHECKAWESOME" != "" ]; then

###	CHECKDISPLAY="$(grep "display0" $HOME/.config/rofi/config.rasi )"
	
###	if [ "$CHECKDISPLAY" != '@import "/mxrm-settings/display0.rasi"' ]; then
	
###	DISPLAYOFF='//@import "/mxrm-settings/display0.rasi"'
###	DISPLAYOFF=$(echo "${DISPLAYOFF}" | sed -e 's/[]$\/*[\^]/\\&/g' )
	
###	DISPLAYON='@import "/mxrm-settings/display0.rasi"'
###	DISPLAYON=$(echo "${DISPLAYON}" | sed -e 's/[]$\/*[\^]/\\&/g' )

###	sed -i -e "s/${DISPLAYOFF}/${DISPLAYON}/g" $HOME/.config/rofi/config.rasi
	
###	fi

###fi


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

declare -a THEMECHECK=(MX-sidebar MX-appfinder MX-run)

for i in "${THEMECHECK[@]}"

do

	if [ $i = $NEWTHEME ]; then

		if [ "$TOGGLE" = global ]; then
		
		TEXTTHEME5="$(eval_gettext 'Global settings are blocked for')"
		TEXTTHEME6="$(gettext 'Specific settings will be used')"
		
		yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --text="$TEXTTHEME5\n<b>$NEWTHEME</b>\n$TEXTTHEME6" --button=gtk-ok --text-align=center --center --width=400 --height=200 --borders=20 \
		
		TOGGLE=specific
		
		fi
	fi
done


cd $HOME/.config/rofi/

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

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


if [ "$TOGGLECHECK" = "$TOGGLE" ]; then

	TOGGLE=NOCHANGE

fi


SPECIFIC='//@import "/mxrm-settings/mxrm-settings.rasi"'
SPECIFIC=$(echo "${SPECIFIC}" | sed -e 's/[]$\/*[\^]/\\&/g' )
	
GLOBAL='@import "/mxrm-settings/mxrm-settings.rasi"'
GLOBAL=$(echo "${GLOBAL}" | sed -e 's/[]$\/*[\^]/\\&/g' )
	

if [ "$TOGGLE" = specific ]; then

	sed -i -e "s/${GLOBAL}/${SPECIFIC}/g" $HOME/.config/rofi/config.rasi

fi

if [ "$TOGGLE" = global ]; then

	sed -i -e "s/${SPECIFIC}/${GLOBAL}/g" $HOME/.config/rofi/config.rasi

fi


#########

TEXTTHEME4="$(eval_gettext 'All done!\n\n<b>$NEWTHEME</b> has been set as the current rofi theme.\n\n')"

yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=400 --height=200 --fixed --center --borders=20 --text="$TEXTTHEME4" --text-align=center --button="gtk-ok" \

exit

}


###################################################################
###################################################################
# function rofi_tabs

rofi_tabs() {

TITLE="$(gettext 'Rofi-Tab-Selector')"
CLASS="mx-rofi-manager"
ROFIPATH="$HOME/.config/rofi/themes"
ROFIPATHUSR="/usr/share/rofi/themes"
ICONPATH="/usr/share/pixmaps/mxrm-icon.svg"
source gettext.sh


TEXTTABS1="$(gettext '<b>Select which tabs rofi should display</b>\n')"
TEXTTABS2="$(gettext 'the MX default that displays available apps')"
TEXTTABS3="$(gettext 'a powerful calculation engine')"
TEXTTABS4="$(gettext 'fast access to files')"
TEXTTABS5="$(gettext 'displays all available executable files')"
TEXTTABS6="$(gettext 'opens a secure shell')"
TEXTTABS7="$(gettext 'enables easy switching')"
TEXTTABS8="$(gettext 'a set of links about using rofi')"
TEXTTABS9="$(gettext 'tab will not be shown\n')\n"
TEXTTABS10="$(gettext 'If a theme does not display tabs, switch tabs with <i>Shift + Arrow</i> or <i>Ctrl + Tab</i>\n')"


CURRENTTABS="$(grep 'modi:' $HOME/.config/rofi/config.rasi)"
CURRENTTABS=$(echo "${CURRENTTABS}" | sed -e "s/help:\/usr\/share\/rofi\/rofi-help/help/g" )
CURRENTTABS=$(echo "${CURRENTTABS}" | sed -e "s/calc:qalc/calc/g" )
CURRENTTABS=$(echo "${CURRENTTABS}" | sed -e 's/modi: //g' | sed -e 's/;//g'| sed -e 's/,/" "/g')

TABARRAY=($CURRENTTABS)
CURTAB1=${TABARRAY[0]}
CURTAB1=$(echo "${CURTAB1}" | sed -e 's/"//g' )
CURTAB2=${TABARRAY[1]}
CURTAB2=$(echo "${CURTAB2}" | sed -e 's/"//g' )
CURTAB3=${TABARRAY[2]}
CURTAB3=$(echo "${CURTAB3}" | sed -e 's/"//g' )
CURTAB4=${TABARRAY[3]}
CURTAB4=$(echo "${CURTAB4}" | sed -e 's/"//g' )
CURTAB5=${TABARRAY[4]}
CURTAB5=$(echo "${CURTAB5}" | sed -e 's/"//g' )
CURTAB6=${TABARRAY[5]}
CURTAB6=$(echo "${CURTAB6}" | sed -e 's/"//g' )

if [ "$CURTAB2" = "" ]; then

	  CURTAB2=none

fi

if [ "$CURTAB3" = "" ]; then

	  CURTAB3=none

fi

if [ "$CURTAB4" = "" ]; then

	  CURTAB4=none

fi

if [ "$CURTAB5" = "" ]; then

	  CURTAB5=none

fi

if [ "$CURTAB6" = "" ]; then

	  CURTAB6=none

fi



TABS=(yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --text-align=left --height=450 --width=600 --borders=20 --center --fixed --separator=" " --form --columns=3 --output-by-row --button="gtk-cancel":1 --button="gtk-ok":0 \
--text="$TEXTTABS1
<i>drun</i>: $TEXTTABS2
<i>calc</i>: $TEXTTABS3
<i>filebrowser</i>: $TEXTTABS4
<i>run</i>: $TEXTTABS5
<i>ssh</i>: $TEXTTABS6
<i>window</i>: $TEXTTABS7
<i>help</i>: $TEXTTABS8
<i>none</i>: $TEXTTABS9

$TEXTTABS10" 
--field="Tab 1 ":CB "$CURTAB1!drun!calc!filebrowser!run!ssh!window!help!none" \
--field="Tab 4 ":CB "$CURTAB4!drun!calc!filebrowser!run!ssh!window!help!none" \
--field="  Tab 2 ":CB "$CURTAB2!drun!calc!filebrowser!run!ssh!window!help!none" \
--field="  Tab 5 ":CB "$CURTAB5!drun!calc!filebrowser!run!ssh!window!help!none" \
--field="  Tab 3 ":CB "$CURTAB3!drun!calc!filebrowser!run!ssh!window!help!none" \
--field="  Tab 6 ":CB "$CURTAB6!drun!calc!filebrowser!run!ssh!window!help!none" \
)

NEWTABS=$("${TABS[@]}")
(($?==0)) && echo "$NEWTABS" > $ROFIPATH/tempdir/temptabs.txt

GETOUT=$(echo "$?")

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

	  exit

fi

read -r -a TEMPTABS < $ROFIPATH/tempdir/temptabs.txt

	TAB0="${TEMPTABS[0]}"
	TAB1="${TEMPTABS[1]}"
	TAB2="${TEMPTABS[2]}"
	TAB3="${TEMPTABS[3]}"
	TAB4="${TEMPTABS[4]}"
	TAB5="${TEMPTABS[5]}"

NEWTABS="modi: \""$TAB0","$TAB1","$TAB2","$TAB3","$TAB4","$TAB5"\";"

	NEWTABS=$(echo "${NEWTABS}" | sed -e "s/help/help\:\/usr\/share\/rofi\/rofi-help/g" )
	NEWTABS=$(echo "${NEWTABS}" | sed -e "s/calc/calc\:qalc/g" )
	NEWTABS=$(echo "${NEWTABS}" | sed -e "s/,none//g" )


OLDTABS="$(grep "modi" $HOME/.config/rofi/config.rasi)"


if [ "$NEWTABS" = "$OLDTABS" ]; then

	TEXTTABNOCHANGE="$(eval_gettext 'No change to current settings!\n')"

	yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=400 --height=200 --fixed --center --borders=20 --text="$TEXTTABNOCHANGE" --text-align=center --button="gtk-ok" \
	

	exit

else

	cd $HOME/.config/rofi/
	
	NEWTABS=$(echo "${NEWTABS}" | sed -e 's/[]$\/*[\^]/\\&/g' )
	OLDTABS=$(echo "${OLDTABS}" | sed -e 's/[]$\/*[\^]/\\&/g' )
	
	sed -i -e "s/${OLDTABS}/${NEWTABS}/g" config.rasi

	TEXTTABS2="$(gettext 'All done!\n\nTab configuration has been updated.\n\n')"

	yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=400 --height=200 --fixed --center --borders=20 --text="$TEXTTABS2" --text-align=center --button="gtk-ok" \

fi


exit

}


###################################################################
###################################################################
# function rofi_position

rofi_position() {

TITLE="$(gettext 'Rofi-Position')"
CLASS="mx-rofi-manager"
ROFIPATH="$HOME/.config/rofi/themes"
ROFIPATHUSR="/usr/share/rofi/themes"
ICONPATH="/usr/share/pixmaps/mxrm-icon.svg"
source gettext.sh

###################################################################
# function rofi_help-position

rofi_help-position() {

TITLE="$(gettext 'MX-Rofi-Help')"
CLASS="mx-rofi-manager"
ICONPATH="/usr/share/pixmaps/mxrm-icon.svg"

HELPTEXTPOS="$(gettext '
TERMS

- LOCATION: The general placement of the rofi window on the screen

- ANCHOR: The point of the rofi window which is set at the location.

- X-OFFSET & Y-OFFSET: The offset (pixels) of the rofi window from the anchor point


EXAMPLES

Location center, anchor center: places the center of the rofi window at the center of the screen

Location northwest, anchor northwest: places the top left corner of the rofi window at the top left of the screen.

Location center, anchor northwest: places the top left corner of the rofi window at the center of the screen.

Location west, anchor west, x-offset 32: displaces the rofi window to avoid overlap with a vertical panel.')"

yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" \
--borders=20 --width=800 --height=600 --center --fixed --text-info --wrap --margins=20 <<<"$HELPTEXTPOS" --image=/usr/share/pixmaps/mxrm-help-position.png --button="gtk-close" \
	
}

export -f rofi_help-position

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

#check MX-sidebar, define Settingslocation

cd $HOME/.config/rofi

ROFICURRENT="$(grep '@theme' $HOME/.config/rofi/config.rasi | tail -1)"

ROFICURRENT=$(echo "${ROFICURRENT}" | sed -e 's/\"//g' | sed -e 's/@theme//g' | sed -e 's/ //g' | sed -e 's/.rasi//g' )

ROFICURRENT=$(basename "${ROFICURRENT}")


TOGGLECURRENT="$(grep '//@import "/mxrm-settings/mxrm-settings.rasi"' $HOME/.config/rofi/config.rasi)"

if [ "$TOGGLECURRENT" = "" ]; then
	
	TOGGLETEXT='global'
	#TOGGLETEXTTRANS="$(gettext 'global')"
	SETTINGSLOCATION="$ROFIPATH/mxrm-settings/mxrm-settings.rasi"
	 
	else
	  
	TOGGLETEXT='specific'
	#TOGGLETEXTTRANS="$(gettext 'specific')"
	SETTINGSLOCATION="$ROFIPATH/$ROFICURRENT.rasi"
	
fi


declare -a THEMECHECK=(MX-sidebar MX-appfinder MX-run)

for i in "${THEMECHECK[@]}"

do

	if [ $i = $ROFICURRENT ]; then
	
	TOGGLETEXT='specific'
	#TOGGLETEXTTRANS="$(gettext 'specific')"
	SETTINGSLOCATION="$ROFIPATH/$ROFICURRENT.rasi"
	
	fi
	
done


###################################################################
#check theme is editable

NEWPOSCHECK=FALSE


if [ ! -f $ROFIPATH/$ROFICURRENT.rasi ]; then

	TEXTSORRY="$(eval_gettext 'Sorry, the appearance settings for $ROFICURRENT can only be modified using global settings.\n\nWould you like to change to global settings?.\n\n')"

	if [ "$TOGGLETEXT" = 'specific' ]; then

	yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" \
	--borders=20 --width=400 --height=200 --center --fixed --margins=20 \
	--text="$TEXTSORRY" --text-align=center --button="gtk-no" --button="gtk-yes" \

	GETOUT=$(echo "$?")


	if [ "$GETOUT" = 1 ]; then

		TOGGLETEXT='global'
		#TOGGLETEXTTRANS="$(gettext 'global')"
		SETTINGSLOCATION="$ROFIPATH/mxrm-settings/mxrm-settings.rasi"
		
		SPECIFIC='//@import "/mxrm-settings/mxrm-settings.rasi"'
		SPECIFIC=$(echo "${SPECIFIC}" | sed -e 's/[]$\/*[\^]/\\&/g' )
		GLOBAL='@import "/mxrm-settings/mxrm-settings.rasi"'
		GLOBAL=$(echo "${GLOBAL}" | sed -e 's/[]$\/*[\^]/\\&/g' )
		
		sed -i -e "s/${SPECIFIC}/${GLOBAL}/g" $HOME/.config/rofi/config.rasi
		
		NEWPOSCHECK=TRUE

	elif [ "$GETOUT" != 1 ]; then

		exit

	fi
	fi

fi


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

#get current position

ROFILOCATIONOLD="$(grep "location" $SETTINGSLOCATION)"

	if [ "$ROFILOCATIONOLD" = "" ]; then

		ROFILOCATIONOLD='     location: center;'
		sed -ie "/^window/a $ROFILOCATIONOLD" $SETTINGSLOCATION

	fi
	
LOCVALUE=$(echo "${ROFILOCATIONOLD}" | sed -e 's/location\://g' | sed -e 's/\;//g' | sed -e 's/ //g')

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

######

ROFIANCHOROLD="$(grep "anchor" $SETTINGSLOCATION)"

	if [ "$ROFIANCHOROLD" = "" ]; then

		ROFIANCHOROLD='     anchor: center;'
		sed -ie "/^window/a $ROFIANCHOROLD" $SETTINGSLOCATION

	fi
	
ANCVALUE=$(echo "${ROFIANCHOROLD}" | sed -e 's/anchor\://g' | sed -e 's/\;//g' | sed -e 's/ //g' )

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

#####

ROFIXOFFSETOLD="$(grep "x-offset" $SETTINGSLOCATION)"

	if [ "$ROFIXOFFSETOLD" = "" ]; then

		ROFIXOFFSETOLD='     x-offset: 0;'
		sed -ie "/^window/a $ROFIXOFFSETOLD" $SETTINGSLOCATION

	fi

XOFFVALUE=$(echo "${ROFIXOFFSETOLD}" | sed -e 's/x-offset\://g' | sed -e 's/\;//g' )

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

#####

ROFIYOFFSETOLD="$(grep "y-offset" $SETTINGSLOCATION)"

	if [ "$ROFIYOFFSETOLD" = "" ]; then

		ROFIYOFFSETOLD='     y-offset: 0;'
		sed -ie "/^window/a $ROFIYOFFSETOLD" $SETTINGSLOCATION

	fi
	
YOFFVALUE=$(echo "${ROFIYOFFSETOLD}" | sed -e 's/y-offset\://g' | sed -e 's/\;//g' )
	
ROFIYOFFSETOLD=$(echo "${ROFIYOFFSETOLD}" | sed -e 's/[]$\/*[\^]/\\&/g' )

#####

ROFIWIDTHOLD="$(grep "width" $SETTINGSLOCATION)"

	if [ "$ROFIWIDTHOLD" = "" ]; then

		ROFIWIDTHOLD='     width: 30%;'
		sed -ie "/^window/a $ROFIWIDTHOLD" $SETTINGSLOCATION

	fi
	
WIDVALUE=$(echo "${ROFIWIDTHOLD}" | sed -e 's/width\://g' | sed -e 's/\;//g' | sed -e 's/\%//g' )

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

#####

ROFIHEIGHTOLD="$(grep "lines" $SETTINGSLOCATION)"

	if [ -z "$ROFIHEIGHTOLD" ]; then

		ROFIHEIGHTOLD='     lines: 12;'
		sed -ie "/^listview/a $ROFIHEIGHTOLD" $SETTINGSLOCATION

	fi


LINVALUE=$(echo "${ROFIHEIGHTOLD}" | sed -e 's/lines\://g' | sed -e 's/\;//g' )

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


#get new position
#MAINPOS

TEXTLOCMAIN1="$(gettext '<b>Adjust the location and size of the current rofi window.\n</b>')"

#TEXTLOCMAIN2="$(eval_gettext 'The current appearance settings are <b>$TOGGLETEXTTRANS</b>\n')"

TEXTLOCMAIN2="$(eval_gettext 'The current appearance settings are <b>$TOGGLETEXT</b>')"

MAINPOS=(yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --borders=20 --center --width=600 --height=400 --fixed --form --columns=1 --align=left --separator=" " --button="gtk-help":"bash -c rofi_help-position" --button="gtk-cancel":1 --button="gtk-ok":0 \
--text="$TEXTLOCMAIN1\n$TEXTLOCMAIN2\n" \
--field="$(gettext 'Location of rofi window on the screen')":CB "$LOCVALUE!northwest!north!northeast!west!center!east!southwest!south!southeast" \
--field="$(gettext '    X-Offset (- to left, + to right)')":NUM "$XOFFVALUE\!-500..500\!1\!0" \
--field="$(gettext '    Y-Offset (- upwards, + downwards)')":NUM "$YOFFVALUE\!-500..500\!1\!0" \
--field="$(gettext 'Width (percentage of screen width)')":NUM "$WIDVALUE\!1..100\!1\!0" \
--field="$(gettext 'Height (number of lines shown in list)')":NUM "$LINVALUE\!1..25\!1\!0" \
)

POS=$("${MAINPOS[@]}")
	(($?==0)) && echo "$POS" > $ROFIPATH/tempdir/tempposition.txt

GETOUT=$(echo "$?")

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

	  exit

elif [ "$GETOUT" = 0 ]; then

	  cd $ROFIPATH/tempdir

	  read -r -a NEWPOS < tempposition.txt

	  LOCNEW="${NEWPOS[0]}"
	  XOFNEW="${NEWPOS[1]}"
	  YOFNEW="${NEWPOS[2]}"
	  WIDNEW="${NEWPOS[3]}"
	  HIGNEW="${NEWPOS[4]}"

fi


ROFILOCATIONNEW="location: "$LOCNEW";"
ROFILOCATIONNEW=$(echo "${ROFILOCATIONNEW}" | sed -e 's/[]$\/*[\^]/\\&/g' )

ROFIANCHORNEW="anchor: "$LOCNEW";"
ROFIANCHORNEW=$(echo "${ROFIANCHORNEW}" | sed -e 's/[]$\/*[\^]/\\&/g' )

ROFIXOFFSETNEW="x-offset: "$XOFNEW";"
ROFIXOFFSETNEW=$(echo "${ROFIXOFFSETNEW}" | sed -e 's/[]$\/*[\^]/\\&/g' )

ROFIYOFFSETNEW="y-offset: "$YOFNEW";"
ROFIYOFFSETNEW=$(echo "${ROFIYOFFSETNEW}" | sed -e 's/[]$\/*[\^]/\\&/g' )

ROFIWIDTHNEW="width: "$WIDNEW"%;"
ROFIWIDTHNEW=$(echo "${ROFIWIDTHNEW}" | sed -e 's/[]$\/*[\^]/\\&/g' )

ROFIHEIGHTNEW="lines: "$HIGNEW";"
ROFIHEIGHTNEW=$(echo "${ROFIHEIGHTNEW}" | sed -e 's/[]$\/*[\^]/\\&/g' )


#check change

NEWPOSLIST="$ROFILOCATIONNEW
$ROFIANCHORNEW
$ROFIXOFFSETNEW
$ROFIYOFFSETNEW
$ROFIWIDTHNEW
$ROFIHEIGHTNEW
$NEWPOSCHECK"

OLDPOSLIST="$ROFILOCATIONOLD
$ROFIANCHOROLD
$ROFIXOFFSETOLD
$ROFIYOFFSETOLD
$ROFIWIDTHOLD
$ROFIHEIGHTOLD
FALSE"


if [ "$NEWPOSLIST" = "$OLDPOSLIST" ]; then

	TEXTPOSNOCHANGE="$(eval_gettext 'No change to current settings!\n')"

	yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=400 --height=200 --fixed --center --borders=20 --text="$TEXTPOSNOCHANGE" --text-align=center --button="gtk-ok" \
	

	exit
	

else


	sed -i -e "s/${ROFILOCATIONOLD}/${ROFILOCATIONNEW}/g" $SETTINGSLOCATION
	sed -i -e "s/${ROFIANCHOROLD}/${ROFIANCHORNEW}/g" $SETTINGSLOCATION
	sed -i -e "s/${ROFIXOFFSETOLD}/${ROFIXOFFSETNEW}/g" $SETTINGSLOCATION
	sed -i -e "s/${ROFIYOFFSETOLD}/${ROFIYOFFSETNEW}/g" $SETTINGSLOCATION
	sed -i -e "s/${ROFIWIDTHOLD}/${ROFIWIDTHNEW}/g" $SETTINGSLOCATION
	sed -i -e "s/${ROFIHEIGHTOLD}/${ROFIHEIGHTNEW}/g" $SETTINGSLOCATION


	TEXTPOSDONE1="$(eval_gettext 'All done!\nPosition and size of the current rofi theme have been amended.')"

	yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=400 --height=200 --fixed --center --borders=20 --text="$TEXTPOSDONE1" --text-align=center --button="gtk-ok" \


#delete rasie

STRANGERASIE=$(echo "${ROFICURRENT}" | sed -e 's/rasi/rasie/g' )

	rm "$STRANGERASIE"


exit

fi

}


###################################################################
###################################################################
# function rofi_font

rofi_font() {

TITLE="$(gettext 'Rofi-Font')"
CLASS="mx-rofi-manager"
ROFIPATH="$HOME/.config/rofi/themes"
ROFIPATHUSR="/usr/share/rofi/themes"
ICONPATH="/usr/share/pixmaps/mxrm-icon.svg"
source gettext.sh

###################################################################
#define Settingslocation
ROFICURRENT="$(grep '@theme' $HOME/.config/rofi/config.rasi | tail -1)"

ROFICURRENT=$(echo "${ROFICURRENT}" | sed -e 's/\"//g' | sed -e 's/@theme//g' | sed -e 's/ //g' | sed -e 's/.rasi//g' )

ROFICURRENT=$(basename "${ROFICURRENT}")

TOGGLECURRENT="$(grep '//@import "/mxrm-settings/mxrm-settings.rasi"' $HOME/.config/rofi/config.rasi)"

if [ "$TOGGLECURRENT" = "" ]; then
	
	TOGGLETEXT='global'
	#TOGGLETEXTTRANS="$(gettext 'global')"
	SETTINGSLOCATION="$ROFIPATH/mxrm-settings/mxrm-settings.rasi"
	 
	else
	  
	TOGGLETEXT='specific'
	#TOGGLETEXTTRANS="$(gettext 'specific')"
	SETTINGSLOCATION="$ROFIPATH/$ROFICURRENT.rasi"
	
fi


declare -a THEMECHECK=(MX-sidebar MX-appfinder MX-run)

for i in "${THEMECHECK[@]}"

do

	if [ $i = $ROFICURRENT ]; then
	
	TOGGLETEXT='specific'
	#TOGGLETEXTTRANS="$(gettext 'specific')"
	SETTINGSLOCATION="$ROFIPATH/$ROFICURRENT.rasi"
	
	fi
	
done


NEWFONTCHECK=FALSE


if [ ! -f $ROFIPATH/$ROFICURRENT.rasi ]; then

	TEXTSORRY="$(eval_gettext 'Sorry, the appearance settings for $ROFICURRENT can only be modified using global settings.\n\nWould you like to change to global settings?.\n\n')"

	if [ "$TOGGLETEXT" = 'specific' ]; then

	yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" \
	--borders=20 --width=400 --height=200 --center --fixed --margins=20 \
	--text="$TEXTSORRY" --text-align=center --button="gtk-no" --button="gtk-yes" \

	GETOUT=$(echo "$?")

	if [ "$GETOUT" = 1 ]; then

		TOGGLETEXT='global'
		#TOGGLETEXTTRANS="$(gettext 'global')"
		SETTINGSLOCATION="$ROFIPATH/mxrm-settings/mxrm-settings.rasi"
		
		SPECIFIC='//@import "/mxrm-settings/mxrm-settings.rasi"'
		SPECIFIC=$(echo "${SPECIFIC}" | sed -e 's/[]$\/*[\^]/\\&/g' )
		GLOBAL='@import "/mxrm-settings/mxrm-settings.rasi"'
		GLOBAL=$(echo "${GLOBAL}" | sed -e 's/[]$\/*[\^]/\\&/g' )
		
		sed -i -e "s/${SPECIFIC}/${GLOBAL}/g" $HOME/.config/rofi/config.rasi
		#sed -i -e "s/\/\/@import/@import/g" $HOME/.config/rofi/config.rasi
		
		NEWFONTCHECK=TRUE

	elif [ "$GETOUT" != 1 ]; then

		exit

	fi
	fi

fi

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

ROFIFONTOLD="$(grep "font" $SETTINGSLOCATION | head -1 )"

	if [ "$ROFIFONTOLD" = "" ]; then

		ROFIFONTOLD='    font: "Noto Sans 12";'
		sed -ie "/^listview/a $ROFIFONTOLD" $SETTINGSLOCATION

	fi
	
	
FONVALUE=$(echo "${ROFIFONTOLD}" | sed -e 's/font\://g' | sed -e 's/\;//g'| sed -e 's/\"//g' )

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


FONTMAINTEXT1="$(gettext '<b>Adjust the font of the current rofi window.</b>')"
#FONTMAINTEXT2="$(eval_gettext 'The current appearance settings are <b>$TOGGLETEXTTRANS</b>\n')"
FONTMAINTEXT2="$(eval_gettext 'The current appearance settings are <b>$TOGGLETEXT</b>')"

#MAINPOS

MAINPOS=(yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --borders=20 --center --width=600 --height=400 --fixed --form --columns=1 --align=left --separator=" " --button="gtk-cancel":1 --button="gtk-ok":0 \
--text="$FONTMAINTEXT1\n\n$FONTMAINTEXT2\n\n" \

--field="$(gettext 'Font')":FN "$FONVALUE" \
)

POS=$("${MAINPOS[@]}")
	(($?==0)) && echo "$POS" > $ROFIPATH/tempdir/tempfont.txt

GETOUT=$(echo "$?")

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

	exit

elif [ "$GETOUT" = 0 ]; then


	cd $ROFIPATH/tempdir

	read -r -a NEWPOS < tempfont.txt

	FONNEW="${NEWPOS[@]}"

fi


ROFIFONTNEW="font: \""$FONNEW"\";"
ROFIFONTNEW=$(echo "${ROFIFONTNEW}" | sed -e 's/[]$\/"*[\^]/\\&/g' )

if [ "$ROFIFONTNEW" = "$ROFIFONTOLD" ]; then

	if [ "$NEWFONTCHECK" = FALSE ]; then

	TEXTFONNOCHANGE="$(eval_gettext 'No change to current settings!\n')"

	yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=400 --height=200 --fixed --center --borders=20 --text="$TEXTFONNOCHANGE" --text-align=center --button="gtk-ok" \

	exit
	
	fi

else

	sed -i -e "s/${ROFIFONTOLD}/${ROFIFONTNEW}/g" $SETTINGSLOCATION

	TEXTFONTDONE="$(eval_gettext 'All done!\nFont of the current rofi theme has been amended.')"

	yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=400 --height=200 --fixed --center --borders=20 --text="$TEXTFONTDONE" --text-align=center --button="gtk-ok" \

	exit

fi

}



###################################################################
###################################################################
# function rofi_recolor

rofi_recolor() {
	
#mxrr MX-comfort-rofi-recolor
#Tool to edit colors in MX-comfort rofi theme

#define some variables

TITLE="$(gettext 'MX-Rofi-Recolor')"
CLASS="mx-rofi-manager"
ROFIPATH="$HOME/.config/rofi/themes"
ICONPATH="/usr/share/pixmaps/mxrm-icon.svg"
source gettext.sh

#read available styles

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


#select starting style

TEXTBASE1="$(gettext '\n<b> Choose which theme to use as a starting point</b>\n')"

BASESTYLE=$( yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --text="$TEXTBASE1" --width=600 --height=400 --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

	TEXTBASE2="$(gettext 'No theme selected.\n\n<b>MX-comfort</b> will be used as the starting theme.')"

	yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --text="$TEXTBASE2" --button=gtk-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() {
	source gettext.sh
    local N=$1 C="$2"
	local YAD=(
            yad
            --title="$(gettext '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"
}


# make mxrrhelptemp
echo "$BASESTYLE" > $ROFIPATH/tempdir/mxrrhelptemp.txt


# function to be called with help button

mxrrhelp() {
TITLE="$(gettext 'MX-Rofi-Recolor Help')"
CLASS="mx-rofi-manager"
ROFIPATH="$HOME/.config/rofi/themes"
ICONPATH="/usr/share/pixmaps/mxrm-icon.svg"
source gettext.sh

read -r -a BASESTYLE < $ROFIPATH/tempdir/mxrrhelptemp.txt

	BASE="${BASESTYLE[0]}"

HELPTEMP="$(grep "mxrr" $ROFIPATH/$BASE.rasi)"
HELPTEMP=$(echo "${HELPTEMP}" | sed -e 's/\/\// /g' | sed -e 's/mxrr/ /g' | sed -e 's/ //g' )


if [ "$HELPTEMP" = template-fancy.rasi ]; then

	HELPPNG=mxrr-help-fancy.png
	
elif [ "$HELPTEMP" = template-sidebar.rasi ]; then

	HELPPNG=mxrr-help-sidebar.png
	
elif [ "$HELPTEMP" = template-appfinder.rasi ]; then

	HELPPNG=mxrr-help-appfinder.png
	
elif [ "$HELPTEMP" = template-run.rasi ]; then

	HELPPNG=mxrr-help-run.png
	
elif [ "$HELPTEMP" = template-iconic.rasi ]; then

	HELPPNG=mxrr-help-iconic.png

else

	HELPPNG="mxrr-help-comfort.png"

fi


MXRRHELPTEXT1="$(gettext 'Title Background')"
MXRRHELPTEXT2="$(gettext 'Title Text')"
MXRRHELPTEXT3="$(gettext 'Searchbar Background')"
MXRRHELPTEXT4="$(gettext 'Searchbar Text')"
MXRRHELPTEXT5="$(gettext 'Inactive Tab Background')"
MXRRHELPTEXT6="$(gettext 'Inactive Tab Text')"
MXRRHELPTEXT7="$(gettext 'Active Tab Background')"
MXRRHELPTEXT8="$(gettext 'Active Tab Text')"
MXRRHELPTEXT9="$(gettext 'Active Tab Highlight')"
MXRRHELPTEXT10="$(gettext 'Selection Background')"
MXRRHELPTEXT11="$(gettext 'Selection Text')"


yad --title="$(gettext 'MX-Rofi-Recolor Help')"  --class=mxrrhelp --window-icon="
$ICONPATH" --borders=20 --fixed --posx=10 --posy=10 --image /usr/share/pixmaps/$HELPPNG --button="$(gettext 'Return to Main Window')":1 \
--text=" 01 $MXRRHELPTEXT1 \n\n 02 $MXRRHELPTEXT2 \n\n 03 $MXRRHELPTEXT3 \n\n 04 $MXRRHELPTEXT4 \n\n 05 $MXRRHELPTEXT5 \n\n 06 $MXRRHELPTEXT6 \n\n 07 $MXRRHELPTEXT7 \n\n 08 $MXRRHELPTEXT8 \n\n 09 $MXRRHELPTEXT9 \n\n 10 $MXRRHELPTEXT10 \n\n 11 $MXRRHELPTEXT11 \n\n"

}

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-cancel":1 --button="gtk-ok":0 \
--text="$(gettext '<b>\nEdit colors and create a new rofi theme.\n\n</b>')" \
--field="$(gettext 'Title Background')":CLR "$COL01" \
--field="$(gettext 'Title Text')":CLR "$COL02" \
--field="$(gettext 'Searchbar Background')":CLR "$COL03" \
--field="$(gettext 'Searchbar Text')":CLR "$COL04" \
--field="$(gettext 'Inactive Tab Background')":CLR "$COL05" \
--field="$(gettext '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="$(gettext 'Active Tab Background')":CLR "$COL07" \
--field="$(gettext 'Active Tab Text')":CLR "$COL08" \
--field="$(gettext 'Active Tab Highlight')":CLR "$COL09" \
--field="$(gettext 'Selection Background')":CLR "$COL10" \
--field="$(gettext '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="$(gettext '<b>Name for new theme</b>     ')":LBL " " \
--field=" " ""$NEWNAME"" \
--field="$(gettext '<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="$(gettext 'No Theme name!')" --form --center --borders=20 --separator="" \
	--field="$(gettext '<b>Name for new theme</b>')":LBL " " \
	--field=" " "$(gettext '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-fancy' -o "$NEWNAME" = 'MX-sidebar' -o "$NEWNAME" = 'MX-run' -o "$NEWNAME" = 'MX-iconic' -o "$NEWNAME" = 'MX-appfinder' ]; do

	  NEWNAME=$(yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=400 --height=200 --fixed --text="$(eval_gettext 'Theme 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 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="$(eval_gettext 'A Theme with this name already exists. \n\nDo you want to overwrite <b>$NEWNAME</b>\nor save as a different theme name?\n')" --button="$(eval_gettext 'Overwrite $NEWNAME')":2 --button="$(gettext '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="$(gettext '<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

TEMPLATE="$(grep "mxrr" $BASESTYLE.rasi)"
TEMPLATE=$(echo "${TEMPLATE}" | sed -e 's/\/\// /g' | sed -e 's/mxrr/ /g' | sed -e 's/ //g' )


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


if [ "$TEMPLATE" = template-fancy.rasi ]; then

	cp $ROFIPATH/mxrm-settings/template-fancy.rasi $ROFIPATH/$NEWNAME.rasi
	
elif [ "$TEMPLATE" = template-sidebar.rasi ]; then

	cp $ROFIPATH/mxrm-settings/template-sidebar.rasi $ROFIPATH/$NEWNAME.rasi
	
elif [ "$TEMPLATE" = template-appfinder.rasi ]; then

	cp $ROFIPATH/mxrm-settings/template-appfinder.rasi $ROFIPATH/$NEWNAME.rasi

elif [ "$TEMPLATE" = template-run.rasi ]; then

	cp $ROFIPATH/mxrm-settings/template-run.rasi $ROFIPATH/$NEWNAME.rasi
	
elif [ "$TEMPLATE" = template-iconic.rasi ]; then

	cp $ROFIPATH/mxrm-settings/template-iconic.rasi $ROFIPATH/$NEWNAME.rasi

else

	cp $ROFIPATH/mxrm-settings/template-comfort.rasi $ROFIPATH/$NEWNAME.rasi

fi

#cp $ROFIPATH/$TEMPLATE $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/
	
	ROFINEW="@theme \""$ROFIPATH"/"$NEWNAME".rasi"\"
	ROFIOLD="$(grep "@theme" $HOME/.config/rofi/config.rasi | tail -1)"

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

	sed -i -e "s/${ROFIOLD}/${ROFINEW}/g" config.rasi
	
	
	TEXTALLDONE1="$(eval_gettext 'All done!\n<b>$NEWNAME</b> has been set as the current rofi theme.')"

	yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=400 --height=200 --fixed --center --borders=20 --text="$TEXTALLDONE1" --text-align=center --button="gtk-ok" \

fi


if [ "$MAKECUR" = no ]; then

	TEXTALLDONE2="$(eval_gettext 'All done!\n<b>$NEWNAME</b> Rofi theme has been created.\n\nYou can change themes with <b>MX-Rofi-Manager</b>')"

	yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --width=400 --height=200 --fixed --center --borders=20 --text="$TEXTALLDONE2" --text-align=center --button="gtk-ok" \
  
fi

exit


}


###################################################################
###################################################################
# function rofi_edit

rofi_edit() {

TITLE="$(gettext 'Rofi-Edit')"
CLASS="mx-rofi-manager"
ROFIPATH="$HOME/.config/rofi/themes"
ROFIPATHUSR="/usr/share/rofi/themes"
ICONPATH="/usr/share/pixmaps/mxrm-icon.svg"
source gettext.sh

#find editor

which mxfb-edit >/dev/null && MX_EDITOR="mxfb-edit"
[[ -z $MX_EDITOR ]] && MX_EDITOR=fpad


#read available themes

cd $ROFIPATH
ls -f *rasi > $ROFIPATH/tempdir/editlist.txt
cd $ROFIPATHUSR
ls -f *rasi >> $ROFIPATH/tempdir/editlist.txt

sed -i '/template/d' $ROFIPATH/tempdir/editlist.txt


#select theme

TEXTEDIT1="$(gettext '\n<b>Select the rofi theme you wish to edit</b>\n\nThe .rasi file will be opened in your preferred text editor\n\n')"

EDITTHEME=$( yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --text="$TEXTEDIT1" --width=600 --height=400 --borders=20 --center --list --no-headers --separator="" --column=Style --text-align=left < $ROFIPATH/tempdir/editlist.txt )

GETOUT=$(echo "$?")

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

	exit

fi

if [ ! -f $ROFIPATH/$EDITTHEME ]; then

	EDITTHEMEUSR="$ROFIPATHUSR/$EDITTHEME"

	COPYTIME=$(date +%Y"%m%d")
	
	EDITTHEME=$(echo "${EDITTHEME}" | sed -e 's/.rasi//g' )

	EDITTHEMECOPY="$EDITTHEME"_"$COPYTIME".rasi

	cp $EDITTHEMEUSR $ROFIPATH/$EDITTHEMECOPY

	TEXTEDIT2="$(eval_gettext '\nThe theme to be edited has been saved in\n<b>$ROFIPATH</b> as\n\n<b>$EDITTHEMECOPY</b>\n\n')"

	yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --text="$TEXTEDIT2" --width=600 --height=400 --borders=20 --center --text-align=center

	GETOUT=$(echo "$?")

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

		exit

		fi


	$MX_EDITOR $ROFIPATH/$EDITTHEMECOPY


else

	EDITTHEME="$ROFIPATH/$EDITTHEME"

	$MX_EDITOR $EDITTHEME

fi

exit

}


###################################################################
###################################################################
# function rofi_help

rofi_help() {

TITLE="$(gettext 'MX-Rofi-Help')"
CLASS="mx-rofi-manager"
ICONPATH="/usr/share/pixmaps/mxrm-icon.svg"

MXRMHELPTEXT="$(gettext 'MX Rofi manager provides a fast and easy way to manipulate the handy app rofi and its many possible functions ("modi"). Available actions:
--select the functions to be shown
--switch the current theme
--adjust colors, font and position (default themes only)
--edit themes manually
--access documentation
For more details, consult the rofi "help" tab.
')"


yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" \
--borders=20 --width=600 --height=500 --center --fixed  --margins=20 --text-info --wrap --show-uri <<<"$MXRMHELPTEXT" --button="gtk-close" \
	
}

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

export -f rofi_theme rofi_tabs rofi_position rofi_font rofi_recolor rofi_edit
export -f rofi_help

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

#MAIN WINDOW
TITLE="$(gettext 'MX-Rofi-Manager')"
CLASS="mx-rofi-manager"
ICONPATH="/usr/share/pixmaps/mxrm-icon.svg"

TEXTMXRM="$(gettext '<b>Rofi Manager is a tool to configure rofi themes.\n\n<i>What would you like to do?</i></b>\n')\n"
BUTTONTHEME="$(gettext 'Change the current rofi theme')"
BUTTONTABS="$(gettext 'Change the functions (<i> modi </i>) of the rofi tabs')"
BUTTONPOSITION="$(gettext 'Change the location and size of the current rofi window')"
BUTTONFONT="$(gettext 'Change the font of the current rofi window')"
BUTTONRECOLOR="$(gettext 'Recolor a rofi theme based on MX-comfort')"
BUTTONEDIT="$(gettext 'Edit a rofi theme config file in text editor')"

yad --title="$TITLE" --class="$CLASS" --window-icon="$ICONPATH" --borders=20 --center --width=600 --height=400 --fixed --form --text-align=center --text="$TEXTMXRM" --button="gtk-help":"bash -c rofi_help" --button="gtk-close" --buttons-layout=spread \
--field="$BUTTONTHEME:FBTN" 'bash -c rofi_theme' \
--field="$BUTTONTABS:FBTN" 'bash -c rofi_tabs' \
--field="$BUTTONPOSITION:FBTN" 'bash -c rofi_position' \
--field="$BUTTONFONT:FBTN" 'bash -c rofi_font' \
--field="$BUTTONRECOLOR:FBTN" 'bash -c rofi_recolor' \
--field="$BUTTONEDIT:FBTN" 'bash -c rofi_edit' \
--field=" ":LBL " "
