#!/bin/csh -f

# This script submits the run_epoc.csh script with the correct parameters

# has the help option been specified?
if ( $1 == "--help" || $1 == "-h") then
    echo "SUBMIT    (N. L. ABRAHAM 2005/09/06)"
    echo " "
    echo "Usage is:"
    echo "submit {nodes} {walltime} {run_name} ({checkp}) ({checkf}) ({afterok:"\#"})"
    echo "where ({checkp}) ({checkf}) ({afterok:"\#"}) are optional arguments."
    echo " "
    echo "These are equivalent to the qsub commands: "
    echo " "
    echo "qsub  -l nodes={nodes}:ppn=2,walltime={walltime} "\$"{HOME}/scripts/run_epoc.csh "\\
    echo "-N {run_name} -v run_name={run_name}"
    echo "      here:"
    echo "            {run_name}    = root of {run_name}.input "
    echo "                                 e.g. test for file test.input"
    echo "            {nodes}       = integer from 1 -} 16     "
    echo "                                 e.g. 8 for 8 nodes, 2 procs/node"
    echo "            {walltime}    = time in HH:MM:SS         "
    echo "                                 e.g. 48:00:00 for a run time of 2 days"
    echo " "
    echo "qsub  -l nodes={nodes}:ppn=2,walltime={walltime} "\$"{HOME}/scripts/run_epoc.csh "\\
    echo "-N {run_name} -v run_name={run_name},checkp={checkp}"
    echo "      here:"
    echo "            {checkp}      = anything: checkpoint file used is "
    echo "                            "\$"FROMDIR"\$"run_name.check"
    echo " "
    echo "qsub  -l nodes={nodes}:ppn=2,walltime={walltime} "\$"{HOME}/scripts/run_epoc.csh "\\
    echo "-N {run_name} -v run_name={run_name},checkp={checkp},checkf={checkf}"
    echo "      here:"
    echo "            {checkp}      = integer: base number (PBS_JOBID) of a "
    echo "                            *****.erik.beo.org/ directory   e.g. 40498 "
    echo "            {checkf}      = file extension of a checkpoint file in XYZ format"
    echo "                                                            e.g. check, xyz"
    echo "                            Whatever the files original extension is, it is "
    echo "                            copied to "\$"run_name.check in "\$"RUNDIR"
    echo " "
    echo "qsub  -l nodes={nodes}:ppn=2,walltime={walltime} "\$"{HOME}/scripts/run_epoc.csh "\\
    echo "-N {run_name} -W depend=afterok:{afterok:"\#"} -v run_name={run_name},"\\
    echo "checkp={checkp},checkf={checkf}"
    echo "      here:"
    echo "            {afterok:"\#"}   = integer, PBS_JOBID of a job already running."
    echo "                               Typically is the same as {checkp}"
    echo " "
    echo " "
    echo "So, the call for the above job would be: "
    echo " "
    echo " "\$" submit 8 48:00:00 test"
    echo " "\$" submit 8 48:00:00 test yes"
    echo " "\$" submit 8 48:00:00 test 40498 xyz"
    echo " "\$" submit 8 48:00:00 test 40498 xyz 40498"
    exit 1
endif

# check to make sure that have the correct number of parameters
if ( $#argv < 3) then
    echo "too few parameters, need 3-6 in order:"
    echo "nodes walltime run_name (checkp) (checkf) (afterok:"\#")"
    echo "run with --help or -h for more details" 
    exit -1
endif
if ( $#argv > 6) then
    echo "too many parameters, need 3-6 in order:"
    echo "nodes walltime run_name (checkp) (checkf) (afterok:"\#")"
    echo "run with --help or -h for more details" 
    exit -1
endif

# assign parameters to correct variables
set runnodes=$1
set runtime=$2
set runname=`echo $3 | awk 'BEGIN{FS="."}{print $1}'`


# only 3 parameters: no checkpointing
if ( $#argv == 3) then
    echo "qsub -l nodes="$runnodes":ppn=2,walltime="$runtime" "$HOME"/scripts/run_epoc.csh -N "$runname" -v run_name="$runname
    qsub -l nodes=${runnodes}:ppn=2,walltime=${runtime} ${HOME}/scripts/run_epoc.csh -N ${runname} -v run_name=${runname}
else 
    # have 4, 5 or 6 parameters: checkpointing
    set runcheckp=$4
    # only have 4 - checkfile is in /home/nla101/epoc/ directory
    if ( $#argv == 4) then
	echo "qsub -l nodes="$runnodes":ppn=2,walltime="$runtime" "$HOME"/scripts/run_epoc.csh -N "$runname" -v run_name="$runname",checkp="$runcheckp
	qsub -l nodes=${runnodes}:ppn=2,walltime=${runtime} ${HOME}/scripts/run_epoc.csh -N ${runname} -v run_name=${runname},checkp=${runcheckp}
    else if ( $#argv == 5) then
	# have 5 parameters, directory is runcheckp, and file is runname.runcheckf
	set runcheckf=$5
	echo "qsub -l nodes="$runnodes":ppn=2,walltime="$runtime" "$HOME"/scripts/run_epoc.csh -N "$runname" -v run_name="$runname",checkp="$runcheckp",checkf="$runcheckf
	qsub -l nodes=${runnodes}:ppn=2,walltime=${runtime} ${HOME}/scripts/run_epoc.csh -N ${runname} -v run_name=${runname},checkp=${runcheckp},checkf=${runcheckf}
    else if ( $#argv == 6) then
	# have 6 parameters:  use depend=after_ok on number
	set runcheckf=$5
	set runokval=$6
	echo "qsub -l nodes="$runnodes":ppn=2,walltime="$runtime" "$HOME"/scripts/run_epoc.csh -N "$runname" -W depend=afterok:"$runokval" -v run_name="$runname",checkp="$runcheckp",checkf="$runcheckf  
	qsub -l nodes=${runnodes}:ppn=2,walltime=${runtime} ${HOME}/scripts/run_epoc.csh -N ${runname} -W depend=afterok:${runokval} -v run_name=${runname},checkp=${runcheckp},checkf=${runcheckf}  
    endif
endif