

#!/bin/sh

timestamp=`date`
logfile="/tmp/pbj.log"
echo $timestamp "pbj ac detect poll v2" >> $logfile
while true
do
	sleep 30
        ac_gpio=`gpio read 8`
	if test $ac_gpio -eq 1 -a ! -e /tmp/i2con
        then
		timestamp=`date`
		echo $timestamp "ac is down (1)" >> $logfile
		t=0
		while test -e /tmp/afx.pid -a $t -le 36
		do
		        t=`echo $t 1 + n | dc`
		        sleep 5
		done
		timestamp=`date`
		echo $timestamp "killing phason processess." >> $logfile
echo -n "CURRENT_STATE_TIME: " >> $logfile
date >> $logfile
echo "**********************************" >> $logfile
echo "*                                *" >> $logfile
echo "    *  CURRENT_STATE: SHUTDOWN   *" >> $logfile
echo "*                                *" >> $logfile
echo "**********************************" >> $logfile

 		pkill -f phason.sh
 		pkill gbr3
 		/etc/init.d/postgresql stop
 		/tmp/os/pbj_db_sync
		/sbin/halt -p
        fi
done

