#!/bin/bash
##########################################################
# (C) ZE CMS, Humboldt-Universitaet zu Berlin
# Written 2014 by Daniel Rohde <d.rohde@cms.hu-berlin.de>
##########################################################
# creates: 
#   LDAP user group 
#   LDAP user entry
#   user home directory

if [ $# = 0 ] ; then
	echo "Usage: $0 user [user2 [user3 ...]]"
	exit 1
fi

. $(dirname $0)/config

uid=$UIDBASE
while [ "$(getent passwd $uid)" != "" ] ; do
	uid=$(( $uid + 1))
done

while (( "$#" )) ; do 
	user=$1
	if [ "$(getent passwd $user)" != "" ] ; then
		echo $user exists!
		shift
		continue
	fi
	
	echo -n Create ldap group $user and user $user with uid $uid ...
	
ldapadd -x -h $LDAP_HOST -D $LDAP_ADMIN -w $LDAP_ADMINPW >/dev/null <<EOF
dn: cn=$user,$LDAP_GROUPS
objectClass: posixGroup
cn: $user
gidNumber: $uid

dn: uid=$user,$LDAP_USERS
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
userPassword: $user
uid: $user
sn: $uid
cn: $user
uidNumber: $uid
gidNumber: $uid
homeDirectory: /home/$user
loginShell: /bin/false
mail: $user@webdavcgi.sf.net

EOF

	echo done
	echo -n Create home directory /home/$user ...
	sudo -A mkdir /home/$user
	sudo -A chown $uid:$uid /home/$user
	sudo -A edquota -p quotatemplate $user
	echo done

	uid=$(( $uid + 1))
	shift
done
