#!/bin/ksh
#
arguments=$*

if [ $1 ] ; then
  cd $1 2> /dev/null
  if [ $? -ne 0 ] ; then
    printf "\n Directory $1 is unavailable --- ABORT ---\n\n"
    exit 1
  fi
fi

Um_checkcfg.ksh multi
flag_cfg=$?

if [ $flag_cfg -eq 0 ] ; then
  set -x
  . ./BATCH_config.cfg
  set +x
fi

. r.entry.dot
#
eval `cclargs_lite -D " " $0 \
\
 -BATCH_launch_mach       "${BATCH_launch_mach}"           ""      "[Backend machine name                     ]"\
 -BATCH_launch_time_ntr   "${BATCH_launch_time_ntr:-600}"  "600"   "[Job resources: gemntr wallclock time     ]"\
 -BATCH_launch_time_mod   "${BATCH_launch_time_mod:-3600}" "3600"  "[Job resources: gemdm  wallclock time     ]"\
 -BATCH_launch_time_post  "${BATCH_launch_time_post:-3600}" "3600" "[Job resources: postprocessing  wall time ]"\
 -BATCH_launch_cm         "${BATCH_launch_cm:-10G}"        "10G"   "[Job resources: gemdm  memory size        ]"\
 -BATCH_launch_execdir    "${BATCH_launch_execdir}"        ""      "[Working directory                        ]"\
 -BATCH_launch_upload     "${BATCH_launch_upload}"         ""      "[Upload directory                         ]"\
 -BATCH_launch_listings   "${BATCH_launch_listings:-${HOME}/listings}" "" "[Directory for job listings        ]"\
 -BATCH_launch_class      "${BATCH_launch_class}"          ""      "[to control -cl option with soumet        ]"\
 -BATCH_launch_nosubmit   "${BATCH_launch_nosubmit:-0}"    "1"     "[to issu -nosubmit option with soumet     ]"\
 -BATCH_launch_cfg        "${BATCH_launch_cfg:-0:0}"       "0:0"   "[Concurrent configurations to run         ]"\
 -BATCH_launch_exp        "${BATCH_launch_exp:-dbg1}"      "dbg1"  "[Current experiment name                  ]"\
 -BATCH_launch_ovbin      "${BATCH_launch_ovbin}"          ""      "[Full path of main model executables      ]"\
 -BATCH_launch_r_upl      "${BATCH_launch_r_upl:-1}"       "0"     "[To upload on the backend target machine  ]"\
 -BATCH_launch_r_ntr      "${BATCH_launch_r_ntr:-1}"       "0"     "[To run Um_runent.ksh                     ]"\
 -BATCH_launch_r_mod      "${BATCH_launch_r_mod:-1}"       "0"     "[To run Um_runmod.ksh                     ]"\
 -BATCH_launch_norset     "${BATCH_launch_norset:-1}"      "0"     "[Make use of LL resources set             ]"\
 -BATCH_launch_ntsortie   "${BATCH_launch_ntsortie:-1}"    "1"     "[Numebr of CPUs for the sortie job        ]"\
 -BATCH_launch_xferl      "${BATCH_launch_xferl:-0}"       "1"     "[Transfer listings along with model output]"\
 -BATCH_launch_onexferjob "${BATCH_launch_onexferjob:-0}"  "1"     "[Sequentializes all model output xfer jobs]"\
 -BATCH_launch_clean      "${BATCH_launch_clean:-1}"       "0"     "[Clean some directories after usage       ]"\
 -BATCH_launch_cpus_ntr   "${BATCH_launch_cpus_ntr:-1}"    ""      "[Number of Cpus to run gemntr             ]"\
 -BATCH_launch_ptopo      "${BATCH_launch_ptopo:-1x1x1}"   ""      "[MPI&OMP PEs topology (npex x npey x nomp)]"\
 -BATCH_launch_instances  "${BATCH_launch_instances}"      ""      "[Number of grids x number of domains      ]"\
 -BATCH_launch_waste      "${BATCH_launch_waste:-0}"       ""      "[Allow cpu waste on compute nodes         ]"\
 -BATCH_launch_in_block   "${BATCH_launch_in_block}"       ""      "[Input  file blocking (nblocx x nblocy)   ]"\
 -BATCH_launch_out_block  "${BATCH_launch_out_block}"      ""      "[Output file blocking (nblocx x nblocy)   ]"\
 -BATCH_launch_outfreq    "${BATCH_launch_outfreq}"        ""      "[Output post-processing frequencing       ]"\
 -BATCH_launch_SMT        "${BATCH_launch_SMT:-1}"         "1"     "[SMT controler for AIX-powerpc7           ]"\
 -BATCH_launch_smt        "${BATCH_launch_smt}"            ""      "[SMT controler (AIX) (smtdyn x smtphy)    ]"\
 -BATCH_launch_bind       "${BATCH_launch_bind:-0}"        "1"     "[Processor binding logical controler      ]"\
 -BATCH_launch_ppid       "${BATCH_launch_ppid:-$$}"       ""      "[User choice PPID for job summission      ]"\
 -BATCH_launch_cmclog     "${BATCH_launch_cmclog}"         ""      "[Full path for CMC log file               ]"\
 -BATCH_launch_barrier    "${BATCH_launch_barrier:-0}"     "1"     "[Third party job (SM) will run the model  ]"\
 -BATCH_launch_timing     "${BATCH_launch_timing}"         ""      "[AIX HPM timing info                      ]"\
 -BATCH_launch_NTR_headscript      "${BATCH_launch_NTR_headscript}"      ""  "[Tailscript to run at beginning of JOBATM_E            ]"\
 -BATCH_launch_NTR_tailscript      "${BATCH_launch_NTR_tailscript}"      ""  "[Tailscript to run at the end   of JOBATM_E            ]"\
 -BATCH_launch_MOD_tailscript_each "${BATCH_launch_MOD_tailscript_each}" ""  "[Tailscript to run at the end   of JOBATM_M each rstrt ]"\
 -BATCH_launch_MOD_tailscript_last "${BATCH_launch_MOD_tailscript_last}" ""  "[Tailscript to run at the end   of JOBATM_M last rstrt ]"\
 -BATCH_launch_MOD_tailjob_each    "${BATCH_launch_MOD_tailjob_each}"    ""  "[Tailjob to launch at the end   of JOBATM_M each rstrt ]"\
 -BATCH_launch_MOD_tailjob_last    "${BATCH_launch_MOD_tailjob_last}"    ""  "[Tailjob to launch at the end   of JOBATM_M last rstrt ]"\
\
 -restart    "0"         "1"    "[Restart mode              ]" \
 -_job       ""          ""     "[OUTPUT: jobname           ]" \
 -_listing   ""          ""     "[Listing directory         ]" \
 -_status    "ABORT"     ""     "[return status             ]" \
 ++ $arguments`
