#!/bin/bash
#
# checks if the radars are giving any information
# if one of the radars is missing for 20min it will reboot.
# after 5 times reboot it will wait for ~once a day for a reboot

#empty file
/bin/echo "" > /tmp/foo

/opt/radac/bin/udpreceiver 9000 >> /tmp/foo &
/bin/sleep 5
/bin/kill -9 `/usr/bin/pgrep -f "udpreceiver 9000"`

if ((/bin/grep -q sensor0 "/tmp/foo") && (/bin/grep -q sensor1 "/tmp/foo") && (/bin/grep -q sensor2 "/tmp/foo")); then
   echo "all sensors working"
   if [ -f /tmp/sensorCheck ]; then
	rm /tmp/sensorCheck
   fi
   if [ -f /opt/radac/config/sensorCheckResetCounter ]; then
	rm /opt/radac/config/sensorCheckResetCounter
   fi
else
   if [ -f /tmp/sensorCheck ]; then
	/bin/echo "sensor not working 2nd check"
        rm /tmp/sensorCheck
   	if [ -f /opt/radac/config/sensorCheckResetCounter ]; then
		RESETCOUNTER=`/bin/cat /opt/radac/config/sensorCheckResetCounter`
		RC=`/bin/echo $RESETCOUNTER+1|/usr/bin/bc`
		/bin/echo "$RC" > /opt/radac/config/sensorCheckResetCounter
		if (( $RC < 5 )); then
        		/bin/echo "Reboot < 5 "
			/sbin/reboot
		else
			if (( $RC > 144 )); then
				#start at 6 for a once a day reboot
				/bin/echo "6" > /opt/radac/config/sensorCheckResetCounter
        			/bin/echo "Reboot > 144"
				/sbin/reboot
			fi
		fi
   	else
		/bin/echo "1" > /opt/radac/config/sensorCheckResetCounter
   	fi
   else
        /bin/echo "sensor not working 1st check"
	/bin/touch /tmp/sensorCheck
   fi	
fi
