#!/bin/sh
# monitor pin PB1 - if it goes high then initial a shutdown immediately

exit 1

WASOFF=0;
CNT=0
V=5
for (( ; ; ))
do
	VALUE=`cat /sys/class/gpio/pioB1/value `
	VALUE2=`cat /sys/class/gpio/pioB1/value `
	if [ "$VALUE" -ne "$VALUE2" ]; then
		echo "bad read"
		continue
	fi
	if [ "$VALUE" -ne "$V" ]; then
	  date
		cat /sys/class/gpio/pioB1/value
		echo "Autostop: Value = $VALUE"
		V=$VALUE
	fi	
	
	#echo "Value = $VALUE"
  if [ "$VALUE" = "1" ]; then 
		WASOFF=1
	fi
	if [ "$VALUE" = "0" ]; then 
		if [ "$WASOFF" = "1" ]; then 
		  if [ "$CNT" -le "3" ]; then
			  CNT=$((CNT+1))
#				echo "AutoStop: Line High. CNT = $CNT"
			else
#				echo "AutoStop: Immediate Shutdown Required"
			# next line is for debugging if everything is screwed up.
				#sleep 30
				date
				echo "AutoStop [debug] : Would Be Shutting Down Now"
				#/etc/init.d/clock shutdown
			fi	
		fi
	else
	  if [ "$CNT" -gt "0" ]; then
			CNT=0
#			date
			#echo "AutoStop: Line Low. CNT = $CNT"
		fi
	fi	
	sleep 1
done
