#!/bin/sh
#
# This script will submit a task 
# using the $1 sixdeskRunnam and the $2 directory sixdeskRundir
# $3=0 mean submit only if not submitted already
# $3=1 means (re-)submit anyway
# The task is NEVER submitted if fort.10.gz exists
# The sixdeskRundir is relative to $sixdesktrack
# It then creates or updates the taskids file to include the new taskid
# The variables sixdeskRunam sixdeskRundir and sixdeskforce must
# be set before calling by . ./dot_boinc

#MACRO myrundir $sixdeskRunnam sixdeskRundir
runnamename=$sixdeskRunnam
sixdeskrundir
sixdeskRundir=$rundirname
#MACRO myrundir

if test ! -d $sixdeskboincdir
then
  mkdir -p $sixdeskboincdir
  # and here use AFS protection group stuff to fix access control list
  fs setacl $sixdeskboincdir $LOGNAME rlidwka
  fs setacl $sixdeskboincdir $sixdeskpts rl
fi
if test -s $sixdeskboincdir/owner
then
  sixdeskowner=`cat $sixdeskboincdir/owner`
  if test "$sixdeskowner" != "$LOGNAME"
  then

#MACRO mymess 0 The workspace/LHCDescrip $sixdeskboincdirname are already in use by $sixdeskowner!!!
    sixdeskmesslevel=0
    sixdeskmess="The workspace/LHCDescrip $sixdeskboincdirname are already in use by $sixdeskowner!!!"
    sixdeskmess
#MACRO mymess


#MACRO myexit 11
    sixdeskexitparam=11
    sixdeskexit
#MACRO myexit

  fi
else
  echo "$LOGNAME" > $sixdeskboincdir/owner
fi
# and here use AFS protection group stuff to fix access control list
fs setacl $sixdeskboincdir $LOGNAME rlidwka
fs setacl $sixdeskboincdir $sixdeskpts rl
mkdir -p $sixdeskboincdir/work
mkdir -p $sixdeskboincdir/results
# and now try and run faster using a different sixdeskTaskId 
# for each study
if test  ! -d $sixdeskhome/sixdeskTaskIds/$LHCDescrip
then
  mkdir -p $sixdeskhome/sixdeskTaskIds/$LHCDescrip
  echo "0" > $sixdeskhome/sixdeskTaskIds/$LHCDescrip/sixdeskTaskId
fi
SIXDESKWHERE=$sixdesktrack/$sixdeskRundir
echo "sixdeskRunnam $sixdeskRunnam SIXDESKWHERE $SIXDESKWHERE FORCE $sixdeskforce sixdeskboincdir  $sixdeskboincdir"
if test ! -d $SIXDESKWHERE
then

#MACRO mymess 0 $SIXDESKWHERE is NOT a directory!!!
  sixdeskmesslevel=0
  sixdeskmess="$SIXDESKWHERE is NOT a directory!!!"
  sixdeskmess
#MACRO mymess
 

#MACRO myexit 5
  sixdeskexitparam=5
  sixdeskexit
#MACRO myexit

fi
sixdeskrunit="true"
if test -s $SIXDESKWHERE/fort.10.gz -a "$sixdeskforce" -lt 2
then

#MACRO mymess 1 Task $sixdeskRunnam already completed; we will NOT re-submit
  sixdeskmesslevel=1
  sixdeskmess="Task $sixdeskRunnam already completed; we will NOT re-submit"
  sixdeskmess
#MACRO mymess

  sixdeskrunit="false"
else
  if test "$sixdeskforce" -eq 0
  then

#MACRO mylock $sixdeskwork
    ###sixdesklockdir=$sixdeskwork
    ###sixdesklock
#MACRO mylock

    if test -s $sixdeskwork/taskids
    then
      grep "$sixdeskRunnam " $sixdeskwork/taskids > /dev/null 2>&1
      sixdeskstat=$?
      if test $sixdeskstat -eq 0
      then
        line=`grep "$sixdeskRunnam " $sixdeskwork/taskids`
        sixdeskoldtaskid=`echo $line | cut -d " " -f2-`
        if test "$sixdeskoldtaskid" != ""
        then

