#!/bin/sh

set -xa

date
export PS4='$SECONDS + ' 
# 
# obtain unique process id (pid) and make temp directory
#

# ecflow log directory
export COMIN_ECFLOG=/lfs/h1/ops/${ecf_envir}/output/ecflow

# comment out some of the following -08/11 - JY
#if [ "${envir:?}" = "prod" ]
#then
    # JY - remove all the / in the end of paths - 08/18
    # JY export COMOUT=${COMOUT:-$COMROOT/logs/runtime/}
    export COMOUT=${COMOUT:-$COMROOT/logs/runtime/${ecf_envir}}
    export COMOUTruntime=${COMOUTdaily:-$COMOUT/runtime/}
    export COMOUTdaily=${COMOUTdaily:-$COMOUT/daily/}
    #export COMOUT2=${COMOUT2:-$COMOUT/runtime2/}
    export COMOUTruntime2=${COMOUTruntime2:-$COMOUT/runtime2/}
    export COMOUTinter=$COMOUT/interProcess
#else
#    export COMOUT=${COMOUT:-$COMROOT/logs/${envir}/runtime/}
#    export COMOUTdaily=${COMOUTdaily:-$COMROOT/logs/${envir}/runtime/daily/}
#    export COMOUT2=${COMOUT2:-$COMROOT/logs/${envir}/runtime2/}
#    export COMOUTinter=$COMOUT/interProcess/
#fi

mkdir -p -m775 $COMOUT $COMOUTdaily $COMOUTruntime $COMOUTruntime2 $COMOUT2 $COMOUTinter $COMOUT/ave30

# JY export HOMEruntime=${HOMEruntime:-/nw${envir}/runtime.$runtime_ver}
export FIXruntime=${FIXruntime:-${HOMEruntime}/fix}
export USHruntime=${USHruntime:-${HOMEruntime}/ush}
export USHrun=${USHrun:-${HOMEruntime}/ush}
export FIXrun=${FIXrun:-${FIXruntime}}

export pid=$$
export DATAROOT=${DATAROOT:-/tmpnwprd1}
export DATA=${DATA:-${DATAROOT}/${job:?}.${pid}}

if [ ! -d $DATA ]
then
  mkdir $DATA
fi
cd $DATA 

export ERRORFN=$DATA/errorFN
export PATH=$PATH:/usr/local/bin

export cyc=00
export cycle=t${cyc}z 

####################################
# File To Log Msgs
####################################
if [ "${envir}" = "prod" ] && [ "${ecf_envir}" = "prod" ]
then
   export SENDWEB=YES	
#  export jlogfile=/com/logs/jlogfiles/jlogfile.${job}.${pid}
else 
   export SENDWEB=NO
#  export jlogfile=/com/logs/${envir}/jlogfile
fi

####################################
# Determine Job Output Name on System
####################################
export outid="LL$job"
export jobid="${outid}.o${pid}"
export pgmout="OUTPUT.${pid}"

export SENDWEB=${SENDWEB:-YES}
export SENDCOM=${SENDCOM:-NO}
export SENDDBN=${SENDDBN:-NO}
export SENDECF=${SENDECF:-YES}

###################################
# Set up the UTILITIES
###################################
#export utilscript=/nwprod/util/ush
#export utilexec=/nwprod/util/exec

## Run setup to initialize working directory and utility scripts
#$utilscript/setup.sh
## Run setpdy and initialize PDY variables
export PDY=`date +%Y%m%d`
setpdy.sh
. ./PDY

##############################################
# Execute the script
#
${HOMEruntime}/scripts/exruntime.sh
##############################################

##############################################
# Clean up the the temporary files if no errors occurred

if [ -f $ERRORFN ]
then
  ecflow_client --abort
else
  cd $DATAROOT
  if [ ${KEEPDATA:-NO} != YES ]; then rm -rf $DATA; fi
fi
exit