# Copyright (C) 2008 José L. Redrejo Rodríguez, jredrejo at-no-spam debian.org.
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
#for this to work, previously, S20-local-app should have stored
#the logged user at /var/runt/ltsplogin$LDM_SERVER"

set -e
if [ -f /usr/share/ltsp/ltsp-common-functions ]; then
    . /usr/share/ltsp/ltsp-common-functions
else
    # backwards compatibility for older ltsp versions
    . /usr/share/ldm/ltsp-common-functions
fi

LOGIN=$(cat /var/run/ltsplogin$LDM_SERVER)

if [ -z "$LOGIN" ]; then
	exit 1
fi


rm -f /var/run/ltsplogin$LDM_SERVER

fusermount -z -u  /var/tmp/${LOGIN}||true

#kill any possible application that ltsp user might be running
PIDS=$(ps -o pid= -U ${LOGIN}||true)
for PID in $PIDS ; do
     kill -9 ${PID}     
done

userdel -f ${LOGIN}

if [ -e /var/lock/localapps ]; then
	PID=$(cat /var/lock/localapps)
	kill -9 $PID ||true
	rm -f /var/lock/localapps
fi
