#!/bin/bash

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>

# (c) Antonio Sánchez León <introlinux[fix@]gmail[fix.]com>
# (c) Miguel Anxo Bouzada <mbouzada[fix@]gmail[fix.]com>
# (c) GALPon MinNo Team <minino[fix@]galpon[fix.]org>
# july 2012

## -

##################################################################################
##										##
## 			Forma de uso de este script				##
##										##
##    Inicialmente se ejecuta sin privilegios, en su momento solicitará		##
##    permisos de root (con gksudo)						##
##										##
##    Depende del fichero anexo «libminino-login» que debe estar en /usr/lib	##
##    y de los ficheros .mo que se encuentran en la carpeta «po», que deben 	##
##    estar en /usr/share/locale/IDIOMA/LC_MESSAGES				##
##										##
##################################################################################

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

# Directorio final
DIR="/usr/local/bin"

# Ponemos a cero la marca de contraste
CONTRAST=""

#Buscamos que usuario tenemos registrado en /etc/lightdm/lightdm.conf
currentUser=`cat /etc/lightdm/lightdm.conf | grep autologin-user= | cut -c16-32`

function changeUser(){

	# Buscamos si tenemos cambiado el nombre del usuario predeterminado
    	# Si encuentra al usuario ######, quiere decir que el modo de acceso
	# es «a través de interface» ó «manual», solicitando usuario/passwd por
	# lo que indicamos que no hay ningún usuario seleccionado.
#	[ "$currentUser" == "######" ] && currentUser="$(gettext "None")" 

	newUser=$(yad --window-icon user_auth --title "$(gettext "Auto Login")" \
		--entry --entry-text "$USER" --entry-label " $(gettext "User:")" \
		--image user_auth \
		--text " $(gettext "Currently, auto login default user is:") \n →\t<b>$currentUser</b>\n\n $(gettext "Enter the <b>user name</b> that will start \n the system automatically:") ")

	[ $? != 0 ] && exit

	[ "$newUser" == "$currentUser" ] && yad --window-icon user_auth --title "$(gettext "Change User")" \
		--image user_auth --button "$(gettext "_Go Back")":2 --button gtk-cancel:252 \
		--text "$(gettext " The new user and the default user are the same. \n Go back and enter a diferent username, \n or Cancel the process.")"

	[ $? = 2 ] && changeUser
	[ $? == 252 ] && exit

	# Buscamos si existe, en /etc/group, el usuario creado
	readUser=`cat /etc/group | grep $newUser:x:10 | cut -f1,2 -d0`
	[ "$readUser" == "$newUser:x:10" ] && State="EXIST" ||  State="NOT-EXIST"

	if [ "$State" == "EXIST" ]; then
		yad --window-icon user_auth --title  "$(gettext "Auto Login")" --image user_auth \
		--button "$(gettext "Change _User")":2 --button gtk-cancel:1 --button gtk-ok:0 \
		--text " $(gettext "Are you sure? Next time, the system will NOT ask you for \n a password and will start automaticaly with the user:") \n\n →\t<b><i>$newUser</i></b>\n"
		ret=$?

		[ $ret = 2 ] && changeUser

		if [ $ret = 0 ]; then
	   	    if [ "$CONTRAST" == "CONTRAST" ]; then
			gksudo $DIR/libminino-login AutoLogin  $newUser
	    	    else
			gksudo $DIR/libminino-login ChangeUser $newUser $currentUser
	    	    fi
		else
	    	    exit
		fi

	elif [ "$State" == "NOT-EXIST" ]; then
		yad --window-icon user_auth --title  "$(gettext "Auto Login")" --image user_auth \
		--button "$(gettext "_Go Back")":2 --button gtk-cancel:1  --title  "$(gettext "Auto Login")" \
		--text " $(gettext "The user") <b><i>$newUser</i></b> $(gettext "<b>DOESN'T EXISTS</b> in this System, and it is \n <b>not posible</b> set it for auto-login. \n\n Process is ignored. \n\n You can back to choose a new user, or Cancel \n the process and create a new user and, then, \n try it again.")"

		[ $? = 2 ] && changeUser || exit
	fi
}

function changeLoginMode(){

	ACTION=$(yad --window-icon user_auth --title "$(gettext "Login")" \
		--text " $(gettext "Choose the option you want \n to configure. ") " \
		--width 340 --image user_auth --list --print-column 1 ---no-headers \
		--column control --column accion:tip --hide-column 1 \
		"auto" "$(gettext "Auto Login (without password)")" \
		"manual" "$(gettext "Manual Login (with password)")" )

		[ $? != 0 ] && exit

		if [ "$ACTION" == "auto|" ]; then
		    if [ "$currentUser" != "######" ]; then
			yad --window-icon user_auth --title  "$(gettext "Change User")" \
			    --image user_auth --button "$(gettext "Change _User")":2 --button gtk-ok:0 \
			    --text " $(gettext "The system IS ALREADY configurated to \n access automatically WITHOUT a password. ") "
			[ $? = 2 ] && changeUser || exit
		    else
			# Creo una variable de contraste para que despues de usar la
			# función changeUser nos envíe a la función libminino-login
			CONTRAST="CONTRAST"
			changeUser
		    fi
		elif [ "$ACTION" == "manual|" ]; then
		    if [ "$currentUser" == "######" ]; then
			yad --window-icon user_auth --title "$(gettext "Change User")" --image user_auth --button gtk-ok:0 \
			    --text " $(gettext "The system IS ALREADY set to access \n through an interface by entering a password.") "
			exit
		    else
			gksudo $DIR/libminino-login ManualLogin
		    fi
		else
		    exit
		fi
}

# Select Login Mode
# Select Auto Login User

OPTION=$(yad --window-icon user_auth --title "$(gettext "Login")" \
	     --text " $(gettext "Choose the option you want to configure. ") " \
	     --image user_auth --list --print-column 1 --no-headers \
	     --width 390 --column control --column accion:tip --hide-column 1 \
	     "login" "$(gettext "Choose the access mode")" \
	     "change" "$(gettext "Choose a diferent user for auto login")" )

[ $? != 0 ] && exit

[ "$OPTION" == "login|" ] && changeLoginMode

if [ "$OPTION" == "change|" ]; then 
    # Buscamos si tenemos cambiado el nombre de usuario de «auto-login»
    if [ "$currentUser" == "######" ]; then
	# Si encuentra el usuario «######» quiere decir que el modo
	# de acceso es «a través de interface» ó «manual» 
	yad --window-icon user_auth --title "$(gettext "Change User")" --image user_auth --button gtk-ok:0 \
	    --text " $(gettext "The system is not set with auto login and therefore, \n it is not possible to change the auto-login user. \n\n Change the access mode first. ") "
	exit
    else
	changeUser
    fi
else
    exit
fi
