#!/bin/bash
#
#  Pearl Add User is a simply tool which lets you add new user to the system
#  Created by Paweł "scottvandergrift" Pijanowski 2013/Oct/28
#  Last updated 2016/04/10
#  Copyright 2013, 2014, 2015, 2016 under the GNU GPL2 License

# get default's locale file
DEFLOCDIR="/usr/share/pearl/pearl-users"
if [ "`cat /etc/default/locale | grep de_DE`" != "" ]; then
. $DEFLOCDIR/de
elif [ "`cat /etc/default/locale | grep es_AR`" != "" ]; then
. $DEFLOCDIR/es
elif [ "`cat /etc/default/locale | grep it_IT`" != "" ]; then
. $DEFLOCDIR/it
elif [ "`cat /etc/default/locale | grep ja_JP`" != "" ]; then
. $DEFLOCDIR/ja
elif [ "`cat /etc/default/locale | grep pl_PL`" != "" ]; then
. $DEFLOCDIR/pl
elif [ "`cat /etc/default/locale | grep pt_BR`" != "" ]; then
. $DEFLOCDIR/pt_BR
elif [ "`cat /etc/default/locale | grep pt_PT`" != "" ]; then
. $DEFLOCDIR/pt_PT
elif [ "`cat /etc/default/locale | grep ru_RU`" != "" ]; then
. $DEFLOCDIR/ru
else
. $DEFLOCDIR/en
fi

testroot="`whoami`"

if [ "$testroot" != "root" ]; then
	/usr/bin/remsu $0 &
fi

DIALOG="`which yad` --width=450 --height=250 --window-icon=contact-new --image=contact-new"
TEXT="--text="
TITLE="--title="
ENTRY="--entry"
OKEXIT="--button=Ok:0 --button=$LOCAL5:1"
OKBUTTON="--button=Ok:0"

if [ -f /opt/pearl/pearl-xterm ]; then
. /opt/pearl/pearl-xterm
else
SPARKYXTERM="x-terminal-emulator"
fi

USER1=`$DIALOG $TEXT"$LOCAL3" $TITLE"$LOCAL4" $ENTRY $OKEXIT`

if [ "$?" != "0" ]; then
	$DIALOG $TEXT"$LOCAL6" $TITLE"$LOCAL4" $OKBUTTON
	exit 0

elif [ "`grep $USER1 /etc/passwd`" != "" ]; then
	$DIALOG $TEXT"$LOCAL7 $USER1 $LOCAL8" $TITLE"$LOCAL4" $OKBUTTON
	exit 0

else
	$SPARKYXTERM -e "adduser $USER1"
	adduser $USER1 audio
	adduser $USER1 video
	adduser $USER1 cdrom
	adduser $USER1 dialout
	adduser $USER1 floppy
	adduser $USER1 plugdev
	adduser $USER1 netdev

	$DIALOG $TEXT"$LOCAL9 $USER1 $LOCAL10" $TITLE"$LOCAL4" $OKBUTTON
	exit 0
fi
