#!/bin/sh

#######################################################
#
#   Configure GDM and KDM to purge all of a user's
#   processes when they login or logout.
#
#   This only occurs if the key "PURGE_PROCESSES=YES"
#   is set in /etc/sysconfig/k12ltsp
#
#######################################################


# don't bother to run unless the script exists
if [ ! -x /usr/sbin/purge_user ]
then
   echo
   echo /usr/sbin/purge_user not found, aborting
   echo
   exit
fi

# set the PURGE_PROCESSES key in /etc/sysconfig/k12ltsp if it does not exist
# default to NO
if [ ! "`grep PURGE_PROCESSES /etc/sysconfig/k12ltsp 2> /dev/null`" ]
then
	( echo 
	  echo "# set PURGE_PROCESSES=YES if you want user's processes to be"
	  echo "# removed whenever they login/logout. NOTE: setting this "
	  echo "# prevents multiple logins with the same username"
	  echo "PURGE_PROCESSES=NO"
	  echo ) >> /etc/sysconfig/k12ltsp
fi

# login modifications
for FILE in /etc/gdm/PreSession/Default /etc/kde/kdm/Xstartup
do
    if [ ! "`grep ^'/usr/sbin/purge_user $USER SANE' $FILE`" ]
    then
        /usr/bin/perl -i.ltsp -p -e '
            if ($_ =~ /^$/ ) {
             print "\n# purge the users processes before login\n" unless $done;
             print "/usr/sbin/purge_user \$USER SANE\n\n" unless $done;
             $done = "yes";
            }
        ' $FILE
    fi
done

# logout modifications
for FILE in /etc/gdm/PostSession/Default /etc/kde/kdm/Xreset
do
    if [ ! "`grep ^'/usr/sbin/purge_user $USER SANE' $FILE`" ]
    then
        /usr/bin/perl -i.ltsp -p -e '
            if ($_ =~ /^$/ ) {
             print "\n# purge the users processes after logout\n" unless $done;
             print "/usr/sbin/purge_user \$USER SANE\n\n" unless $done;
             $done = "yes";
            }
        ' $FILE
    fi
done
