#!/bin/bash
#
# /usr/local/bin/autologin_set
# used in Absolute's adaptation of icewm
# under settings > logout type
#
# Copyright 2018  by Paul Sherman, GPL3
#
#
#find the current autologin setting
autologinfile=/etc/lxdm/lxdm.conf
defstr="#autologin=dgod"
aset=`cat $autologinfile | grep "autologin="`
if echo $aset | egrep -q '^ *#'
then
	echo "not set to autologin"
	
	yad --image dialog-question --title Autologin? --center --button=gtk-yes:0 --button=gtk-no:1 --text "\n\nAutologin is currently NOT set,\nWould you like to setup autologin?    \n\n"
	todoit=$?
	[[ $todoit -eq 1 ]] && exit 0
	if [[ $todoit -eq 0 ]]; then
		xwho=`whoami`
		astr="autologin=$xwho"
		sed -i s"/$aset/$astr/" $autologinfile
	else
		exit 0
	fi
else
	# ALREADY AUTOLOGIN
	# autologin is set... by who?
	lname=${aset#*=}
	lname=`echo $lname | xargs`
	xwho=`whoami`
	if [[ "$xwho" != "$lname" ]]
	then
		# different user set to autologin
		yad --image dialog-question --title "Autologin Currently ON" --center --button=gtk-yes:0 --button=gtk-no:1 --text "\n\nAutologin is currently ON,\nbut is set for user \"$lname\"\n\nWould you like to change that?   \n\n"
		todoit=$?
		[[ $todoit -eq 1 ]] && exit 0
		if [[ $todoit -eq 0 ]]; then
			sed -i s"/$aset/$defstr/" $autologinfile
			yad --image dialog-question --title "LEAVE OFF OR..." --center --button="Autologin for ME":0 --button="No Autologin":1 --text "\n\nAutologin is OFF,\nWould you like to turn it ON for you?   \n\n"
			todoit=$?
			[[ $todoit -eq 1 ]] && exit 0
			if [[ $todoit -eq 0 ]]; then
				astr="autologin=$xwho"
				sed -i s"/$defstr/$astr/" $autologinfile
			else
				exit 0
			fi	
		else
			exit 0
		fi
	else
		yad --image dialog-question --title "Autologin Currently ON" --center --button=gtk-yes:0 --button=gtk-no:1 --text "\n\nAutologin is currently ON, \n\nWould you like to turn it OFF?   \n\n"
		todoit=$?
		[[ $todoit -eq 1 ]] && exit 0
		if [[ $todoit -eq 0 ]]; then
			sed -i s"/$aset/$defstr/" $autologinfile
		else
			exit 0
		fi			
	fi
fi
