#!/bin/sh

logfile="/home/pi/pbj.log"
logsize=`du $logfile | cut -f 1`
echo "logfile size =" $logsize >> $logfile
if test $logsize -gt 5000
then
	rm $logfile
fi
timestamp=`date`
echo $timestamp "pbj ac detect enabled" >> $logfile
/usr/local/bin/pbj_ac_poll &
b=0
while test $b -eq 0
do
	gpio wfi 8 rising
	sleep 5
	timestamp=`date`
	echo $timestamp  "ac is down" >> $logfile
	ac_gpio=`gpio read 8`
        if test $ac_gpio -eq 1
	then
		b=1
	fi
done

t=0
while test -e /tmp/afx.pid -a $t -le 12
do
	t=`echo $t 1 + n | dc`
	sleep 5
done
/sbin/halt -p
