#!/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(){

	# Modificamos /etc/inittab
	sed -i "s+1:2345:respawn:/sbin/getty 38400 tty1+1:2345:respawn:/bin/login -f $newUser tty1 /dev/tty1 2>\&1+" /etc/inittab
	sed -i "s+#2:23:respawn:/sbin/getty 38400 tty2+2:23:respawn:/sbin/getty 38400 tty2+" /etc/inittab

	# Modificamos /etc/X11/Xwrapper.config
	sed -i "s+console+anybody+" /etc/X11/Xwrapper.config

	# El .bashrc estará ya preparado con el comando startx, pero por si acaso
	cargax=`cat /home/minino/.bashrc|grep startx`

	if [ "$cargax" == "" ];then
	    echo "" >>/home/minino/.bashrc
	    echo "#Si no están cargadas las X entonces lanza startx" >>/home/minino/.bashrc
	    echo "if [ -z \"\$(pgrep X)\" ]; then" >> /home/minino/.bashrc
	    echo "    startx -- -dpi 96 vt7 -nolisten tcp" >>/home/minino/.bashrc
	    echo "fi" >>/home/minino/.bashrc
	fi

#	# Quitamos XDM del inicio
#	update-rc.d -f xdm remove

	# Quitamos Lightdm del inicio
	update-rc.d -f lightdm remove

	sleep 5
	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(){

	# Buscamos cual es el usuario que tiene asignado el acceso automático en este momento
	currentUser=`cat /etc/inittab | grep 1:2345:respawn: | cut -f3 -d" "`

	# Modificamos /etc/inittab
	sed -i "s+1:2345:respawn:/bin/login -f $currentUser tty1 /dev/tty1 2>\&1+1:2345:respawn:/sbin/getty 38400 tty1+" /etc/inittab
	sed -i "s+2:23:respawn:/sbin/getty 38400 tty2+#2:23:respawn:/sbin/getty 38400 tty2+" /etc/inittab

	# Modificamos /etc/X11/Xwrapper.config
	sed -i "s+anybody+console+" /etc/X11/Xwrapper.config

#	# Volver a poner xdm en el inicio
#	sudo update-rc.d xdm defaults 99 01

	# Volver a poner Lightdm en el inicio
	sudo update-rc.d lightdm defaults 99 01

	sleep 5
	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(){

echo $currentUser current post
echo $newUser new post

	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+/bin/login -f $currentUser tty1+/bin/login -f $newUser tty1+" /etc/inittab

	[ $? != 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

