#!/bin/bash
#Shut monitor off after X seconds
#Depends: xprintidle
#It monitors idle tise with xprintidle, when the X seconds have passed, it manually turns off monitor with "xset dpms force off"

#Variables: x = Inactive seconds after the monitor will shut off.

#is = inactive seconds. Internal use
#sit = Screensaver Idle Time. Idle time to activate the screensaver
#Put this script to initialize with your session, under System -> Preferences -> StartUp Applications
#tt = total time. Screensaver idle time + gnome power manager idle time.
#Add and browse the script file.

x=1800 #Actually it will read from "Gnome Power Manager". Comment the line specified below if you want to manually set the time on this variable.
sit=5 #Actually it will read from "Gnome Power Manager". Comment the line specified below if you want to manually set the time on this variable.


while [ 1 ]
	do
	xset dpms
	if [ `xset -q | grep Monitor | awk -F " is " {'print$2'}` = "On" ];
	then
		sit=$(gconftool-2 -R /desktop/gnome/session | grep idle_delay | awk -F "= " {'print$2'}) #Comment this line if you want to manually set the time, instead of reading from "Screensaver Preferences"
		x=$(gconftool-2 -R /apps/gnome-power-manager/timeout | grep "sleep_display_ac" | awk -F "= " {'print$2'}) #Comment this line if you want to manually set the time, instead of reading from "Gnome power manager"
		tt=$((($sit * 60) + $x)) #Comment this line if you want to manually set the time, instead of reading from Gnome settings
		#echo $tt
		is=$(xprintidle)
		is=$(($is/1000))
		#echo $is
		if [ $is -ge $tt ] && [ $x -ne 0 ] && [ `ps aux | grep -e "electricsheep --root 1" | grep -v grep | wc -l` -gt 0 ];
			then
			xset dpms force off
			kill `ps aux | grep -e "electricsheep --root 1" | grep -v grep | head -n1 | awk {'print$2'}`
		fi
		sleep 60
	else
	sleep 60
	fi
done

