#!/bin/bash
#This script requires dzen2 and acpi!


ok_level=8	#8% and above: ok!
warn_level=6	#7%-6%: warn!
		#below 6%: shutdown

#parameters for dzen2
width=1000	
height=18
xgap=140
ygap=0

#you can delete this after the first successful run or just set $pretend to 0
#################################################################################
pretend=1
if [ $pretend -eq 1 ]	
then
	which "acpi" || { echo "acpi not found" ; exit ; } 
	bat_status=$(acpi | awk '{print $4}' | sed 's#%.*##')
	charging=$(acpi | awk '{print $3}' | sed 's#,.*##')
	echo "acpi output: $(acpi | sed 's#^\s*##')"
	echo ""	
	echo "Check if the following values are ok...."
	echo "Battery status: $bat_status"
	echo "Charging, Discharging or Full =    $charging"

	echo ""	
	echo "Testing dzen2, hit Enter to continue"
	read
	which dzen2 || { echo "This script requires dzen2...." ; exit ; }
	echo "You should notice a red bar ob the top of your screen"
	echo "    Hello :D     *klick*  *klick*  *klick*" |\
		dzen2 -bg red -fg white -e "entertitle=grabmouse;button1=exit"\
		-ta l -sa c -p 10 -h $height -w $width -x $xgap	-y $ygap
	echo 'Done!'
	exit;
fi
#################################################################################
	



while [ 1 ]
do
	sleep 10;
	bat_status=$(acpi | awk '{print $4}' | sed 's#%.*##')
	charging=$(acpi | awk '{print $3}' | sed 's#,.*##')

	if [ "$charging" =  "Charging" ] || [ "$bat_status" -ge "$ok_level" ]
	then
		sleep 10
		continue
	fi

	if [ "$bat_status" -ge "$warn_level" ]
	then
		echo "    SYSTEM MESSAGE: LOW BATTERY STATUS - PLUG IN YOUR BATTERY CABLE" |\
		 dzen2 -bg red -fg white -e "entertitle=grabmouse;button1=exit"\
		  -ta l -sa c -p 10 -h $height -w $width -x $xgap -y $ygap
		 continue
	fi
	echo "    SYSTEM MESSAGE: SYSTEM SHUTDOWN IN 10 SECONDS..." |\
	 dzen2 -bg red -fg white -e "entertitle=grabmouse;button1=exit"\
	  -ta l -sa c -p 10 -h $height -w $width -x $xgap -y $ygap

#the user running this script must be able to shutdown the computer
#one way to achieve this is sudo
	sudo /sbin/halt
done
		
	


