#!/bin/bash
#
# Author D.M-Wilhelm aka Leiche <kellerleicheorig@aol.com> at Nov 20 2018	
# Website <kellerleiche.bplaced.net> 										
# Designed for PCLinuxOS'18													
# Release 2018 																
# License GPLv3	
# Spell check for description by tbschommer
# First tester tbschommer
#
Encoding=UTF-8
#--------------------------------	 i18n - Internationalization -------------------------
export TEXTDOMAIN=sddm-gtk
export TEXTDOMAINDIR="/usr/share/locale"
#-------------------------------- variables ----------------------------------------------
NAME=sddm-gtk
WNAME=$"Configure SDDM"
VERSION=23.08.30
#--------------------------------  check status of installation --------------------------
if test -d /union ; then
export INFO_DIALOG='
<window title="'$WNAME'" icon-name="sddm-gtk">
	<vbox>
		<hbox border-width="30">
			<text use-markup="True">
				<label>"'$"<b>Please run this program after installation!</b> "'"</label>
			</text>
		</hbox>
		<hseparator></hseparator>
		<vbox>
			<button ok></button>
		</vbox>
	</vbox>
</window>
'
gtkdialog --program=INFO_DIALOG 
exit 0
fi
#-------------------------------- 	check of required tools ------------------------------
if [ ! -f /usr/bin/gtkdialog ]; then 
	echo sddm-gtk: $"gtkdialog is not installed, use synaptic to install gtkdialog!"
	exit 1
fi

if [ ! -f /usr/bin/sddm ]; then
	echo sddm-gtk: $"sddm is not installed, and sddm-gtk can not work, exit!"
	exit 0
fi
#--------------------------------   only root can do this --------------------------------
script_dir="$( cd "$( dirname "$0" )" && pwd )"
script="`basename $0`"
script_path_n_name="${script_dir}/${script}"
GUI_SU="pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY"
if [ "$UID" != "0" ]; then
$GUI_SU bash $script_path_n_name; exit;
fi
#------------------------------- fix conflict with kde -----------------------------------
if [ -f /etc/sddm.conf.d/kde_settings.conf ]; then
cp /usr/share/sddm-gtk/sddm.conf /etc/sddm.conf 
rm -r /etc/sddm.conf.d/kde_settings.conf
echo sddm-gtk: "changed to default sddm.conf"
fi
#-------------------------------- 	create tmp directory ---------------------------------
if [ ! -d /tmp/sddmconfig ]; then
#--------------------------------	 create the tmp directory ----------------------------
	mkdir /tmp/sddmconfig 
fi 
#--------------------------------	 End create tmp directory ----------------------------
#
#--------------------------------	 define the variable ---------------------------------
TMP_DIR=/tmp/sddmconfig
# copy the conf file to your tmp directory to edit the settings. 
cp /etc/sddm.conf $TMP_DIR/sddm.conf	
# GUI
#--------------------------------	End define the variable ------------------------------
#
#################################	 read current settings ###############################
#
#--------------------------------	 Autologin -------------------------------------------
# Relogin
	if [ `cat /etc/sddm.conf | grep Relogin | cut -d "=" -f2` == "true" ]; then
		RLGN_STS=true
		else
		RLGN_STS=false
	fi
	# User
	if [ `cat /etc/sddm.conf | grep  -w User | cut -d "=" -f2` ]; then
		SR_STS=true
		SLCT_STS=`cat /etc/sddm.conf | grep -w User | cut -d "=" -f2`
		else
		SR_STS=false
		SLCT_STS=`cat /etc/sddm.conf | grep -w User | cut -d "=" -f2`
	fi
	if  [ `cat /etc/passwd | grep "/bin/bash" |grep "[5-9][0-9][0-9]" | cut -d: -f1` ];then
		SRPN=$(cat /etc/passwd | grep "/bin/bash" |grep "[5-9][0-9][0-9]" | cut -d: -f1 |\
		 while read programm; do echo "<item>$programm</item>"; done)
		else
		SRPN=$(cat /etc/passwd | grep "/bin/bash" |grep "[1-5][0-9][0-9][0-9]" | cut -d: -f1 |\
		 while read programm; do echo "<item>$programm</item>"; done)
	fi	
	# Session
	if [ `cat /etc/sddm.conf | grep  -w Session | cut -d "=" -f2` ]; then
		SSN_STS=true
		SSLCT_STS=`cat /etc/sddm.conf | grep -w Session | cut -d "=" -f2`
		else
		SSN_STS=false
		SSLCT_STS=`cat /etc/sddm.conf | grep -w Session | cut -d "=" -f2`
	fi
	SLCTPN=$(ls /etc/X11/dm/Sessions | while read programm; do echo "<item>$programm</item>"; done)
	ls /etc/X11/dm/Sessions > $TMP_DIR/sessions