#MACRO mymess 1 Task $sixdeskRunnam already submitted taskid(s) $sixdeskoldtaskid; skipping it
          sixdeskmesslevel=1
          sixdeskmess="Task $sixdeskRunnam already submitted taskid(s) $sixdeskoldtaskid; skipping it"
          sixdeskmess
#MACRO mymess

          sixdeskrunit="false"
        fi
      fi 
    fi

#MACRO myunlock $sixdeskwork
    ###sixdesklockdir=$sixdeskwork
    ###sixdeskunlock
#MACRO myunlock

  fi
fi
if test $sixdeskrunit = "true"
then

#MACRO mymktmp tmp 
  sixdesktmpname=tmp
  sixdesktmpdirarg=
  sixdeskmktmp
  tmp=$sixdesktmp
#MACRO mymktmp

  mkdir -p $sixdeskjobs_logs/run
  # We are going to (re-)submit it
  sixdeskexists=0
  for sixdeski in 2 3 8 16
  do
    if test ! -f $SIXDESKWHERE/fort.$sixdeski.gz
    then

#MACRO mymess 0 File $SIXDESKWHERE/fort.$sixdeski.gz does NOT exist!!!
      sixdeskmesslevel=0
      sixdeskmess="File $SIXDESKWHERE/fort.$sixdeski.gz does NOT exist!!!"
      sixdeskmess
#MACRO mymess

    else
      sixdeskexists=`expr $sixdeskexists + 1`
    fi
  done
  if test "$sixdeskexists" -ne 4
  then
    sixdeskexists=`expr 4 - $sixdeskexists`

#MACRO mymess 0 There are $sixdeskexists input files missing!!! in $SIXDESKWHERE
    sixdeskmesslevel=0
    sixdeskmess="There are $sixdeskexists input files missing!!! in $SIXDESKWHERE"
    sixdeskmess
#MACRO mymess


#MACRO mymess 0 Skipping this case which will require a RUN_SIX!!!
    sixdeskmesslevel=0
    sixdeskmess="Skipping this case which will require a RUN_SIX!!!"
    sixdeskmess
#MACRO mymess

    rm -f $tmp
  else
    # Make sure we are clean (in case of a run_six or system crash)
    rm -f $sixdeskjobs_logs/run/fort.*
    for sixdeski in 2 3 8 16
    do
      cp $SIXDESKWHERE/fort.$sixdeski.gz $sixdeskjobs_logs/run
      gunzip -f $sixdeskjobs_logs/run/fort.$sixdeski.gz
    done
    sixdeskTaskName=$workspace"_"$sixdeskRunnam
    sixdeskTaskName=`echo $sixdeskTaskName | sed -e's?%?__?g'`
    sixdeskfileDir=$sixdeskjobs_logs/run

#MACRO mylock $sixdeskhome/sixdeskTaskIds
  # sixdesklockdir=$sixdeskhome/sixdeskTaskIds
  # sixdesklock
#MACRO mylock

    sixdeskTaskId=`cat $sixdeskhome/sixdeskTaskIds/$LHCDescrip/sixdeskTaskId`
    sixdeskTaskId=`expr $sixdeskTaskId + 1`
    echo $sixdeskTaskId > $sixdeskhome/sixdeskTaskIds/$LHCDescrip/sixdeskTaskId

#MACRO myunlock $sixdeskhome/sixdeskTaskIds
  # sixdesklockdir=$sixdeskhome/sixdeskTaskIds
  # sixdeskunlock
#MACRO myunlock

    sixdesktaskid=boinc$sixdeskTaskId
    gotit=""
    mytries=0
    while test "$gotit" != "OK" -a $mytries -ne 11
    do
      mytries=`expr $mytries + 1`
###  $sixdeskpath/Bin/uploadWorkunit_many $sixdeskTaskName 1 sixvf $sixdeskjobs_logs/run $sixdesktaskid $sixdeskfpopse $sixdeskboincdir > $tmp 2>&1
     $sixdeskpath/Bin/uploadWorkunit $sixdeskTaskName 1 sixvf $sixdeskjobs_logs/run $sixdesktaskid $sixdeskfpopse $sixdeskboincdir > $tmp 2>&1
      sixdeskstat=$?
      if test $sixdeskstat -ne 0
      then

