#!/usr/bin/ksh

#------------------------------------------------------------------
#starts the batch job for just 1 directory(no final slash in path)
#syntax:  quick_start rundir (something_batch.flist)
# $1 = masterbatch
#----------------------------------------------------------------
#--------------------------------
#set environment variables
#--------------------------------
. $WFU_PATH/global.site.ksh

bgk=`echo '&'`
if (test ${#grid_engine} -eq 0) then 
	grid_engine=0
	export grid_engine
fi
if (test ${#grid_engine} -eq 1) then
	bgk=`echo `
fi
hostname=`hostname`
if test $# -eq 0
	then nextrundir=`pwd`
fi
if test $# -gt 0
	then nextrundir=$1
	error_recovery=`echo $nextrundir | grep error_recovery`
	if test ${#error_recovery} -gt 0 
		then nextrundir=`pwd`
	fi
fi
batchdir=`echo $nextrundir`
batchlog=$batchdir/batch_log
batchque=$batchdir/batch_que

#----------------------------------
#Check for an error_recovery flag
#----------------------------------
er_flag=0
error_recovery=`echo $@ | grep error_recovery=1`
if (test ${#error_recovery} -gt 0) then er_flag=1; else er_flag=0; fi


#--------------------------------------
#Get the current system time and date
#-------------------------------------
cdate=`date`
current_time=`$script_path/systime`
if test $rsh_flag -gt 0
	then cdate=`rsh $timekeeper date`
	current_time=`rsh $timekeeper $script_path/systime`
fi

#--------------------------------------
#Update the job_control file
#--------------------------------------
db=`echo '  |  ' `


#-----------------------------------------------
#Check if this batch has been completed already	
#-----------------------------------------------
bque_empty=1
batchque=$nextrundir/batch_que
qtext=`cat $batchque`
blen=${#qtext}
if test $blen -eq 0
	then echo -------------------------------------
	echo $nextrunbatchlist already completed
	echo -------------------------------------
fi
if test $blen -gt 0
	then bque_empty=0
	#---------------------------------------------------
	#kill any batch programs running on this directory
	#---------------------------------------------------
	already_running=`execute_kill_batch $nextrundir`
	#-----------------------------------------
	#give the batch programs a chance to 
	#see the kill files if there was a license
	#------------------------------------------
	let sleeptime=polltime+10
	if test $already_running -eq 1
		then sleep $sleeptime
	fi	

	#----------------------------------
	#clean the directory for new output
	#----------------------------------
	rm $nextrundir/spmbatchlicense*
	rm $nextrundir/output_*
	rm $nextrundir/running.*
	rm $nextrundir/kill*
	rm $nextrundir/waiting*
	rm $nextrundir/cpufinder_alive*
	if test $er_flag -eq 0
		then rm $nextrundir/error*
	fi
	
	#----------------------------------------------------------
	#create a new batchlicense
	#create a batchlogfile
	#Launch the cpufinder with a fake script in the background
	#----------------------------------------------------------

	licensefile=`update_batchlicense $nextrundir`
	cdate=`date`
	#--------------------------------------
	#Get the current system time and date
	#-------------------------------------
	current_time=`$script_path/systime`
	if test $rsh_flag -gt 0
		then cdate=`rsh $timekeeper date`
		current_time=`rsh $timekeeper $script_path/systime`
	fi	

	#----------------------------
	#initialize the batch_log file
	#----------------------------
	db=`echo '  |  ' `
	message1=`echo $nextrundir$db initiated on $db$hostname$db$cdate$db$db$current_time`
	batchlog=$nextrundir/batch_log
	echo $message1 > $batchlog
	echo $licensefile >> $batchlog


	#-------------------------------------------
	#T3mighty mouse error recovery
	#-------------------------------------------
	if test $T3mm -eq 1
		then cat $batchdir/T3_mm_log > $batchdir/T3_mm_log_last 2>/dev/null
		T3_mighty_mouse $batchdir >$batchdir/T3_mm_log 2>>$batchdir/T3_mm_log &	
	fi

	#----------------------------------------------------------
	#Launch the cpufinder with a fake script in the background
	#Turn off backgrounding for grid engine
	#----------------------------------------------------------
	sendscript=$nextrundir/initialisation_script.m
	echo ------------------------------------------- 
	echo Launching cpufinder with $sendscript
	echo ------------------------------------------
	cpu_finder $sendscript >$nextrundir/output_cpufinder 2>$nextrundir/output_cpufinder 



	
fi
echo ALL DONE
exit
		
