#!/bin/csh 

#
# Written J.Lauret Sep  7 2000.
# Installed on %%DATE%% by %%USER%%.
#
# This script is an interface to the WatchFrog script and
# meant to be executed in a crontab.
#
# It checks if $PRGM really runs and if it has changed
# pid (i.e. restarted).
#
# This cannot work without a rc0.d deletion and kill of
# the WatchFrog process. Please, see /sbin/init.d/WFrog
# script for more information on how it starts/stop. Note 
# that $PIDF is assumed there as well as where this script is 
# placed.
#
# J.Lauret Sep 11 2000 : 
# Missfeature corrections :
#    -  >&dev/null required otherwise, we get the pid from the cronjob.
#    -  different way of evaluating the pid.
#
#

set PIDF = "%%RUN%%/watchfrog.pid" 
set PRGM = "%%LOC%%/WatchFrog"

set ISRUNNING=`ps aux | grep $PRGM | grep perl | awk '{print ($2)}'`

if ( "$ISRUNNING" == "") then
    echo "$0 :: Starting $PRGM"
    $PRGM &
    sleep 1
    set ISRUNNING=`ps aux | grep $PRGM | grep perl | awk '{print ($2)}'`
    if ( -e $PIDF) then
	rm -f $PIDF
    endif
    echo $ISRUNNING >$PIDF
    chmod 0600 $PIDF
else
    if ( -e $PIDF) then
	# compare pids
	set VERIFY = `cat $PIDF`
	set CHECK  = `echo "$ISRUNNING" | grep $VERIFY`

	# and verify it
	if ( "$CHECK" == "") then
	    # Pid change for sure
	    echo "$0 :: $PRGM is running under a new PID (was $VERIFY, now $ISRUNNING"
	    rm -f $PIDF
	    echo $ISRUNNING >$PIDF
	    chmod 0600 $PIDF
	endif
    else
	# pid file does not exists
	echo "$0 :: $PIDF does not exists but process has started (??)"
	echo $ISRUNNING >$PIDF
	chmod 0600 $PIDF
    endif
endif








