#!/bin/bash
# 
# init-script to add ldap-configuration for linux clients 
# who can retrieve updates from the server
#
LDIFDIR=$(mktemp -d /var/tmp/PCTMP-XXXXXX)

# ACHTUNG!
# Das hier angegebene Skript muss unterhalb von 
# /var/SuSE/update/
# liegen:
SCRIPTNAME=update.sh
ROOMS=`ldapsearch -x "(&(Objectclass=SchoolRoom)(description=*))" description |  awk '/description:/ {print $2}'`

if [ -x $LDIFDIR ]; then
 echo "$LDIFDIR exists!"
 echo "Please rm $LDIFDIR and run me again!"
 exit 1
fi

mkdir -p $LDIFDIR

if [ "$ROOMS" == "" ]; then 
 echo "You have to define some rooms first!"
 exit 1
fi

echo "Defined rooms:"
echo $ROOMS
echo
echo -n "Please enter the Room-Name(s) or press [Enter] for all rooms: "
read room

echo -n "Please enter the Scriptname for the clients (must be beneath /var/SuSE/update) [update.sh]: " 
read scriptname

if [ "$scriptname" != "" ]; then
  SCRIPTNAME=$scriptname;
fi

echo "Using $SCRIPTNAME for Updatepath."
echo

if [ "$room" == "" ]; then
 echo "initialise all rooms ..."
 for PCDN in `ldapsearch -x "(&(objectClass=SchoolWorkstation)(sn=*Workstation-User))" dn | grep dn  | cut -d " " -f 2 | grep -v requesting`; do
# Create an tempfile for each client
/bin/cat >> $LDIFDIR/$PCDN.ldiff << EOF_LDIF
dn: $PCDN
changetype: modify
add: lientUpdatePath
clientUpdatePath: $SCRIPTNAME
-

dn: $PCDN
changetype: modify
add: clientMustUpdate
clientMustUpdate: yes

EOF_LDIF
done
else
for ROOM in $room; do
 echo "initialise $ROOM ..."
 for PCDN in `ldapsearch -x "(&(objectClass=SchoolWorkstation)(sn=$ROOM*Workstation-User))" dn | grep dn  | cut -d " " -f 2 | grep -v requesting`; do 
# Create an tempfile for each client in room
/bin/cat >> $LDIFDIR/$PCDN.ldiff << EOF_LDIF
dn: $PCDN
changetype: modify
add: clientUpdatePath
clientUpdatePath: $SCRIPTNAME
-

dn: $PCDN
changetype: modify
add: clientMustUpdate
clientMustUpdate: yes

EOF_LDIF
done
done
fi

echo
echo "You can find my results in $LDIFDIR"

echo -n "Should I update the LDAP-Database now [y/N] ? "
read LDAP

if [ x"$LDAP" = x"y" ]; then
for i in $LDIFDIR/*; do
 PASS=`/usr/sbin/get_admin_pw`
 DN=`cat /etc/openldap/slapd.conf | grep -v "#" | awk '/rootdn/ {print $2}' | cut -d "\"" -f 2`
 ldapadd -x -D $DN -w $PASS -f $i
done
fi

echo -n "Should I remove $LDIFDIR now [y/N] ? "
read answ

if [ x"$answ" = x"y" ]; then
 rm -rf $LDIFDIR
fi

echo
echo "READY"
echo

