#! /bin/ksh

# Usage example:    multiple_whiz_run_repair xlong 10433 1000 00241 00830
#                                                    ^    ^    ^     ^
#                                                    |    |    |     |
#                                                    |    |    |     | last ini run
#                                                    |    |    | first ini run
#                                                    |    | integer sqrt(s) (GeV)
#                                                    | max cpu time in slac min
#                                    

echo $1
echo $2
echo $3
echo $4
echo $5

# typeset -Z6 -i  i
typeset  -i  i
typeset -Z4 kecm
typeset -Z3 necm
# typeset -Z2 -i  seq
typeset  -i  seq
typeset  -i  ilswi
typeset  -i  isubmit
typeset  -i  igrepnosp
typeset  -i  igrepoter
typeset  -i  igrephpss
typeset  -i  igreptim
typeset  -i  igrepxxl
typeset  -i  igrepcmp
typeset  -i  igrepnwr
typeset  -i  igrepnin
typeset  -i  igrepmnf
typeset  -i  igreptpr
typeset  -i  ibjobs
typeset  -i  igening
typeset  -i  igened
typeset  -i  igenev
typeset  -Z2 nrun
# typeset  -Z2 -i  nrunw
# typeset  -Z3 -i  nfile
# typeset  -i  nrun
typeset  -i  nfile
typeset  -i  nfilew
typeset  cnine



pwd > /var/tmp/dirsave
exec 3< /var/tmp/dirsave
read -u3
dirname=$REPLY
exec 3<&-

bsubqq=$1
kecm=$3
necm=$3

# set -xv

date > /var/tmp/datefile
sed "s/:/h/" /var/tmp/datefile > /var/tmp/datefile_temp
sed "s/:/m/" /var/tmp/datefile_temp > /var/tmp/datefile
exec 3< /var/tmp/datefile
read -u3 wkday month day time zone year
pname=$month'_'$day'_'$year'_'$time's' 
echo $pname
exec 3<&-

i=$4
while [[ i -le $5 ]]
do
    typeset -Z6 i
#    ls -l $FA/$kecm/run_output/w$i  | grep '^d' | grep run > /var/tmp/ls_l_3
#    ilswi=$?
#    echo "i,ilswi=" $i $ilswi
    ilswi=1
    if [ -e $FA/$kecm/run_output/w$i ] ; then
      ilswi=0
    fi
    if [[ ilswi -eq 0 ]]
    then
#	exec 3< /var/tmp/ls_l_3
#	while read -u3 c1 c2 c3 c4 c5 c6 c7 c8 c9
        for cc9 in $FA/$kecm/run_output/w$i/run* 
	do
	    c9=`basename $cc9`
	    cnine=$c9
	    isubmit=1
            okcmp="ok"
	    grep -q 'Event generation finished' $FA/$kecm/run_output/w$i/$c9/whizard.log
	    igrepcmp=$?
            if [ $igrepcmp -ne 0 ] ; then 
              okcmp="NG"
            fi 
	    grep -q 'ilc_fragment_print ncount=' $FA/$kecm/run_output/w$i/$c9/whizard.log
	    igrepnwr=$?
            oknwr="ok"     
	    if [ $igrepnwr -ne 0 ] ; then 
	      oknwr="NG" 
            fi 
	    grep -q 'cannot create' $FA/$kecm/run_output/w$i/$c9/whizard.errlog
	    igrephpss=$?
            okhpss="ok"
	    if [ $igrephpss -eq 0 ] ; then 
	      okhpss="NG" 
            fi 

	    grep -q 'output error' $FA/$kecm/run_output/w$i/$c9/whizard.errlog
	    igrepoter=$?
            okoter="ok"
	    if [ $igrepoter -eq 0 ] ; then 
	      okoter="NG" 
            fi 
	    grep -q 'No space left' $FA/$kecm/run_output/w$i/$c9/whizard.errlog
	    igrepnosp=$?
            oknosp="ok"
	    if [ $igrepnosp -eq 0 ] ; then 
	      oknosp="NG" 
            fi 
	    grep -q 'a9=' $FA/$kecm/run_output/w$i/$c9/whizard.log
	    igrepnin=$?
            oknin="ok"
	    if [ $igrepnin -ne 0 ] ; then 
	      oknin="NG" 
            fi 
	    grep -q 'User-defined time limit exceeded' $FA/$kecm/run_output/w$i/$c9/whizard.log
	    igreptim=$?
            oktim="ok"
	    if [ $igreptim -eq 0 ] ; then 
	      oktim="NG" 
            fi 
            grep -q 'stop' $FA/$kecm/run_output/w$i/$c9/whizard.log
            igrepstop=$?
            okstop="ok"
	    if [ $igrepstop -eq 0 ] ; then 
	      okstop="NG" 
            fi 
            grep -q 'Successfully completed.' $FA/$kecm/run_output/w$i/$c9/whizard.log
            igrepsuccess=$? 
            oksuccess="ok"
	    if [ $igrepsuccess -ne 0 ] ; then 
	      oksuccess="NG" 
            fi 
