#! /bin/ksh

# Usage example:    whiz_run xlong 1000 $WHZ/0-2-4-fermion/ 00241
#                                    ^                        ^
#                                    |                        |
#                                    |                        | run used for initialization
#                                    | integer sqrt(s) (GeV)
#                             
#
#typeset -Z2 -i  seq
typeset  -i  seq
typeset -Z4 kecm

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

kecm=$2

if [ ! -e $FA/$kecm/run_output/w$4 ] ; then
  mkdir -p $FA/$kecm/run_output/w$4
fi

ls -l $FA/$kecm/run_output/w$4 | grep '^d' | grep run > /var/tmp/ls_l_3

nextseq.pl /var/tmp/ls_l_3
seq=$?
#echo $seq
typeset -Z2 seq
mkdir $FA/$kecm/run_output/w$4/run_$seq
echo "created " $FA/$kecm/run_output/w$4/run_$seq

# Set CPU TIME limit automatically
TIME_LIMIT=
case $1 in 
  s) TIME_LIMIT=3;;
  m) TIME_LIMIT=700;;
  l) TIME_LIMIT=1430;;
  h) TIME_LIMIT=10070;;
  *) TIME_LIMIT=99999;;
esac


jobprefix=w$4'_'
jobname=$jobprefix$seq
cp $FA/$kecm/whiz$4/cardswhiz_in $FA/$kecm/run_output/w$4/run_$seq
sed -e "s/xxxx/$4/" $FA/$kecm/whiz$4/cardswhiz_in \
    -e "/time_limit/c time_limit = ${TIME_LIMIT}" \
    > $FA/$kecm/run_output/w$4/run_$seq/whizard.in
sed -e "s!yyyy!$3!" \
    -e "s!whizxxxx!run_output/w$4/run_$seq!" \
    -e "s!zzzz!$jobprefix!" \
    -e "s!kkkk!$kecm!" \
    -e "s!vvvv!$4!g" \
    -e "s!wwww!$jobname!" $WHIZTOOLS/bin/runwhiz \
	 > $FA/$kecm/run_output/w$4/run_$seq/runwhiz
# cp -f $FA/$kecm/whiz$4/*.cut0 $FA/$kecm/run_output/w$4/run_$seq
cp -f $FA/$kecm/whiz$4/*.cut1 $FA/$kecm/run_output/w$4/run_$seq
cp -f $FA/$kecm/whiz$4/*.cut5 $FA/$kecm/run_output/w$4/run_$seq
cp -f $FA/$kecm/whiz$4/*.grb $FA/$kecm/run_output/w$4/run_$seq
cp -f $FA/$kecm/whiz$4/*.grc $FA/$kecm/run_output/w$4/run_$seq
cp -f $FA/$kecm/whiz$4/*.mdl $FA/$kecm/run_output/w$4/run_$seq
cp -f $FA/$kecm/whiz$4/*.phx $FA/$kecm/run_output/w$4/run_$seq
cp -f $FA/$kecm/whiz$4/*.prc $FA/$kecm/run_output/w$4/run_$seq
# cp -f $FA/$kecm/whiz$4/slha*.in $FA/$kecm/run_output/w$4/run_$seq
for f in $FA/$kecm/whiz$4/slha*.in $FA/$kecm/whiz$4/*.cut0 ; do
  if [ -e $f ] ; then
    cp -f $f $FA/$kecm/run_output/w$4/run_$seq
  fi
done

chmod a+x $FA/$kecm/run_output/w$4/run_$seq/runwhiz
    if [[ $1 == noric ]]
    then
         echo "noric place"
	$FA/$kecm/run_output/w$4/run_$seq/runwhiz > $FA/$kecm/run_output/w$4/run_$seq/whizard.log \ 
                2> $FA/$kecm/run_output/w$4/run_$seq/whizard.errlog &
    else
	echo "#!/bin/bash " > $FA/$kecm/run_output/w$4/run_$seq/sub.sh
        echo "export FA=$FA" >> $FA/$kecm/run_output/w$4/run_$seq/sub.sh
        echo "bsub -q $1 -o whizard.log -e whizard.errlog -J w$4_$seq ./runwhiz " >> $FA/$kecm/run_output/w$4/run_$seq/sub.sh
        if [ "x$SUBCMD_FILE" == "x" ] ; then
          ( cd $FA/$kecm/run_output/w$4/run_$seq 
		. ./sub.sh )
           echo "Job whiz$4 submitted."
        else 
           echo "(cd $FA/$kecm/run_output/w$4/run_$seq && . sub.sh ) " >> $SUBCMD_FILE
           echo "A command to submit job w$4_$seq is added to $SUBCMD_FILE"
        fi
#        echo "bsub place"
    fi

typeset -i seq
exit $seq