#--------------------------------	 End Autologin ---------------------------------------
#
#--------------------------------	 General ---------------------------------------------
	
	if [ `cat /etc/sddm.conf | grep Numlock | cut -d "=" -f2` == "none" ]; then
		NMLCK_STS=none
		elif [ `cat /etc/sddm.conf | grep Numlock | cut -d "=" -f2` == "on" ]; then
		NMLCK_STS=on
		else
		NMLCK_STS=off
	fi

#--------------------------------	 End General ----------------------------------------- 
#
#--------------------------------	 Users -----------------------------------------------
# Remember Last Session
if [ `cat /etc/sddm.conf | grep RememberLastSession | cut -d "=" -f2` == "true" ]; then
	RMLSSSN_STS=true
	else
	RMLSSSN_STS=false
fi
# Remember Last User
if [ `cat /etc/sddm.conf | grep RememberLastUser | cut -d "=" -f2` == "true" ]; then
	RMLSR_STS=true
	else
	RMLSR_STS=false
fi
#--------------------------------	 End User --------------------------------------------
#
#--------------------------------	 Theme -----------------------------------------------
					
	if [ `cat /etc/sddm.conf | grep EnableAvatars | cut -d "=" -f2` == "true" ]; then
		VTRS_STS=true
		else
		VTRS_STS=false
	fi	
	FCSDR_STS=`cat /etc/sddm.conf | grep FacesDir | cut -d "=" -f2`
	THMDR_STS=`cat /etc/sddm.conf | grep ThemeDir | cut -d "=" -f2`
	THMNDR_STS=`cat /etc/sddm.conf | grep Current | cut -d "=" -f2`
	THMNDRPN=$(ls `cat /etc/sddm.conf | grep ThemeDir | cut -d "=" -f2` |\
			while read programm; do echo "<item>$programm</item>"; done)
			
	CRST_STS=`cat /etc/sddm.conf | grep CursorTheme | cut -d "=" -f2`
	CRSTVB=$(ls -d /usr/share/icons/*/cursors | cut -d/ -f5 |\
			while read programm; do echo "<item>$programm</item>"; done)		
#--------------------------------	 End Theme -------------------------------------------
#
#--------------------------------	 End read the current settings -----------------------
#
#------------------------------- main dialog ---------------------------------------------
# about display
export ABOUT='
<window window_position="1" icon-name="sddm-gtk" width-request="600" height-request="400" title=" '$WNAME' - '$VERSION' ">
	<vbox>
		<notebook labels="'$"About|History"'" scrollable="true" width-request="500" height-request="400">
			<vbox scrollable="true" width-request="500" height-request="400">
			<text use-markup="True">
				<label>"'$"Configure SDDM

is a simple gui to manage your sddm.conf file. Choose 
your own settings for sddm.

Since sddm Version 0.19.0 is a install conflict with sddm_kcm.


 Designed for PCLinuxOS on 2018.12.01
 Author:	Daniel Meiß-Wilhelm
 Website: 	www.pclinuxos.com/forum
 Release: 	2018-2023
 License: 	GPL3 
 
"'"</label>
			</text>
			</vbox>
			<vbox scrollable="true" width-request="500" height-request="400">
			<text>
				<input file>/usr/share/doc/sddm-gtk/Changelog</input>
			</text>	
			</vbox>
		</notebook>		
		<hbox>
			<button>
				<label>'$"Close"'</label>
				<input file stock="gtk-ok"></input>
				<variable>ABOUT</variable>
				<action type="closewindow">ABOUT</action>
			</button>
		</hbox>
	</vbox>
</window>
'
export INFO='
<window window_position="1" icon-name="sddm-gtk" width-request="600"  title=" '$WNAME' - '$VERSION' ">
	<vbox wrap-mode="1">
		<frame>
			<text use-markup="true" xalign="0" wrap-mode="0">
				<label>'$"All settings are done... Program will now closed."'</label>
			</text>
		</frame>	
		<vbox><hbox>  
			<button>
				<label>OK</label>
				<variable>INFO</variable>
				<action type="closewindow">CHANGE</action>
				<action type="closewindow">INFO</action>
			</button>
		</hbox></vbox>
	</vbox>		
</window>
'

export KILL='
<window type-hint="6" window-position="2" width-request="600"  title=" '$WNAME' - '$VERSION' ">
	<vbox wrap-mode="1">
		<frame>
			<text use-markup="true" xalign="0" wrap-mode="0">
				<label>'$"Exit preview..."'</label>
			</text>
		</frame>	
		<vbox><hbox>  
			<button>
				<label>OK</label>
				<variable>KILL1</variable>
				<action>killall sddm-greeter</action>
				<action type="closewindow">KILL1</action>
			</button>
		</hbox></vbox>
	</vbox>		
</window>
'

function do_settings () {
	export CHANGE='
<window title=" '$WNAME' - '$VERSION' " window_position="1" resizable="false" icon-name="sddm-gtk">
	<vbox>
		<frame '$"Log"'>
			<vbox scrollable="true" width="600" height="300">
				<text><input file>'$TMP_DIR'/sddm.log</input></text> 
				<variable>CHANGE</variable>
			</vbox>
		</frame>
		<hbox>
			<button>
				<label>'$"Run"'</label>
				<action>if [ ! -f /etc/sddm.conf.bak ]; then mv /etc/sddm.conf /etc/sddm.conf.bak ; fi ; cp '$TMP_DIR'/sddm.conf /etc/sddm.conf ; chown -R root:root /etc/sddm.conf</action>
				<action type="launch">INFO</action>
			</button>
			<button>
				<label>'$"Exit"'</label>
				<action type="closewindow">CHANGE</action>
			</button>
		</hbox>
	</vbox>
</window>
'
export GUI="`echo "$CHANGE" | sed -e 's/##.*//'`" 
gtkdialog -p GUI
}
{
export MAIN='
<window title=" '$WNAME' - '$VERSION' " window_position="1" resizable="false" icon-name="sddm-gtk" sensitive="true">
	<hbox>
	   
	   <vbox>
	   
		<frame '$"Autologin"'>
			<vbox tooltip-text="'$"Name of the user to automatically log in when 
the system starts first time.  Default value is empty."'">
				<checkbox active="'$SR_STS'">
					<label>"'$"Enable User"'"</label>
					<variable>USER</variable>
					<action>if true enable:USLCT</action>
					<action>if false disable:USLCT</action>
					<action type="refresh">USLCT</action>
				</checkbox>
				<comboboxentry width-request="60" case-sensitive="false" value-in-list="false" active="0">
					<visible>disabled</visible>
					<variable>USLCT</variable>
					<item>'$SLCT_STS' </item>
					'$SRPN' 
				</comboboxentry>
			</vbox>
			<vbox tooltip-text="'$"Name of the session to automatically log in when 
the system starts first time.  Default value is empty."'">
				<checkbox active="'$SSN_STS'">
					<label>"'$"Enable Session"'"</label>
					<variable>SESSION</variable>
					<action>if true enable:SLCT</action>
					<action>if false disable:SLCT</action>
					<action type="refresh">SLCT</action>
				</checkbox>
				<comboboxentry width-request="60" case-sensitive="false" value-in-list="false" active="0">
					<visible>disabled</visible>
					<variable>SLCT</variable>
					<item>'$SSLCT_STS' </item>
					'$SLCTPN' 
				</comboboxentry>
			</vbox>
			<vbox tooltip-text="'$"If true and User and Session are set automatic 
login will kick in again on session exit, otherwise 
it will work only the first time.  
Default value is false."'">
				<checkbox active="'$RLGN_STS'">
					<label>"'$"Enable Relogin"'"</label>
					<variable>RLGN</variable>
				</checkbox>
			</vbox>
		</frame>
	
		<frame '$"Users"'>
		<vbox tooltip-text="'$"If this flag is true, LastSession value 
will updated on every successful login, 
if false last session value won't  be  updated. 
Default value is empty."'">
				<checkbox active="'$RMLSSSN_STS'">
					<label>"'$"Enable Remember Last Session"'"</label>
					<variable>RMLSSSN</variable>
				</checkbox>
			</vbox>
			<vbox tooltip-text="'$"If  this  flag  is  true,  LastUser  value  
will  updated on every successful login, 
if false last user value won't be updated.
Default value is empty."'">
				<checkbox active="'$RMLSR_STS'">
					<label>"'$"Enable Remember Last User"'"</label>
					<variable>RMLSR</variable>
				</checkbox>
			</vbox>
		</frame>
		
		</vbox>
		
		<vbox>
		<frame '$"General"'>
			<vbox tooltip-text="'$"Change numlock state when sddm-greeter starts. 
Valid values are on, off or none.  If property is set to none, numlock won't be 
changed.  Default value is none "'">
				<text><label>"'$"Numlock is: "'"</label></text>
					<comboboxentry width-request="60" case-sensitive="false" value-in-list="false">
						<variable>NMLCK</variable>
						<default>'$NMLCK_STS'</default>
						<item>none</item>
						<item>on</item>
						<item>off</item>
					</comboboxentry>
			</vbox>
			<vbox tooltip-text="'$"HaltCommand and RebootCommand should be not changed, 
and is here only displayed!"'">
			<text><label>"'$"Shutdown Command"'"</label></text>
				<entry>
					<input>cat /etc/sddm.conf | grep HaltCommand | cut -d "=" -f2</input>
				</entry>
			<text><label>"'$"Reboot Command"'"</label></text>
				<entry>
					<input>cat /etc/sddm.conf | grep RebootCommand | cut -d "=" -f2</input>
				</entry>
			</vbox>	
		</frame>	
		</vbox>
		
		<frame '$"Theme"'>
		<vbox tooltip-text="'$"When enabled, home directories are searched for 
.face.icon images to display as their avatars. 
This can be slow on some file systems. When 
disabled, all avatars will be default. Themes 
may choose to hide them altogether. 
Default value is true."'">		
			<checkbox active="'$VTRS_STS'">
					<label>"'$"Enable Avatars"'"</label>
					<variable>VTRS</variable>
				</checkbox>
		</vbox>
		<vbox tooltip-text="'$"Name of the current theme. 
By default this setting is empty, meaning the 
embedded theme will be used."'">
			<text><label>"'$"Current"'"</label></text>
				<comboboxentry width-request="60">
					<variable>THMNDRSLCT</variable>
					<default>'$THMNDR_STS'</default>
					'$THMNDRPN' 
				</comboboxentry>
		</vbox>
		<vbox tooltip-text="'$"Name of the cursor theme to be set before starting the display server."'">		
			<text><label>"'$"Cursor Theme"'"</label></text>
				<comboboxentry width-request="60">
					<variable>CRSTVBSLCT</variable>
					<default>'$CRST_STS'</default>
					'$CRSTVB' 
				</comboboxentry>
		</vbox>
		<vbox tooltip-text="'$"Path of the directory containing face files, face 
files should be in username.face.icon format. 
Default value is /usr/share/sddm/faces."'">		
			<text><label>"'$"Faces Dir"'"</label></text>
				<hbox>	
				<entry accept="directory">
					<default>'$FCSDR_STS'</default>
					<variable>FCSDR</variable>
				</entry>
				<button>
					<label>'$"Open"'</label>
					<variable>FILE_BROWSE_FILENAME</variable>
					<action type="fileselect">FCSDR</action>
				</button>
			</hbox>	
		</vbox>
		<vbox tooltip-text="'$"Path of the directory containing theme files. 
Default value is /usr/share/sddm/themes."'">
			<text><label>"'$"Theme Dir"'"</label></text>
			<hbox>	
				<entry accept="directory">
					<default>'$THMDR_STS'</default>
					<variable>THMDR</variable>
				</entry>
				<button>
					<label>'$"Open"'</label>
					<variable>FILE_BROWSE_FILENAME</variable>
					<action type="fileselect">THMDR</action>
				</button>
			</hbox>					
		</vbox>	
	</frame>
	<vbox start="true">
		<vbox tooltip-text="'$"The current settings can be tested by generating a preview using this button. Closing the window will return to the main window"'">
		<button>
			<label>"'$" Preview "'"</label>
			<input file icon="gtk-execute"></input>
			<action>sddm-greeter --test-mode --theme $THMDR/$THMNDRSLCT 2>&1 > /dev/null & </action>
			<action>sleep 3</action>
			<action type="launch">KILL</action>
		</button>
		</vbox>
		<vbox tooltip-text="'$"The modified configuration will be saved in the current directory as $TMP_DIR/sddm.conf, and a protocol of the changes made are displayed in a separate window"'">
		<button>
			<label>"'$" Save "'"</label>
			<input file icon="document-save"></input>
			<action type="exit">save</action>
		</button>
		</vbox>
		<button>
			<label>"'$" Cancel "'"</label>
			<input file icon="gtk-cancel"></input>
			<action type="exit">cancel</action>
		</button>
		<button>
			<label>'$"About"'</label>
			<input file stock="gtk-help"></input>
			<action type="launch">ABOUT</action>
		</button>
	</vbox>
</hbox>
</window>
'
}
#-------------------------- end main dialog ----------------------------------------------
{
I=$IFS; IFS=""
for STATEMENT in  $(gtkdialog --center --program=MAIN); do
	eval $STATEMENT
done
IFS=$I
	
	
# Process variables if dialog exit status == save
if [ "$EXIT" == "save" ];then

#--------------------------------	 Autologin -------------------------------------------	
# Relogin
	if [ ! $RLGN_STS == $RLGN ]; then	
	sed -i "s/Relogin=$RLGN_STS/Relogin=$RLGN/" $TMP_DIR/sddm.conf
	echo $"changed Relogin $RLGN_STS to $RLGN" > $TMP_DIR/sddm.log
	fi
# User
#	if [ $SLCT_STS == $USLCT ]; then
		if [ $USER == true ]; then
			sed -i "/RememberLast/!s/User=$SLCT_STS/User=$USLCT/" $TMP_DIR/sddm.conf
			echo $"changed User $SLCT_STS to $USLCT" >> $TMP_DIR/sddm.log
			else
			sed -i "/RememberLast/!s/User=$SLCT_STS/User=/" $TMP_DIR/sddm.conf
			echo $"changed User $SLCT_STS to " >> $TMP_DIR/sddm.log
		fi
#	fi	
# Session
#	if [ ! $SSLCT_STS == $SLCT ]; then
		if [ $SESSION == true ]; then
			sed -i "/RememberLast/!s/Session=$SSLCT_STS/Session=$SLCT/" $TMP_DIR/sddm.conf
			echo $"changed Session $SSLCT_STS to $SLCT" >> $TMP_DIR/sddm.log
			else
			sed -i "/RememberLast/!s/Session=$SSLCT_STS/Session=/" $TMP_DIR/sddm.conf
			echo $"changed Session $SSLCT_STS to " >> $TMP_DIR/sddm.log
		fi
#	fi	
#--------------------------------	 End Autologin ---------------------------------------	
#
#--------------------------------	 User ------------------------------------------------
# Remember Last Session
	if [ ! $RMLSSSN_STS == $RMLSSSN ]; then
	sed -i "s/RememberLastSession=$RMLSSSN_STS/RememberLastSession=$RMLSSSN/" $TMP_DIR/sddm.conf
	# if neither false nor true behind =
	sed -i "s/RememberLastSession=/RememberLastSession=$RMLSSSN/" $TMP_DIR/sddm.conf
	echo $"changed Remember Last Session $RMLSSSN_STS to $RMLSSSN" >> $TMP_DIR/sddm.log
	fi
# Remember Last User
	if [ ! $RMLSR_STS == $RMLSR ]; then
	sed -i "s/RememberLastUser=$RMLSR_STS/RememberLastUser=$RMLSR/" $TMP_DIR/sddm.conf
	# if neither false nor true behind =
	sed -i "s/RememberLastUser=/RememberLastUser=$RMLSR/" $TMP_DIR/sddm.conf
	echo $"changed Remember Last User $RMLSR_STS to $RMLSR" >> $TMP_DIR/sddm.log
	fi
#--------------------------------	 End User --------------------------------------------
#
#--------------------------------	 General ---------------------------------------------
	if [ ! $NMLCK_STS == $NMLCK ]; then
	sed -i "s/Numlock=$NMLCK_STS/Numlock=$NMLCK/" $TMP_DIR/sddm.conf
	echo $"changed Numlock $NMLCK_STS to $NMLCK" >> $TMP_DIR/sddm.log
	fi
#--------------------------------	 End General -----------------------------------------
#
#--------------------------------	 Theme -----------------------------------------------
	if [ ! $VTRS_STS == $VTRS ]; then
	sed -i "s/EnableAvatars=$VTRS_STS/EnableAvatars=$VTRS/" $TMP_DIR/sddm.conf
	echo $"changed Enable Avatars $VTRS_STS to $VTRS" >> $TMP_DIR/sddm.log
	fi
	if [ ! $THMNDR_STS == $THMNDRSLCT ]; then
	sed -i "s/Current=$THMNDR_STS/Current=$THMNDRSLCT/" $TMP_DIR/sddm.conf
	echo $"changed Current $THMNDR_STS to $THMNDRSLCT" >> $TMP_DIR/sddm.log
	fi
	if [ ! $FCSDR_STS == $FCSDR ]; then
	sed -i "s:FacesDir=$FCSDR_STS:FacesDir=$FCSDR:" $TMP_DIR/sddm.conf
	echo $"changed Faces Dir $FCSDR_STS to $FCSDR" >> $TMP_DIR/sddm.log
	fi
	if [ ! $THMDR_STS == $THMDR ]; then
	sed -i "s:ThemeDir=$THMDR_STS:ThemeDir=$THMDR:" $TMP_DIR/sddm.conf
	echo $"changed Theme Dir $THMDR_STS to $THMDR" >> $TMP_DIR/sddm.log
	fi
	if [ ! $CRST_STS == $CRSTVBSLCT ]; then
	sed -i "s:CursorTheme=$CRST_STS:CursorTheme=$CRSTVBSLCT:" $TMP_DIR/sddm.conf
	echo $"changed Cursor Theme $CRST_STS to $CRSTVBSLCT" >> $TMP_DIR/sddm.log
	fi
#--------------------------------  	 End Theme -------------------------------------------	
	do_settings
	else
#do nothing		
	echo sddm-gtk: $"Settings are not changed, exiting!"
fi

}
rm -r $TMP_DIR/sddm.conf > /dev/null 2>&1
rm -r $TMP_DIR/sddm.log > /dev/null 2>&1

