#!/bin/bash

#log everything
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>/var/log/groovix/reset-station.log 2>&1

. /usr/share/groovix/global.conf

#useful for debugging, disabled reset completely
if [ "$GX_DISABLE_RESET_STATION" = "true" ] ; then
	echo "WARNING: GX_DISABLE_RESET_STATION is true , NOT resetting station at all"
	exit 0
fi

#stop any things from last session from running
killall groovix-session-exec-after-sleep

#need to keep exec name < 15 characters to ensure killall always works
#groovix-reset-station-zenity --progress --no-cancel --title "System Reset in Progress" --pulsate --text "Resetting System to a Secure State" &
gx-rs-zenity  --width=400  --progress --no-cancel --title "System Reset in Progress" --pulsate --text "Resetting System to a Secure State" &


LASTUSER=$(cat /var/lib/groovix/session-info/current-user)
echo LASTUSER is $LASTUSER

#logout of pacrez and pactime
if [ "$GX_PACTIME_ENABLE" = "true" ] ; then
        #log out out of pactime
        pactime --status=logout --user $LASTUSER
fi
if [ "$GX_PACREZ_ENABLE" = "true" ] ; then
        #end the reservation NOW
        pacrez --logout --user=$LASTUSER
fi


if [ "$GX_ENABLE_MONITOR_POWER_SAVING" = "true" ] ; then
	xset +dpms
else
	xset -dpms
fi

if [ "$GX_DISABLE_PROCESS_KILLER" = "true" ] ; then
	echo "groovix-user-process-killer disabled"
else
	time groovix-user-process-killer
fi

if [ "$GX_DISABLE_SOUND_RESET" = "true" ] ; then
	echo "groovix-sound-reset disabled"
else
	time groovix-sound-reset 
fi

if [ "$GX_CANCEL_PRINT_JOBS_ON_LOGOUT" = "true" ] ; then
	time cancel -a
fi

if [ "$GX_DISABLE_USER_PROCESS_UNMOUNTER" = "true" ] ; then
	echo "groovix-user-process-unmounter disabled"
else
	time groovix-user-process-unmounter
fi


# permanently save home dir if applicable
for T in /var/lib/groovix/session-info/*.save-home ; do
        echo found save home marker $T
        SAVEUSER=$(basename $T | cut -f 1 -d.)
	echo SAVEUSER is $SAVEUSER
	
	if [ "$SAVEUSER" = "$LASTUSER" ] ; then
		echo SAVEUSER matches LASTUSER, proceeding with save
		groovix-save-home $SAVEUSER
	else
		echo SAVEUSER does NOT match LASTUSER, NOT saving home dir
	fi

done
rm -f /var/lib/groovix/session-info/*.save-home


if [ "$GX_DISABLE_FILESYSTEM_RESTORER" = "true" ] ; then
	echo "groovix-filesystem-restorer disabled"
else
	time groovix-filesystem-restorer 
fi


#get rid of old logins
for T in `cat /etc/passwd |  cut -f 1 -d: | perl -ni -e 'print if (/^u\d\d\d\d|^guest\d\d|^g\d\d/)'  `; do
	echo /usr/sbin/deluser $T;
	/usr/sbin/deluser $T;
done

#clean up group orphans
for T in `cat /etc/group |  cut -f 1 -d: | perl -ni -e 'print if (/^u\d\d\d\d|^guest\d\d|^g\d\d/)'  `; do
	echo /usr/sbin/delgroup $T;
	/usr/sbin/delgroup $T;
done


#lock down executable permissions
chmod 700 /usr/bin/alacarte


#reset state variables
#nothing is dirty anymore at this point and no one is logged in
rm -f  /var/lib/groovix/session-info/*.dirty-home
rm -f /var/lib/groovix/session-info/*.session-ended-normally
rm -f /var/lib/groovix/session-info/*.login-time
rm -f /var/lib/groovix/session-info/*.effective-login-time
rm -f /var/lib/groovix/session-info/*.time-infocache
rm -f /var/lib/groovix/session-info/*.user
rm -f /var/lib/groovix/session-info/*.display
rm -f /var/lib/groovix/session-info/current-display
rm -f /var/lib/groovix/session-info/current-user
rm -f /var/lib/groovix/session-info/current-home
rm -f /var/lib/groovix/session-info/effective-current-user
rm -f /var/lib/groovix/session-info/session-setup-started
rm -f /var/lib/groovix/session-info/session-setup-completed
rm -f /var/lib/groovix/session-info/post-accept-use

killall groovix-kill-screensaver-if-dirty
#killall groovix-reset-station-zenity
killall gx-rs-zenity

echo $0 done