#	    echo "i c9 igrepcmp,igrepnwr,igrephpss,igrepoter,igrepnosp,igrepnin,igreptim,=" $i $c9 $igrepcmp $igrepnwr $igrephpss $igrepoter $igrepnosp $igrepnin $igreptim
            echo "i c9 genfini,ncount,hpss,outerr,nosp,nfiles,time,stop,success=" $i $c9 $okcmp $oknwr $okhpss $okoter $oknosp $oknin $oktim $okstop $oksuccess

	    if [[ igrepcmp -ne 0 || igrepnwr -ne 0 || igrepnin -ne 0 || igrephpss -eq 0 || igrepoter -eq 0 || igrepnosp -eq 0 ]]
	    then
		isubmit=0   
		nrun=${c9#run_}
		jobname=w$i'_'$nrun
		storename=$jobname'.stdhep'
		bjobs -J $jobname 2> /var/tmp/bjobs_grep > /var/tmp/bjobs_grep
		grep 'not found' /var/tmp/bjobs_grep > /var/tmp/grep_not_found
		ibjobs=$?
		echo "i c9 nrun jobname storename ibjobs=" $i $c9 $nrun $jobname $MS/$storename $ibjobs
		if [[ ibjobs -eq 0 ]]
		then
                  if [ "x$RESUBMIT" == "xyes" ] ; then
   		    mkdir $FA/$kecm/run_output/archive/w$i
		    archivedir=$FA/$kecm/run_output/archive/w$i/$c9
                    if [ -e $archivedir ] ; then
			mv -v ${archivedir} ${archivedir}-`date +%y%m%d-%H%M%S`
                    fi
   		    mkdir $archivedir
   		    mv -f $FA/$kecm/run_output/w$i/$c9/*.out $archivedir
   		    mv -f $FA/$kecm/run_output/w$i/$c9/*.log $archivedir
   		    mv -f $FA/$kecm/run_output/w$i/$c9/*.errlog $archivedir
   		    mv -f $FA/$kecm/run_output/w$i/$c9/*.dat $archivedir
   		    mv -f $FA/$kecm/run_output/w$i/$c9/whizard-plots.tex.in $archivedir
# 		    echo "mkdir and cp place,   i c9=" $i $c9
  
		    jobprefix=w$i'_'
  		    sed "s/time_limit =.*/time_limit = $2./" $FA/$kecm/run_output/w$i/$c9/cardswhiz_in > $FA/$kecm/run_output/w$i/$c9/cardswhiz_in_temp
  		    mv $FA/$kecm/run_output/w$i/$c9/cardswhiz_in_temp $FA/$kecm/run_output/w$i/$c9/cardswhiz_in
  		    sed "s/time_limit =.*/time_limit = $2./" $FA/$kecm/run_output/w$i/$c9/whizard.in > $FA/$kecm/run_output/w$i/$c9/whizard.in_temp
   		    mv $FA/$kecm/run_output/w$i/$c9/whizard.in_temp $FA/$kecm/run_output/w$i/$c9/whizard.in
		    cat $FA/$kecm/run_output/w$i/$c9/runwhiz | 
  		    sed -e "s/typeset.*/typeset -Z2 kwhiz/" \
  		        -e 's!cp \/.*!cp /scratch/$a9 $MS/kkkk/zzzz$kwhiz.stdhep\
cp -f /scratch/$a9 $FTPWHIZ2/ILCqqqq/zzzz$kwhiz.stdhep!' \
		        -e "s!zzzz!$jobprefix!" \
		        -e "s!kkkk!$kecm!" \
		        -e "s!qqqq!$necm!" > $FA/$kecm/run_output/w$i/$c9/runwhiz_temp5
  		    mv $FA/$kecm/run_output/w$i/$c9/runwhiz_temp5 $FA/$kecm/run_output/w$i/$c9/runwhiz
		    chmod a+x  $FA/$kecm/run_output/w$i/$c9/runwhiz

#  		    bsub -q $bsubqq -G lcd \
#  			-o $FA/$kecm/run_output/w$i/run_$nrun/whizard.log \
#  			-e $FA/$kecm/run_output/w$i/run_$nrun/whizard.errlog \
#  			-J w$i'_'$nrun  $FA/$kecm/run_output/w$i/run_$nrun/runwhiz
#  		    echo "bsub place, bsubqq=" $bsubqq
		    echo " bsub -q $bsubqq -o whizard.log -e whizard.errlog -J w$i'_'$nrun ./runwhiz " \
			> $FA/$kecm/run_output/w$i/run_$nrun/resub.sh
                    if [ "x$SUBCMD_FILE" == "x" ] ; then
			( cd $FA/$kecm/run_output/w$i/run_$nrun 
			  . resub.sh )
		    else
			echo " ( cd $FA/$kecm/run_output/w$i/run_$nrun && . resub.sh )" >> ${SUBCMD_FILE}
		    fi
		fi
              fi
	    fi
	done
	exec 3<&-
	    if [[ igreptim -eq 0 && isubmit -ne 0 ]] 
	    then
		c9=$cnine
		grep 'a9=' $FA/$kecm/run_output/w$i/$c9/whizard.log > /var/tmp/grep_a9
		exec 4< /var/tmp/grep_a9
		while read -u4 d1 d2
		do
		filepart=${d2%%.stdhep}
                typeset -Z3 nfile
		nfile=${filepart##wh$i.}
		done
		exec 4<&-
		echo "final filepart,nfile=" $filepart $nfile
                typeset -i nfile
		if [[ nfile -lt 5 ]]
		then
		    grep 'Generating' $FA/$kecm/run_output/w$i/$c9/whizard.log > /var/tmp/generating
		    exec 4< /var/tmp/generating
		    read -u4 g1 g2 g3 g4 g5 g6 
		    exec 4<&-
		    igening=$g3
		    grep 'ilc_fragment_print ncount=' $FA/$kecm/run_output/w$i/$c9/whizard.log > /var/tmp/generated
		    exec 4< /var/tmp/generated
		    read -u4 h1 h2 h3
		    exec 4<&-
		    igened=$h3
		    igenev=igening-igened
		    echo "igening,igened,igenev=" $igening $igened $igenev
		    nfilew=nfile+1
		    chmod u+w $FA/$kecm/whiz$i/cardswhiz_*
		    cp $FA/$kecm/whiz$i/cardswhiz_in $FA/$kecm/whiz$i/cardswhiz_in_0b
		    cp $FA/$kecm/run_output/w$i/$c9/cardswhiz_in $FA/$kecm/whiz$i/cardswhiz_in_tmp
		    sed "s/luminosity =.*/luminosity = 0/" $FA/$kecm/whiz$i/cardswhiz_in_tmp > $FA/$kecm/whiz$i/cardswhiz_in_tmp2
		    sed "s/n_events =.*/n_events = $igenev/" $FA/$kecm/whiz$i/cardswhiz_in_tmp2 > $FA/$kecm/whiz$i/cardswhiz_in_tmp3
		    grep -q 'min_file_count' $FA/$kecm/whiz$i/cardswhiz_in_tmp3
		    igrepmnf=$? 
		    if [[ igrepmnf -eq 0 ]]
		    then
		        cp $FA/$kecm/whiz$i/cardswhiz_in_tmp3 $FA/$kecm/whiz$i/cardswhiz_in_tmp4
		    else
			sed 's/simulation_input/simulation_input\
 min_file_count = 1/' $FA/$kecm/whiz$i/cardswhiz_in_tmp3 > $FA/$kecm/whiz$i/cardswhiz_in_tmp4
		    fi
		    sed "s/min_file_count =.*/min_file_count = $nfilew/" $FA/$kecm/whiz$i/cardswhiz_in_tmp4 > $FA/$kecm/whiz$i/cardswhiz_in_tmp5
 		    sed "s/time_limit =.*/time_limit = 83966./" $FA/$kecm/whiz$i/cardswhiz_in_tmp5 > $FA/$kecm/whiz$i/cardswhiz_in_tmp6
    		    cp $FA/$kecm/whiz$i/cardswhiz_in_tmp6 $FA/$kecm/whiz$i/cardswhiz_in
		    grep 'results' $FA/$kecm/whiz$i/runwhiz > /var/tmp/grep_results
		    exec 4< /var/tmp/grep_results
		    read -u4 q1 
		    exec 4<&-
		    dirpart=${q1%%results/whizard}
		    echo " bsubqq kecm dirpart i=" $bsubqq $kecm $dirpart $i

   		    whiz_run xxl $kecm $dirpart $i
   		    seq=$?
   		    cp $FA/$kecm/whiz$i/cardswhiz_in_0b $FA/$kecm/whiz$i/cardswhiz_in
		fi
	    fi
    fi
    typeset -i i
    i=i+1

done



 
