#!/bin/sh
#
#	cwtimer_mon v3.0
#	This is the startup and supervisory script for cwtimer.
#	It should be called from a cron job at regular intervals.
#
#	To add it to root's crontab, simply do, 'crontab -e'
#	at the command prompt AS USER root!  Then, put the
#	following line into it:
#
#	*/10 * * * * su - -c "/home/irlp/custom/cwtimer_mon" repeater
#
#       Save it and cwtimer will be checked every 10 minutes and auto-
#	matically (re)started if necessary!  You can optionally add
#	the following 2 lines to the /home/irlp/custom/rc.irlp file
#	to have it start an instance of cwtimer upon boot-up right
#	away:
#
#	echo -n "Enabling cwtimer... "
#	/bin/su - -c "$CUSTOM/cwtimer_mon" repeater >&/dev/null 2>&1	
#
#    v3.1  14/05/11 VK8SJ   Mod to stop restart if node disabled and shifted
#                           kill flag to $LOCAL and unhid it.
#
###########################################################################

# ensure this script is being run as the repeater user
if [ `/usr/bin/whoami` != "repeater" ] ; then
	echo "cwtimer_mon must be run as user, repeater!  Exiting..."
	exit 1
fi

# source the environment
. /home/irlp/custom/environment


# Checks to see if cwtimer is enabled or disabled.  You can easily control
# whether or not this script will launch cwtimer by the presense or absense
# of a file in /home/irlp/local called, "cwtimer" (~/local/cwtimer).  If it
# exists, then this script will kill any instances of cwtimer and NOT start
# any new instances.  This is handy for temporarily disabling the identifier
# such as via other scripts like the custom-decode script, etc.
if [ -f $LOCAL/cwtimer ] ; then
	echo "cwtimer is disabled! Re-enable it by removing the ~/local/cwtimer file."
	killall cwtimer > /dev/null 2>&1
	exit 0
fi

if [ ! -f /home/irlp/local/enable ] ; then
	echo "Node is disabled! cwtimer is now disabled too!"
	killall cwtimer >&/dev/null 2>&1
	exit 0
fi

# checks for an active cwtimer process; forks a new instance to the background
# if it died for any reason
# for systems that do NOT support, "pidof" for locating process ids
check_cwtimer=`ps -A | egrep 'cwtimer$' | awk '{$1=$1;print}' | cut -d ' ' -f1`
if [ "$check_cwtimer" = "" ]; then
	# start cwtimer and fork it into the background
	$CUSTOM/cwtimer > /dev/null 2>&1 &
	sleep .5
	# check to see if new instance was successfully started
	check_cwtimer=`ps -A | egrep 'cwtimer$' | awk '{$1=$1;print}' | cut -d ' ' -f1`
	if [ "$check_cwtimer" = "" ]; then
		echo "cwtimer could not be started!"
	else
		echo "cwtimer started!"
	fi
else
	echo "cwtimer instance found.  Looks good!"
fi

exit 0

