#!/bin/bash

TIMEOUT=$1
MSTIMEOUT=$TIMEOUT
let MSTIMEOUT*=1000
#2 second grace period to account for any race conditions
let MSTIMEOUT+=2000

#the delay is passed in in seconds (usually from GX_LOGOUT_NOTIFY_TIME)

SEC=0

# make sure $INTERVAL*$MULTIPLIER = 1
# done to keep things as integers for bash
# but still need to keep $INTERVAL , $MULTIPLIER , and  1000/$MULTIPLIER an integer
# it gets choppy anyways - maybe due to rounding or xserver inactivity query
# looks best with 1 second
INTERVAL=1
MULTIPLIER=1

INTERVALTIMEOUT=TIMEOUT
let INTERVALTIMEOUT*=$MULTIPLIER
while [ $SEC -lt $INTERVALTIMEOUT  ] ; do
        PERCENT=$(( SEC * 100 / TIMEOUT / $MULTIPLIER ))
        echo $PERCENT
        sleep $INTERVAL
        let SEC+=1
	MSEC=$SEC
	let MSEC*=1000
	let MSEC/=$MULTIPLIER
	INACTIVITY=`groovix-xserver-inactivity-time`

	# if we've only been inactive for 2 seconds but we've been counting for 3 seconds, then 
	#  we know there has been activity and we should exit
	echo i $INACTIVITY d  $MSEC
	if [ "$INACTIVITY" -lt "$MSEC" ] ; then	
		echo 100
		break
	fi
	
done | zenity --width=400  --title "WARNING: Session Will Be Closed"  --text "WARNING: This session has been inactive and will be closed.\n\nPlease move the mouse or hit a key to extend your session.\n\n"  --progress --auto-close

#final check
echo final check

INACTIVITY=`groovix-xserver-inactivity-time`

echo inactivity is $INACTIVITY and timeout with 2 second grace period is  $MSTIMEOUT
if [ "$INACTIVITY" -gt "$MSTIMEOUT" ] ; then
	echo no xserver activity detected
	echo groovix-session-forced-exit "This session has been inactive and will be closed."
	groovix-session-forced-exit "This session has been inactive and will be closed."
else
	echo xserver activity detected
	#zenity --title "Activity Detected"  --text "Activiy detected, this session will continue.\n\n"  --progress --timeout 3 &
	zenity  --width=400  --info --title "Activity Detected"  --text "Activiy detected, this session will continue.\n\n"  --timeout 5 &
fi





