#!/usr/bin/ksh

#----------------------------------------------------------
#returns execution errors
#syntax:  check_errors batchdir runtime
# $1 = batch directory
# $2 = how long the cpu_finder has been running
# returns 1 if there is an error, 0 if everything is OK
#----------------------------------------------------------
#-----------------------------------------------
# set some defaults
#-----------------------------------------------
#--------------------------------
#set environment variables
#--------------------------------
. $WFU_PATH/global.site.ksh

#killhours=3
let killtime=killhours*60*60
batchdir=$1
runtime=$2
batchlog=$batchdir/batch_log
if (test ${#runtime} -eq 0) then 
	runtime=1
fi

error=0

#-----------------------
#check for a kill file
#-----------------------
killfile=`ls $batchdir | grep kill`
len=${#killfile}
if test $len -gt 0
	then error=1
	message=`echo ------ERROR KILL FILE DETECTED---------` 
fi

#-----------------------
#check for a licensefile
#-----------------------
licensefile=`ls $batchdir | grep spmbatchlicense`
len=${#licensefile}
if test $len -eq 0
	then error=1
	message=`echo ---ERROR NO SPMBATCHLICENSE DETECTED----` 
fi

#---------------------------
#check for runtime overflow
#---------------------------
if test $runtime -gt $killtime
	then error=1
	message=`echo -------ERROR CPUFINDER TIMEOUT-----------`
fi

if test $error -eq 1
	then echo $message >> $batchlog
	message1=`echo ----------SCRIPT TERMINATED--------------`
	echo $message1 >> $batchlog
	echo 1
fi

if test $error -eq 0
	then echo 0
fi
exit
