#!/bin/sh

##########################################################################################
##											##
## Un pequeno "script" para engadir ou eliminar de forma gráfica usuarios ao/do sistema	##
## en contornos GNU/Linux - Debian							##
##											##
## 				Forma de uso de este script				##
##				===========================				##
##											##
## Dependencias: Xdialog								##
##		 expect									##
##		 gawk									##											##
## Debe executarse con permisos de administrador (su, sudo, gksu, gksudo)		##
##											##
## Este script foi realizado polo equipo de desenvolvedores de GALPon MiniNo		##
##	<minino@galpon.org>							 	##
##											##
## Este script publicase SEN NINGUNHA GARANTIA e baixo a Licenza Pública Xeral GNU/GPL	##
## versión 2 ou superior, publicada pola Free Software Fundation.			##
##											##
##########################################################################################
##########################################################################################
##											##
## This is a little script to add or delete users in the system with a simple interface ##
## in Debian GNU/Linux environments.							##
##											##
## 				How to use this script					##
##				======================					##
##											##
## Dependences:  Xdialog								##
##		 expect									##
##		 gawk									##
##		 									##
##											##
## You have to execute it with administrator permissions (su, sudo, gksu, gksudo)	##
##											##
## Script developed by GALPon MiniNo Team <minino@galpon.org>				##
##										 	##
## This program is distributed, but WITHOUT ANY WARRANTY and following the		##
## GNU/GPL General Public License version 2 or any later version published by the	##
## Free Software Fundation.								##
##											##
##########################################################################################


export TEXTDOMAIN="minino-SSUser"
export TEXTDOMAINDIR="./locale" #"/usr/local/share/locale" 

# Ruta a iconos / Patht to icons
ICONS=./Xdialog #/usr/share/icons/Xdialog

# Comprobamos la ejecución como administrador
if [ $(whoami) != "root" ]; then
    Xdialog --title "$(gettext "WARNING")" \
	--ok-label "$(gettext "OK")" \
	--icon $ICONS/importante.xpm \
	--msgbox "$(gettext "This script requires root priviledges! Be careful!")" 0 0 0	
	exit 0
fi

# Comprobamos la existencia de expect.
if ! [ -f /usr/bin/expect ]; then
    Xdialog --title "$(gettext "WARNING")" --icon $ICONS/danger.xpm \
	--msgbox "$(gettext "«expect» not found.
	Execute as administrator (root or sudo):")
	apt-get install expect" 0 0
  	exit 0
fi

# Comprobamos la existencia de "awk" (gaw, mawk o awk-original).
if ! [ -d /usr/lib/awk ]; then
    Xdialog --title "$(gettext "WARNING")" --icon $ICONS/danger.xpm \
	--msgbox "$(gettext "«awk» not found.
	Execute as administrator (root or sudo):")
	apt-get install gawk 	$(gettext "(recommended)
	or")
	apt-get install mawk" 0 0
  	exit 0
fi

# Elegimos si añadir o eliminar usuario
MODO=`Xdialog --title $(gettext "System User Administration") --no-tags  \
	--item-help \
	--ok-label $(gettext "Aply") \
	--icon $ICONS/usuarios.xpm \
	--radiolist "$(gettext "Select Action")" 10 45 2  \
	    "ADD" "$(gettext "Add NEW user to your System.")"    on   "$(gettext "A new user will be created")"	\
	    "DEL" "$(gettext "Delete existing user in your System.")"    off  "$(gettext "A user will be deleted")"  2>&1`

case $MODO in
    ADD)
	LOGIN=`Xdialog	--stdout --separator "|" --left\
	    --icon $ICONS/usuarios.xpm --password --password\
	    --title "$(gettext "New User")" --backtitle "$(gettext "Add User")"\
	    --3inputsbox "$(gettext "System User creation or modification.\n
		With this procedure you will create a new user with its personal folder.
		This user won't have personal datas like:
		Full name
		Room number
		Work phone
		Home phone
		Other		
		If you wish to have more control to create this user, close this window
		and open a terminal. Then execute:
		         sudo adduser UserName_to_Add		
	    \nNOTE: Use Tab to move into box dialog.")" 0 0 \
		"$(gettext "Username:")" "minino" \
		"$(gettext "Enter new password:")" "" \
		"$(gettext "Retype new password:")" "" \
	    --no-close --no-cancel --title "$(gettext "Create New User") $NAME"\
	    --icon $ICONS/save-as.xpm --yesno "$(gettext "Really do you like Create this New User?")" 0 0`

	case $? in
	    0)
		NAME=`echo $LOGIN | awk --source 'BEGIN { FS="|" }' --source '{ print $1 }'`
		PASSWD=`echo $LOGIN | awk --source 'BEGIN { FS="|" }' --source '{ print $2 }'`
		REPASSWD=`echo $LOGIN | awk --source 'BEGIN { FS="|" }' --source '{ print $3 }'`

		# Comprobamos si existe el usuario en el sistema
		if [ `find /etc/group -type f | xargs grep -o $NAME:x:` ] ; then
		    `Xdialog --title "$(gettext "WARNING")" \
			--icon $ICONS/importante.xpm \
			--infobox "$(gettext "This user already exists in your system.
			\nAborting!")" 0 0 3000`
		    exit 0
		fi

		# Comprobamos la coherencia de las contraseñas
		if [ "$PASSWD" != "$REPASSWD" ]; then
		    `Xdialog --title "$(gettext "WARNING")"\
			--icon $ICONS/importante.xpm\
			--ok-label "$(gettext "Repeat")"\
			--yesno "$(gettext "Mismatch Passwords.\n\nRepeat the process.")" 0 0`
		    case $? in
			0)
			    #sudo /usr/local/bin/NewUser.sh ;;
			    sudo ./NewUser.sh ;;
			*)
			    exit ;;
			esac
		else
		    echo "$NAME -- $PASSWD -- $REPASSWD"
		    `Xdialog --title "$(gettext "New User")" --backtitle "$(gettext "Add Users")"\
			--icon $ICONS/usuarios.xpm --no-button\
			--infobox "$(gettext "Starting the process.\n\nWhait please...")"\
			0 0 1500`

