#!/bin/sh
#
TZ=CUT0		#Assure logs are in Central Universal Time
export TZ
autokill=YES
PINGKYDIR=/rtdata/rover/logs/InetRover
PINGKYDIR=/usr/local/rover/data/InetRover
export PINGKYDIR
NETWORK=MBONE
NETWORKSTATUSFILE=$PINGKYDIR/$NETWORK.STATUS
COMMUNITY=public
#DODISCOVERY=DoDiscovery
pidFILE="$PINGKYDIR/$NETWORK.pid"
#
#       Assure no other Rover of this type is running in my PINGKYDIR
#
if [ -r $pidFILE ]
then
        ProcessNum=`cat $pidFILE`
        echo '*************************** ERROR ******************************'
        echo '**'" $0  already running:  Apparently Process ID="$ProcessNum
        echo '** ( pid file exists: '$pidFILE' )'
        echo '*************************** ERROR ******************************'
	if [ "$autokill" = "YES" ]
	then
		echo "auto kill is enabled - Killing old $0 process"
        	ps aux | grep -v grep | grep $ProcessNum
		kill $ProcessNum
		echo ""
		echo `date`" SUCCESS: $0 is now starting - old process was killed"
	else
        	echo "Kill This process and restart - Your request is ignored."
        	echo ""
        	exit 1
	fi
fi
echo hello
if [ -r $NETWORKSTATUSFILE ]
then
echo test1
	ls -l $NETWORKSTATUSFILE
else
echo test2 $NETWORKSTATUSFILE
	echo "" > $NETWORKSTATUSFILE
fi
echo goodbye
echo $$ > $pidFILE 	#Install a Process ID Registration File
#
#	First time through, resolve IP addresses to DNS names
#	Otherwise, we'll leave the IP Addresses as names
#
meritpoller -t $NETWORK -c $COMMUNITY -s $NETWORK.STATUS  -r

while :
do
	( meritpoller -t $NETWORK -c $COMMUNITY -s $NETWORK.STATUS 2>&1 ) >/tmp/$NETWORK.log
	( SortStatus -P MCH_ -s $NETWORK.STATUS 2>&1  ) >/dev/null # >>$PINGKYDIR/rover.log.`day -0`

	if [ -r $PINGKYDIR/$NETWORK.SLEEP ]
	then
		SLEEPTIME=`cat $PINGKYDIR/$NETWORK.SLEEP`
	fi
	if [ "$SLEEPTIME" = "" ]
	then
		SLEEPTIME=100
	fi
	sleep $SLEEPTIME
done
