#!/bin/bash
# UpdateCron.sh
#
ROOMS="r126 r112"
SCRIPTNAME="online_update.sh"
SUSE_VERSION="11.0"
LOGFILE="/var/log/rsync_update_$SUSE_VERSION.log"
UPDATE_PATH="/srv/tftp/update"
SCRIPTPATH="/var/SuSE/update"
RSYNC_SERVER="rsync.opensuse.org"
RSYNC_PATH="opensuse-updates/$SUSE_VERSION"
RSYNC_OPTIONS="-az --stats --delete --exclude=*.src.rpm"
PATH=/sbin:/bin:/usr/bin:/usr/sbin

if [ x"$ROOMS" = x"" ]; then
	ROOMS=`ldapsearch -x "(&(Objectclass=SchoolRoom)(description=*))" description |  awk '/description:/ {print $2}'`
fi

test -d $SCRIPTPATH || mkdir -p $SCRIPTPATH
test -d $UPDATE_PATH/$SUSE_VERSION || mkdir -p $UPDATE_PATH/$SUSE_VERSION

rsync $RSYNC_OPTIONS rsync://$RSYNC_SERVER/$RSYNC_PATH $UPDATE_PATH 1>>$LOGFILE 2>&1

echo "#!/bin/bash" > $SCRIPTPATH/$SCRIPTNAME
if [ $SUSE_VERSION <= 10.0 ]; then
    echo "/usr/bin/online_update -u ftp://install/update/" >>  $SCRIPTPATH/$SCRIPTNAME
else 
    echo "if [ -f /usr/bin/rug ]; then "  >>  $SCRIPTPATH/$SCRIPTNAME
    echo "   /usr/bin/rug update" >>  $SCRIPTPATH/$SCRIPTNAME
    echo "else" >>  $SCRIPTPATH/$SCRIPTNAME
    echo "   /usr/bin/zypper update -t patch --skip-interactive -y" >>  $SCRIPTPATH/$SCRIPTNAME
    echo "fi" >>  $SCRIPTPATH/$SCRIPTNAME
fi
chmod 755 $SCRIPTPATH/$SCRIPTNAME

for ROOM in $ROOMS; do
 UpdateClients.sh --script $SCRIPTNAME --room $ROOM
done

