#!/bin/sh

##########################################################################################
##											##
## Un pequeno "script" para engadir ou modificar de forma gráfica usuarios SaMBa	##
## en contornos GNU/Linux - Debian							##
##											##
## 				Forma de uso de este script				##
##				===========================				##
##											##
## Dependencias: Xdialog								##
##		 samba									##
##		 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 modify SaMBa Users with a simple interface in 	##
## Debian GNU/Linux environments.							##
##											##
## 				How to use this script					##
##				======================					##
##											##
## Dependences:  Xdialog								##
##		 samba									##
##		 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 "minino-SambaUser requires administrator 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

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

LOGIN1=`Xdialog	--stdout --separator "|" \
		--icon $ICONS/usuarios.xpm --password --password\
		--title "$(gettext "SaMBa")" --backtitle "$(gettext "SaMBa Users")"\
		--3inputsbox "$(gettext "Creation or modification of Samba users.\n			
			NOTE: Use Tab to move into box dialog.")" 0 0 \
			"$(gettext "User Name:")" "minino" \
			"$(gettext "New SMB Password:")" "" \
			"$(gettext "Retype new SMB Password:")" "" \
		--no-close --no-cancel --title "$(gettext "SaMBa")"\
		--icon $ICONS/save-as.xpm --yesno "$(gettext "Aply?")" 0 0`

ret=$?

case $ret in
	0)
		NAME=`echo $LOGIN1 | awk --source 'BEGIN { FS="|" }' --source '{ print $1 }'`
		PASSWD=`echo $LOGIN1 | awk --source 'BEGIN { FS="|" }' --source '{ print $2 }'`
		REPASSWD=`echo $LOGIN1 | 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 \
				--msgbox "$(gettext "No such user in the system.
				\nYou must add it before (as root) with the command:")
				adduser $NAME" 0 0`
			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/SambaPasswd.sh ;;
					sudo ./SambaPasswd.sh ;;
				1)
					exit ;;
				255)
					exit ;;
			esac
		else
#			echo "$NAME -- $PASSWD -- $REPASSWD"
		`Xdialog --title "$(gettext "SaMBa")" --backtitle "$(gettext "SaMBa 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 smbpasswd -a $NAME
# expect "*:*"
# send "${PASSWD}\r"
# expect "*:*"
# send "${REPASSWD}\r"
# expect eof;
# EOF

		(echo $PASSWD; echo $PASSWD)| sudo smbpasswd -s -a $NAME

		`Xdialog --title "$(gettext "SaMBa")" --backtitle "$(gettext "SaMBa Users")"\
			--icon $ICONS/usuarios.xpm --no-button\
			--infobox "$(gettext "Done!\nCreated or Modified SaMBa User")" 0 0 3000`

		fi
		;;
	1)
		exit ;;
	255)
		exit ;;
esac
