#! /bin/ksh

# This script will kill the oasis download programs in the following order:
#	getmoor (master script)
#	getm1a
#	getm1b
#	getm2
#	getoasis

# It will attempt to send a kill signal to the process a total of MAXSIGNALS
# times. If it finds a process which cannot be killed it will exit with an 
# error message.

PATH=/bin:/usr/bin:/usr/local/oasis/bin
export PATH

# This is the signal to be used in the kill statements
SIGNAL=-9

# Maximum number of times to kill a process
MAXKILLS=20

# Build the temporary file names to be used
FNAME=`mktemp -p"."`

for PROCESS in getmoor getm1a getm1b getm2 getoasis
do
	PROCKILLS=0	# number of times this process has been killed

	while true
	do
		# check to see if $PROCESS is running
		rm -f $FNAME
		ps -ef | grep $PROCESS | grep -v grep > $FNAME
		if [ ! -s $FNAME ]
		then
			rm -f $FNAME
			break
		fi

		# open the file for reading and read the process' pid
		exec 3< $FNAME
		read -u3 USER PID REMAINDER
		echo "kill_oasis: $PROCESS process running (process ID $PID)"

		# kill the process
		kill $SIGNAL $PID

		# increment the counter and compare it to the max # of kills
		let PROCKILLS=PROCKILLS+1
		if [ $PROCKILLS -gt $MAXKILLS ]
		then
			rm -f $FNAME
			echo "kill_oasis: $PROCESS can't be killed; get help!"
			exit
		fi

		# sleep a second before checking again
		sleep 1
	done
done


# Remove the temporary work file
rm -f $FNAME

# Remove the lock file
rm -f /usr/spool/uucp/LCK..tnc