#MACRO mymess 0 UploadWorkunit failed with Status $sixdeskstat !!! `cat $tmp`
        sixdeskmesslevel=0
        sixdeskmess="UploadWorkunit failed with Status $sixdeskstat !!! `cat $tmp`"
        sixdeskmess
#MACRO mymess

        rm $tmp
      else
        gotit="OK"
      fi
    done 
    rm $sixdeskjobs_logs/run/fort.2 $sixdeskjobs_logs/run/fort.3 $sixdeskjobs_logs/run/fort.8 $sixdeskjobs_logs/run/fort.16
    if test "$gotit" != "OK"
    then

#MACRO mymess 0 FAILED to submit task 10 times!!
      sixdeskmesslevel=0
      sixdeskmess="FAILED to submit task 10 times!!"
      sixdeskmess
#MACRO mymess

      rm -f $tmp

#MACRO myexit 10
      sixdeskexitparam=10
      sixdeskexit
#MACRO myexit

    fi
    if test ! -s $SIXDESKWHERE/fort.10.gz 
    then
      touch $SIXDESKWHERE/JOB_NOT_YET_COMPLETED
    fi

#MACRO mylock $sixdeskwork
    ###sixdesklockdir=$sixdeskwork
    ###sixdesklock
#MACRO mylock

    sixdeskoldtaskid=""
    if test -s $sixdeskwork/taskids
    then
      grep "$sixdeskRunnam " $sixdeskwork/taskids
      sixdeskstat=$?
      if test $sixdeskstat -eq 0
      then
        line=`grep "$sixdeskRunnam " $sixdeskwork/taskids`
        sixdeskoldtaskid=`echo $line | cut -d " " -f2-`
      fi
    fi
    if test "$sixdeskoldtaskid" = ""
    then
      sixdesktaskids=$sixdesktaskid" "
    else
      sixdesktaskids=$sixdeskoldtaskid" "$sixdesktaskid" "
    fi
    touch $SIXDESKWHERE/JOB_NOT_YET_COMPLETED
    if test -s $sixdeskwork/taskids
    then
      sed -e'/'$sixdeskRunnam' /d' $sixdeskwork/taskids > $tmp
      mv $tmp $sixdeskwork/taskids
    fi
    echo "$sixdeskRunnam" "$sixdesktaskids" >> $sixdeskwork/taskids
    if test "$sixdeskoldtaskid" != ""
    then

#MACRO mymess 1 Task $sixdeskRunnam re-submitted with TaskId $sixdesktaskid; other tasks $sixdeskoldtaskid
      sixdeskmesslevel=1
      sixdeskmess="Task $sixdeskRunnam re-submitted with TaskId $sixdesktaskid; other tasks $sixdeskoldtaskid"
      sixdeskmess
#MACRO mymess

    else
      echo $sixdeskRunnam >> $sixdeskwork/incomplete_cases
      echo $sixdeskRunnam >> $sixdeskwork/myincomplete_cases

#MACRO mymess 1 Task $sixdeskRunnam submitted with TaskId $sixdesktaskid
      sixdeskmesslevel=1
      sixdeskmess="Task $sixdeskRunnam submitted with TaskId $sixdesktaskid"
      sixdeskmess
#MACRO mymess

    fi

#MACRO myunlock $sixdeskwork
    ###sixdesklockdir=$sixdeskwork
    ###sixdeskunlock
#MACRO myunlock


#MACRO mylock $sixdeskjobs
    ###sixdesklockdir=$sixdeskjobs
    ###sixdesklock
#MACRO mylock

    echo "$sixdeskRunnam" "$sixdesktaskid" >> $sixdeskjobs/tasks
    echo "$sixdeskRunnam" "$sixdesktaskid" >> $sixdeskjobs/incomplete_tasks

#MACRO myunlock $sixdeskjobs
    ###sixdesklockdir=$sixdeskjobs
    ###sixdeskunlock
#MACRO myunlock

    rm -f $tmp
  fi
fi