#####-------- IMPORTANTE --------#####
## No tabular la sección siguiente
## sinó no trabaja bien y da un error
## de final de fichero
#####----------------------------#####

#!/usr/bin/expect
expect << EOF
spawn sudo adduser $NAME
expect "*UNIX*:*"
send "${PASSWD}\r"
expect "*UNIX*:*"
send "${REPASSWD}\r"
expect "*[]*"
send "\r"
expect "*[]*"
send "\r"
expect "*[]*"
send "\r"
expect "*[]*"
send "\r"
expect "*[]*"
send "\r"
expect "*"
send "y\r"
expect eof;
EOF
		    `Xdialog --title "$(gettext "New User")" --backtitle "$(gettext "Add Users")"\
			--icon $ICONS/usuarios.xpm --no-button\
			--infobox "$(gettext "Done!\nCreated New User")" 0 0 3000`
		fi
		;;
	    *)
		exit ;;
	esac
	;;
    DEL)
	NAME2=`Xdialog --stdout \
	    --icon $ICONS/usuarios.xpm \
	    --title "$(gettext "Delete User")" \
	    --inputbox "$(gettext "Enter Username to deleting:")" 0 0 2>&1`

	# Comprobamos si existe el usuario en el sistema
	if ! [ `find /etc/group -type f | xargs grep -o $NAME2:x:` ] ; then
	    `Xdialog --title "$(gettext "WARNING")" \
		--icon $ICONS/importante.xpm \
		--msgbox "$(gettext "This user does not exist on your system.
		\nAborting!")" 0 0 3000`
	    exit 0
	else
	    MODO2=`Xdialog --title "$(gettext "System User Administration")" --no-tags  \
		--item-help --ok-label "$(gettext "Aply")" \
		---icon $ICONS/usuarios.xpm \
    		--radiolist "$(gettext "Select Action")" 10 55 2  \
    		    "DEL" "$(gettext "Deleting ONLY user, but keeping its files.")"    on   $(gettext "User folder will not deleted")"	\
    		    "REM" "$(gettext "Deleting user & Removing its personal files.")"    off  $(gettext "User folder will be deleted")"  2>&1`

	    case $MODO2 in
		DEL)
		    `Xdialog --title "$(gettext "WARNING")" \
			--icon $ICONS/importante.xpm \
			--ok-label "$(gettext "Delete it!")" \
			--yesno "$(gettext "You will permanently delete the user") \n$NAME2\n $(gettext "of your system.
			\nare you sure?")" 0 0`
		    case $? in
			0) sudo deluser $NAME2 ;;
			*) exit ;;
		    esac
		    ;;
		REM)
		    `Xdialog --title "$(gettext "WARNING")" \
			--icon $ICONS/importante.xpm \
			--ok-label "$(gettext "Delete it!")" \
			--yesno "$(gettext "You will permanently delete the user") \n$NAME2\n $(gettext "of your system\
			and delete all data in the user folder.
			\nare you sure?")" 0 0`
		    case $? in
			0)  sudo deluser $NAME2
			    sudo rm -r /home/$NAME2
			    ;;
			*) exit ;;
		    esac
		    ;;
		*)
		    exit ;;
	    esac
	fi
	;;	
    *)
	exit ;;
esac
