#!/bin/ksh -l
set -xa

########################################
# RAP Product Generation
########################################

######################################################
# The following two variables could be defined in the
# loadleveler submission script (the ecf script), if
# not they will take the default values which is set
# for the NCO running enviroment
#######################################################
export RUN_ENVIR=${RUN_ENVIR:-nco}
export SENDECF=${SENDECF:-YES}

###################################
# Specify NET and RUN Name and model
####################################
export NET=${NET:-rap}
export RUN=${RUN:-rap}
export model=${model:-rap}

#####################################################################################
# This block is for Developer's test run:
# Run config file to get input parameters
# This config file should define the following variables
# DATA_IN: Location of working directory, default to /tmpnwprd
# SENDECF: If the job is to be running using ECF, default to YES
# SENDDBN: Set to NO for developers, default to YES
# COM_IN:  Directory for input files, default to /com/$NET/${envir}
# COM_OUT: Directory for output file, default to /com/$NET/${envir}
# gespath: Directory for the guess or restart files, default to /nwges/${envir}
#####################################################################################
if [ "$RUN_ENVIR" != nco ]      ### For Developers, "group_name" is passed from the SMS script
then
  group_name=${group_name:-meso}
  package_name=${package_name:-rap}
#  . /${group_name}/save/${LOGNAME}/${package_name}/nwprod/parm/${package_name}_para_config
  export userid=${userid:-$LOGNAME}
fi

export PS4='$SECONDS + '
date

####################################
# obtain unique process id (pid) and make temp directory
####################################
export DATA=$DATAROOT/rap_sndp_${cyc}.$$
mkdir -p $DATA
cd $DATA

export cycle=t${cyc}z
export tmmark=${tmmark:-tm00}

export pgmout="OUTPUT.$$"

####################################
# SENDECF  - Flag Events on ECF
# SENDCOM  - Copy files to /com directory
####################################
export SENDECF=${SENDECF:-YES}
export SENDCOM=${SENDCOM:-YES}
export SENDDBN=${SENDDBN:-YES}

####################################
# Specify Execution Areas
####################################
export HOMErap=${HOMErap:-${PACKAGEROOT}/${NET}.${rap_ver}}
export EXECrap=${EXECrap:-${HOMErap}/exec}
export FIXrap=${FIXrap:-${HOMErap}/fix}
export PARMrap=${PARMrap:-${HOMErap}/parm}
export USHrap=${USHrap:-${HOMErap}/ush}

###########################################
# Run setpdy and initialize PDY variables
###########################################
setpdy.sh
. ./PDY

##############################################
# Define COM directories
##############################################

export COMIN=${COMIN:-$(compath.py ${NET}/${rap_ver})/${RUN}.${PDY}}
export COMOUT=${COMOUT:-$(compath.py ${NET}/${rap_ver})/${RUN}.${PDY}}

export COMAWP=${COMAWP:-${COMOUT}/gempak}

mkdir -m 775 -p $COMOUT $COMAWP

##############################################
# Define GES directories
##############################################

export gespath=${gespath:-$(compath.py ${NET}/${rap_ver})/nwges}
export RAPBC=${RAPBC:-$gespath/rapbc}

mkdir -m 775 -p $RAPBC

env

########################################################
# Execute the script.
${RAP_SNDPSH:-$HOMErap/scripts/exrap_postsnd.sh}
########################################################

cat $pgmout

msg="ENDED NORMALLY."
postmsg "$msg"

##############################
# Remove the Temporary working directory
##############################
cd $DATAROOT
if [ ${KEEPDATA} = NO ] ; then rm -rf $DATA ; fi

date