#!/usr/bin/ksh

#------------------------------------------------------------
#kills any batch processes running in a directory
#syntax: execute_kill_batch batchdir
#returns 0 if there was no license file
#returns 1 if something was running (ie, found a licensefile)
#------------------------------------------------------------ 
#--------------------------------
#set environment variables
#--------------------------------
. $WFU_PATH/global.site.ksh

batchdir=$1

#----------------------------
#get a current datetimestamp
#----------------------------
cdate=`date`
if test $rsh_flag -gt 0
	then cdate=`rsh $timekeeper date`
fi

#------------------------------------------------------
#First drop in some kill files
#This should kill any qued proceses
#polling however may take up to 30 seconds to see them
#-----------------------------------------------------

 echo kill_batch executed on $cdate from `hostname` > $batchdir/kill
 echo kill_batch executed on $cdate from `hostname` > $batchdir/running.kill

#----------------------------------------
#Now look for a license file
#and execute any embedded kill command
#----------------------------------------
#licensefile=`ls $batchdir/spmbatchlicense* 2>/dev/null | sed -n '1l' | tr -d '$'`
licensefile=`ls $batchdir/spmbatchlicense* 2>/dev/null | head -1 | tr -d '$'`
len=${#licensefile}
if test $len -eq 0
	then echo 0
fi
if test $len -gt 0
	then echo 1
	killcmd=`cat $licensefile | tr -d "'"`
	len2=${#killcmd}
	if test $len2 -gt 4
		then echo $killcmd >> $batchdir/output_unix
		$killcmd >> $batchdir/output_unix
	fi
fi

#-------------------------
#remove the license file
#-------------------------
rmcmd=`rm $batchdir/spmbatchlicense* 2>/dev/null`

exit


