#!/usr/bin/sh

# User switching tool
# Created: 2019.02.12. 07:22
# Author: Kārlis Kalviškis <eko@lanet.lv>
# License: GPLv3


# For translation
export TEXTDOMAIN="karlo-scripts"
. gettext.sh

zinjo ()
{
	if [ -t 1 ]; then
		echo $1
	else
		zenity  --warning --text="$1"
	fi
}

dari ()
{
	case $CURRENT_DM in
		lightdm)
			dm-tool switch-to-greeter
			;;
		lxdm)
			lxdm -c USER_SWITCH
			;;
	esac
	
}

teksts="$(echo  `eval_gettext 'Error, the following command is missing:'` )"

echo 
type -P zenity &>/dev/null || { echo  "$teksts zenity"; exit 1; }

# Each Display Manager has its own switcher.
if [ "$(systemctl status display-manager | grep lightdm.service)" != "" ]; then
	type -P dm-tool &>/dev/null || { zinjo  "$(echo $teksts dm-tool)"; exit 1; }
	CURRENT_DM=lightdm
else
	if [ "$(systemctl status display-manager | grep lxdm.service)" != "" ]; then
		CURRENT_DM=lxdm
	fi
fi
if [ "$CURRENT_DM" == "" ]; then
	zinjo  "$(echo `eval_gettext 'Sorry, unsupported Display Manager.'`)"
	exit 1
fi

echo 


teksts=$(echo `eval_gettext 'Do you want to switch to another user?'`)
if [ -t 1 ]; then
	echo $teksts `eval_gettext '(Y)es or (N)o?'`
	while true; do
		read yn
		case $yn in
			[YyJj]* ) dari; break
				;;
			[Nn]* ) exit
				;;
			* ) echo `eval_gettext '(Y)es or (N)o?'`
				;;
		esac
	done
else
	zenity  --question --text="$teksts"
	if [ $? -eq 0 ] ; then
		dari
	else
		exit 0
	fi
fi
