#! /bin/csh -f

#################################################################################
if ($PHASE == set_batch) then
#################################################################################

source ./Tools/ccsm_getenv || exit -1

set ntasks  = `${CASEROOT}/Tools/taskmaker.pl -sumonly`
set maxthrds = `${CASEROOT}/Tools/taskmaker.pl -maxthrds`

#@ nodes = $ntasks / ${MAX_TASKS_PER_NODE}
#if ( $ntasks % ${MAX_TASKS_PER_NODE} > 0) then
#  @ nodes = $nodes + 1
#  @ ntasks = $nodes * ${MAX_TASKS_PER_NODE}
#endif
#@ taskpernode = ${MAX_TASKS_PER_NODE} / ${maxthrds}

set qname = standard
set tlimit = "02:59:00"
if ($CCSM_ESTCOST > 2) set tlimit = "05:59:00"

if ($?TESTMODE) then
 set file = $CASEROOT/${CASE}.test 
else
 set file = $CASEROOT/${CASE}.run 
endif

cat >! $file << EOF1
#!/bin/csh -f
#PBS -N ${CASE}
#PBS -q ${qname}
#PBS -l nodes=${ntasks}
#PBS -l walltime=${tlimit}
#PBS -j oe
#PBS -S /bin/csh -V

EOF1

#################################################################################
else if ($PHASE == set_exe) then
#################################################################################

set maxthrds = `${CASEROOT}/Tools/taskmaker.pl -maxthrds`
set maxtasks = `${CASEROOT}/Tools/taskmaker.pl -sumtasks`

cat >> ${CASEROOT}/${CASE}.run << EOF1
sleep 25
cd \$RUNDIR
echo "\`date\` -- CSM EXECUTION BEGINS HERE" 

setenv OMP_NUM_THREADS ${maxthrds}
mpirun -np ${maxtasks} \$EXEROOT/cesm.exe >&! cesm.log.\$LID

wait
echo "\`date\` -- CSM EXECUTION HAS FINISHED" 

EOF1

#################################################################################
else if ($PHASE == set_larch) then
#################################################################################

   #This is a place holder for a long-term archiving script

#################################################################################
else
#################################################################################

    echo "  PHASE setting of $PHASE is not an accepted value"
    echo "  accepted values are set_batch, set_exe and set_larch"
    exit 1

#################################################################################
endif
#################################################################################
