#!/bin/bash

INTERVAL=$1
sleep $INTERVAL

. /usr/share/groovix/global.conf


while groovix-no-x-activity-since-login || pgrep -f groovix-accept-use ; do
	# no activity yet OR accept use is still up, screensaver is still allowed to be running
	sleep $INTERVAL
done

if [ "$GX_SCREENSAVER_RUN_CONTINUOUSLY" = "true" ] ; then
	echo "not killing xscreensaver because GX_SCREENSAVER_RUN_CONTINUOUSLY is true"
else
	#stop screensaver from activating during the rest of this session
	killall xscreensaver
fi

# in the case of acceptable use rejection/timeout the session is getting killed, don't try to startup pactime, etc.
sleep 0.1
if ! pgrep xfce4-session ; then
        # no xfce4-session running
        exit
fi


#consider session as starting now - screensaver during user session assumes there is no real login so these things
# aren't done yet
GX_SCREENSAVER_RUN_DURING_USER_SESSION=$(groovix-get-parameter GX_SCREENSAVER_RUN_DURING_USER_SESSION)
if  [ "$GX_SCREENSAVER_RUN_DURING_USER_SESSION" = "true" ] ; then

	touch /var/lib/groovix/session-info/$USER.effective-login-time
	echo $USER > /var/lib/groovix/session-info/effective-current-user

	GX_PACREZ_ENABLE=$(groovix-get-parameter GX_PACREZ_ENABLE)
	if [ "$GX_PACREZ_ENABLE" = "true" ] ; then

	        #rotate log
	        LOG=/var/log/groovix/pacrez.log
	        mv $LOG.4 $LOG.5
	        mv $LOG.3 $LOG.4
	        mv $LOG.2 $LOG.3
	        mv $LOG.1 $LOG.2
	        mv $LOG $LOG.1

	        pacrez_daemon --user $USER >> $LOG 2>&1 &
	fi
	
	GX_PACTIME_ENABLE=$(groovix-get-parameter GX_PACTIME_ENABLE)
	if [ "$GX_PACTIME_ENABLE" = "true" ] ; then
	
	        #rotate log
	        LOG=/var/log/groovix/pactime_daemon.log
	        mv $LOG.4 $LOG.5
	        mv $LOG.3 $LOG.4
	        mv $LOG.2 $LOG.3
	        mv $LOG.1 $LOG.2
	        mv $LOG $LOG.1
	
	        pactime_daemon --user $USER >> $LOG 2>&1 &
	fi

fi


