#!/bin/sh
# NAME: dasygenis_kankun_control.sh
# URL: http://arch.ece.uowm.gr/mdasyg/misc/dasygenis_kankun_control.sh
# Minas Dasygenis
# http://arch.ece.uowm.gr
#
#

version=008

#Required File Locations
contrlscr="/root/relaycontrol.sh"
watchdog="/root/watchdog.sh"
masterscript="/root/dasygenis_kankun_control.sh"


check_file ()
{
if [ $# -ne 1 ] ; then
	echo "check file function was specified without any argument"
	exit 3
fi

if [ ! -x "$1" ] ; then
	echo "Required File $1 is absent or not executable"
	exit 3
fi
}

	
	
check_file $contrlscr
check_file $watchdog
check_file $masterscript

force_state=0

# Initially the socket is powered-down state
# This helps in case the network is down and we cannot connect.
# Otherwise we start a reboot cycle every 15 minutes
sh $contrlscr $force_state > /dev/null 2>&1
(sleep 11 ;  $contrlscr  $force_state )  > /dev/null 2>&1 &


#TEMPLATE
# The serverIP
server=http://<myIP>/smartsocket.php
# The secret
psk=mypassword
# Device iname
name=home




mac=`ifconfig  wlan0 | grep HW | tr -d " " | cut -f 3 -d"r"`
ip=`ifconfig wlan0 | grep "inet " | cut -f 2 -d: | cut -f 1 -d" "`
failcounter=0
restartdone=0
countrun=0
echo "Dasygenis Web Control initiated, scriptversion=$version"
echo "Device with ip $ip and mac $mac and name $name"


#This is a never ending while loop, with 5 sec delay each iteration
while [ 1 -eq 1 ] ;
do
echo -n "."
#ip may change during DHCP, so needs updating...
ip=`ifconfig wlan0 | grep "inet " | cut -f 2 -d: | cut -f 1 -d" "`
status=`$watchdog wget -q ${server}\?psk=${psk}\&ip=${ip}\&mac=${mac}\&name=${name}\&count=${countrun}\&failcounter=${failcounter} -O-`
if [ ! -z "$status" ] ; then
	#echo $status
	if [ "$status" -eq 0 -o "$status" -eq 1 ] ; then
		if [ $status != $force_state ] ; then
			echo -n $status
		fi
		force_state=$status
	fi
fi


#Always enforce the force_state because kankun backend may change it
sh $contrlscr $force_state > /dev/null 2>&1

currentstate=`$contrlscr  state`

# always overwrite the /tmpfs file (for debugging, just change to >>)
echo "`date` Counterrun=[$countrun] FailCounter=[$failcounter] CurrentState=[$currentstate]  Forced State=[$force_state] STATUS: [$status]" > /tmp/kankun.log




ping -q -c1 ippower.vlsi.gr > /dev/null 2>&1
if [ $? -ne 0 ] ; then
    failcounter=`expr $failcounter + 1`
	echo -n "F"
else
	#we got an answer, everything is ok
	failcounter=0
	restartdone=0
fi


#ping arch or bigb6 and if no answer do a network restart
#network restart, is done only once per failure session
ping -q -c2 62.217.127.198  > /dev/null ||  ping -q -c2 62.217.127.47 > /dev/null
if [ $? -ne 0 -a $failcounter -gt 30 -a $restartdone -eq 0 ];  then
	 echo -n "N"
     /etc/init.d/network reload > /dev/null 2>&1
     /etc/init.d/network restart > /dev/null 2>&1
	 restartdone=1
fi

if [ $failcounter -eq 100 -o $failcounter -eq 200 -o $failcounter -eq 300 ] ; then
	# Lets reset the flag, in order to try to re establish connection
	restartdone=0
fi


#at least 30 minutes online (1200=360*5)
if [ "$failcounter" -gt 360 ] ; then
		echo "I Will reboot due to ping error $failcounter"
        reboot 
fi



countrun=`expr $countrun + 1`
sleep 5
done

