#!/usr/bin/ksh
#

function createldap
{
	echo "dn: uid=$Sbt,ou=people,ou=useradmin,o=domain.com"> $Workdir/add-$Sbt.ldif
	echo "givenName: $First">> $Workdir/add-$Sbt.ldif
	echo "sn: $Last">> $Workdir/add-$Sbt.ldif
	echo "loginShell: /usr/bin/sh">> $Workdir/add-$Sbt.ldif
	echo "uidNumber: $Uid">> $Workdir/add-$Sbt.ldif
	echo "gidNumber: $Gid">> $Workdir/add-$Sbt.ldif
	echo "uid: $Sbt">> $Workdir/add-$Sbt.ldif
	echo "gecos: $First $Last">> $Workdir/add-$Sbt.ldif
	echo "cn: $First $Last">> $Workdir/add-$Sbt.ldif
	echo "userPassword: $Cpw">> $Workdir/add-$Sbt.ldif
	echo "homeDirectory: /home/$Sbt">> $Workdir/add-$Sbt.ldif
	echo "objectClass: top">> $Workdir/add-$Sbt.ldif
	echo "objectClass: person">> $Workdir/add-$Sbt.ldif
	echo "objectClass: organizationalPerson">> $Workdir/add-$Sbt.ldif
	echo "objectClass: inetorgperson">> $Workdir/add-$Sbt.ldif
	echo "objectClass: posixAccount">> $Workdir/add-$Sbt.ldif
	echo "objectClass: shadowaccount">> $Workdir/add-$Sbt.ldif
	echo " "
	ldapadd -D "cn=Directory Manager" -w password1 -f $Workdir/add-$Sbt.ldif
}

function leesinvoer
{
clear
Sbt=" " 
First=" "
Last=" "
Uid=" "
Gid=" "
Userbestaat="n"

echo "\n-------------------------------------------"
echo "----    Create user in LDAP directory   ---"
echo "-------------------------------------------\n"
echo "\nEnter SBT-code: \c"
read Sbt
checkbestaat
if [ $? -eq 1 ]
then
	checkserver01
	if [ $? -eq 0 ]
	then
		echo "\nUser exists on server01"
		echo "\nCreate user and password with data from server01? <y/n> : \c"
                read Antwrd
		if [ "$Antwrd" = "y" ]
		then
			ssh server01 "grep $Sbt /etc/passwd">/tmp/${Sbt}file
                        Uid=1$(cat /tmp/${Sbt}file|awk -F\: ' { print $3 } ' )
                        Gid=110$(cat /tmp/${Sbt}file|awk -F\: ' { print $4 } ' )
                        Name=$(cat /tmp/${Sbt}file |awk -F\: ' { print $5 } ' )
                        First=$(echo $Name |awk ' { print $1 } ' )
                        Last=$(echo ${Name#* })
			P=`echo $Sbt | cut -c 1`
			ssh server01 "sudo grep u_pwd /tcb/files/auth/$P/$Sbt | cut -d= -f2 | cut -d: -f1" > /tmp/{$Sbt}paswoord
			PW=`cat /tmp/{$Sbt}paswoord`
			rm /tmp/{$Sbt}paswoord
			rm /tmp/{$Sbt}file
			Cpw=$(echo "{crypt}$PW")
		else
			vraaginvoer
		fi
	else
		vraaginvoer
	fi
else
	echo "\n--------------------------"
	echo "User already exist!"
	echo "--------------------------"
	Userbestaat="y"
fi
}

function checkbestaat
{
	echo " "
	id $Sbt 2>/dev/null
}

function vraaginvoer
{
		echo "First name: \c"
		read First
		echo "Last name : \c"
		read Last
		echo "Userid    : \c"
		read Uid
		echo "Groupid   : \c"
		read Gid
}

function tooninvoer
{
	clear
	echo "\nNew user will be created as follows:" 
	echo "--------------------------------------------"
	echo "Sbt code: " $Sbt
	echo "Name:     " $First $Last
	echo "Uid:      " $Uid
	echo "Group:    " $Gid
	echo "--------------------------------------------"
}

function checkid
{
	id -a|grep netsadm
}

function checkserver01
{
	echo "\n--------------------------------------"
	echo "----    Check Sbtcode on server01   ----"
	echo "--------------------------------------\n"
	ssh -T server01 grep $Sbt /etc/passwd
}

function passwdchange
{
	Wachtwoord=""
	while [ -z $Wachtwoord ]
	do
		echo "\n--------------------------------------"
		echo "--- modify password user $First $Last      ---"
		echo "--------------------------------------\n"
		echo "Enter user password  : \c"
		read Wachtwoord
	done
	echo "\n"
	echo "dn: uid=$Sbt,ou=people,ou=useradmin,o=domain.com">$Workdir/passwd-$Sbt.ldif
	echo "changetype: modify">>$Workdir/passwd-$Sbt.ldif
	echo "replace: userPassword">>$Workdir/passwd-$Sbt.ldif
	echo "UserPassword: $Wachtwoord">>$Workdir/passwd-$Sbt.ldif
	ldapmodify -D "cn=Directory Manager" -w password1 -f $Workdir/passwd-$Sbt.ldif
	#rm $Workdir/passwd-$Sbt.ldif
}


Workdir="/admin/tools/ldif"
Opnieuw="y"
checkid
if [ $? -eq 0 ]
then
	while [ $Opnieuw = "y" ]
	do
		leesinvoer
		if [ $Userbestaat = "n" ]
		then
			tooninvoer
			#echo "\nCreate user? <y/n> : \c"
			#read Antw
			#if [ "$Antw" = "y" ]
			#then
				createldap
				#echo "\nCreate new password? <y/n> : \c"
				#read Antw
				#if [ "$Antw" = "y" ]
				#then
				#	passwdchange
				#fi
			#fi
		fi
		echo "\nCreate another user? <y/n> : \c"
		read Opnieuw
	done
else
	echo "\n--------------------------------------"
	echo "You are not authorized for this function"
	echo "--------------------------------------\n"
fi

