#! /bin/ksh

# Usage example:    multiple_whiz_ini_cleanup xlong 0500 00241 00820
#                                                     ^    ^     ^
#                                                     |    |     |
#                                                     |    |     | last ini run
#                                                     |    | first ini run
#                                                     | integer sqrt(s) (GeV)
#                                     
#                                    

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

typeset -Z4 kecm
typeset  -i  i
typeset  -i  izeroc
typeset  -i  izeralt
typeset  -i  isumany
typeset  -i  istdhep
typeset  -i  iseed




kecm=$2


i=$3
while [[ i -le $4 ]]
do
    ibad=0
    echo "i=" $i
    typeset -Z6 i
    grep -q 'zero cross section in limit of' $FA/$kecm/whiz$i/whizard.log
    izeroc=$?
    grep -q 'sum            0\.0000000' $FA/$kecm/whiz$i/whizard.log
    izeralt=$?
    echo "izeroc,izeralt=" $izeroc $izeralt
    if [[ izeroc -ne 0 && izeralt -ne 0 ]]
    then
	cd $FA/$kecm/whiz$i
	ls -l | grep -q 'whizard\.stdhep'
	istdhep=$?
	grep -q 'sum           ' $FA/$kecm/whiz$i/whizard.log
	isumany=$?
	echo "istdhep,isumany=" $istdhep $isumany
	if [[ istdhep -ne 0 || isumany -ne 0 ]]
	then
            echo "Some error in integration....."
            /bin/echo -n "Continue ?> "
            read ans
            if [ "x$ans" != "yes" ] ; then 
		exit 
            fi
          if [ "xerror" == "xerrors" ] ; then
	    grep 'seed =' whizard.in > /var/tmp/seedfile
	    exec 3< /var/tmp/seedfile
	    read -u3 g1 g2 g3
	    exec 3<&-
	    iseed=$g3
	    iseed=iseed+100000
	    $MOM/cpf whizard.in
	    sed "s/seed.*/seed = xxxx/" whizard.in > whizard.in_temp
	    sed "s/xxxx/$iseed/" whizard.in_temp > whizard.in
	    $MOM/cpf whizard.log
	    rm whizard.log
	    $MOM/cpf whizard.errlog
	    rm whizard.errlog
	    $MOM/cpf whizard.out
 	    rm whizard.out
	    ls *.out | grep -v 'whizard\.out' > /var/tmp/whizoutfile
	    exec 3< /var/tmp/whizoutfile
	    read -u3 whizout
	    exec 3<&-
	    $MOM/cpf $whizout
	    echo "whizout=" $whizout
 	    rm $whizout
            ibad=1
	    if [[ $1 == noric ]]
	    then
#               echo "noric place"
 		$FA/$kecm/whiz$i/runwhiz > $FA/$kecm/whiz$i/whizard.log \
 		    2> $FA/$kecm/whiz$i/whizard.errlog 
	    else
#               echo "bsub place"
                echo "#!/bin/bash" > $FA/$kecm/whiz$i/sub.sh 
		echo " bsub -q $1 $BSUB_OPTION -o whizard.log -e whizard.errlog -J whiz$i ./runwhiz " > $FA/$kecm/whiz$i/sub.sh 
                if [ "x$SUBCMD_FILE" == "" ] ; then
                   . $FA/$kecm/whiz$i/sub.sh
                else
                   echo "(cd $FA/$kecm/whiz$i && . sub.sh )" >> $SUBCMD_FILE
                fi
	    fi
          fi
	fi
    fi

    if [ $ibad -eq 0 ] ; then
       echo "whiz$i passed cleanup test."
    else
       if [ "x$SUBCMD_FILE" == "" ] ; then
          echo "whiz$i failed cleanup test and job was re-submitted."
	else
          echo "whiz$i failed cleanup test and job re-submission command is saved in $SUBCMD_FILE."
	fi
    fi


    typeset -i i
    i=i+1

done