\

BATCH_launch_onexferjob=0

if [ $flag_cfg -eq 0 ] ; then

if [ ! $BATCH_launch_mach   ] ; then . Um_aborthere.ksh "BATCH_launch_mach undefined"   ; fi
if [ ! $BATCH_launch_exp    ] ; then . Um_aborthere.ksh "BATCH_launch_exp undefined"    ; fi

listings=${BATCH_launch_listings:-${HOME}/listings}
if [ -d $listings/${BATCH_launch_mach} -o -L $listings/${BATCH_launch_mach} ] ; then
  listings=$listings/${BATCH_launch_mach}
fi
_listing=$listings
BATCH_launch_listings=$listings

lajob=${_job:-${BATCH_launch_exp}}
jobname=`pwd`/${lajob}
_job=$jobname

rootdir_exec=${HOME}/MODEL_EXEC_RUN/${BATCH_launch_mach}/gem/${BATCH_launch_exp}
BATCH_launch_execdir=${BATCH_launch_execdir:-${rootdir_exec}}
BATCH_launch_upload=${BATCH_launch_upload:-${BATCH_launch_execdir}/UPLOAD}

if [ ${BATCH_launch_xferl:-0} -gt 0 ] ; then
  BATCH_launch_xferl="${listings}/${lajob}_?.${BATCH_launch_ppid}*"
else
  BATCH_launch_xferl=''
fi

for i in `set | grep '^BATCH_launch' | sed "s/'//g"` ; do
  var=${i%%=*}
  val=${i##*=}
  export ${var}=${val}
done

env | grep BATCH_launch

. r.call.dot Um_drive.ksh -jobname $jobname -restart $restart

fi

. r.return.dot
