#!/bin/bash

##################################################################################
##										##
## 			Forma de uso de este script				##
##										##
##    Inicialmente se ejecuta sin privilegios, en su momento solicitará		##
##    permisos de root (con gksu)						##
## 										##
##    Este script fué realizado por Miguel Anxo Bouzada para el equipo		##
##    de GALPon MiniNo <minino@galpon.org> http://minino.galpon.org 		##
##										##
##    Este script se publica SIN NINGUNA GARANTIA y bajo la Licencia Pública	##
##    General GNU versión 3, publicada por la Free Software Fundation.		##
##										##
##    Depende del fichero anexo «minino-login»					##
##										##
##################################################################################

export TEXTDOMAIN="minino-login"
export TEXTDOMAINDIR="/usr/local/share/locale"

function applyAutoLogin(){

	sed -i "s+autologin-user=######+autologin-user=$newUser+" /etc/lightdm/lightdm.conf

	sleep 1
	yad --title  "$(gettext "Auto Login")" --image user_auth --button gtk-ok:0 \
		--text " $(gettext "Next time, the system will start automatically with the user:") \n\n →\t<b><i>$newUser</i></b>"

	exit
}

function applyManualLogin(){

	currentUser=`cat /etc/lightdm/lightdm.conf | grep autologin-user= | cut -c16-32`
	sed -i "s+autologin-user=$currentUser+autologin-user=######+" /etc/lightdm/lightdm.conf

	sleep 1
	yad --title  "$(gettext "Manual Login")" --image user_auth --button gtk-ok:0 \
		--text " $(gettext "Next time the system starts you will \n need the username and password.") "

	exit
}

function applyChangeUser(){

	yad --title  "$(gettext "Auto Login")" --image user_auth --button gtk-ok:0 \
	    --text " $(gettext "Next time, the system will start automatically with the user:") \n\n →\t<b><i>$newUser</i></b>"

	[ $? = 0 ] && sed -i "s+autologin-user=$currentUser+autologin-user=$newUser+" /etc/lightdm/lightdm.conf

	[ $? != 0 ] && exit
}

# # Tomamos los valores de currentUser y newUser de los parámetros de la orden

newUser="$2"
currentUser="$3"

[ "$1" == "AutoLogin" ] && applyAutoLogin

[ "$1" == "ManualLogin" ] && applyManualLogin

[ "$1" == "ChangeUser" ] && applyChangeUser

