#!/bin/sh
#
# ClientCheck - based on upchk from Alex C. de Haas <alex@uptimes.net>
# Written by Karl Kornel <kornel.1@osu.edu>
# Slightly modified by Alex de Haas <alex@uptimes.net>
#
# Version 1.0.2-ah
# Copyright 2001 by Karl Kornel
#
# This file is distributed under the GNU General Public License (GNU GPL).
# See the end of the file for more details.
# For full GPL contents, go to <http://www.gnu.org/copyleft/gpl.html>.
#
# This script, when run, checks to see if a client rogram is running.
# It first checks for the existence of a PID file in some location.
# If the PID file can't be found, it will try to start the client.
# If the PID file is found, it uses the PID to query the status of the
# client process.  If it's not there, then we try to restart it.
#
# This script can be easily set up to run through crontab.  For example,
# the following line in your crontab will execute a copy of clientchk
# every 6 hours, as root:
#
# 0	*/6	*	*	*	root	clientchk
#
# If you don't want results emailed, use this line:
#
# 0	*/6	*	*	*	root	clientchk > /dev/null
#
# This script must be customized to your client, mainly by setting specific
# variables:

# client: This is the path to the client program.
client="/usr/local/sbin/upclient"

# clientpid: This is the path to the client PID file.
clientpid="/var/run/upclient.pid"

# deletefiles: If any files must be deleted before the client can be
#  re-started, put them here.  Give absolute paths, and separate each entry
#  with a space.
deletefiles="/var/run/upclient.pid"

# chdirectroy: If we need to change directory before running the client,
#  enter it here.
chdirectory="/"

# You can leave deletefiles or chdirector empty if you don't need them.
# However, you NEED client and clientpid.

######## The script begins here.  Please don't mess with this! ########
runtimeresults="0"

# Show startup data
echo "Client check v1.0.0"
echo "By Karl Kornel <kornel.1@osu.edu>"
echo "Client=${client}"
echo "PID File=${clientpid}"
echo "Delete Files=${deletefiles}"
echo "Directory=${chdirectory}"
echo ""

# First, change to our new working directory
if test -d $directory
then
	echo "Changing directory to ${chdirectory}..."
	cd ${chdirectory}
	echo " complete."
else
	echo "No directory change."
fi

# Test for the PID file
echo "Testing for PID file..."
if `test -r $clientpid`
then
	# PID file found!
	echo " PID file found!"

	# Test for runtime status
	theactualclientpid=`cat $clientpid`
	echo "Querying runtime status (PID=${theactualclientpid})..."
	runtimeresults=`ps -x | grep -c $theactualclientpid`
	if test $runtimeresults = "2"
	then
		# Process is runnning
		echo " Client is running."
		echo "Exiting quietly...Bye!"
		exit 0
	else
		# Client is not running
		echo " Client NOT running!"
		# Run file deletion first
		if test ! "-z $deletefiles"
		then
			# Delete our delete files
			echo "Ready to delete ${deletefiles}."
			echo "Deleting files..."
			rm $deletefiles
			echo " complete."
		fi
		# Re-launch client now
		echo "Attempting to re-launch client at ${client}..."
		$client &
	fi
else
	# PID file not found
	echo " PID file not found!"
	echo "Presume that client is not running."
	# Test for delete files
	if test ! "-z $deletefiles"
	then
		echo "Ready to delete ${deletefiles}."
		echo "Deleting files..."
		rm $deletefiles
		echo " complete."
	fi
	# Re-launch client
	echo "Attempting to re-launch client at ${client}..."
	$client &
	echo " complete."		
fi

# Program ends
echo "Program ends.  Bye!"
exit 0
