#!/bin/bash
set -xa
date -u

###################################################################
# Set DEBUG LEVEL - modifies info in execution trace prompt string
###################################################################
export DEBUG_LEVEL=${DEBUG_LEVEL=1}
case $DEBUG_LEVEL in
  1) export PS4='+ ${SECONDS}s + ' ;;       # add seconds (this is typical case)
  0) export PS4='+ ';;                      # don't include seconds (useful if
                                            #  want to diff output from 2 runs)
  2) export PS4='+ ${SECONDS}s:L$LINENO + ';;         # add script line #
  3) export PS4='+ ${SECONDS}s:${0##*/}:L$LINENO + ';;# add script name & line #
  4) export PS4='+ ${0##*/}:L$LINENO + ';;  # script name & line (no seconds)
  *) echo "Unrecognized DEBUG_LEVEL.  Stay with current PS4 setting";;
esac

########################################
# Check that required variables are set
########################################
set +x
echo
echo cyc is ${cyc:?"###FATAL ERROR \"cyc\" is not set"}
echo tmmark is ${tmmark:?"###FATAL ERROR \"tmmark\" is not set"}
echo envir is ${envir:?"###FATAL ERROR \"envir\" is not set"}
echo job is ${job:?"###FATAL ERROR \"job\" is not set"}
echo DATAROOT is ${DATAROOT:?"###FATAL ERROR \"DATAROOT\" is not set"}
echo
set -x

################################
# Print out version information
################################
set +x
echo
echo "####################################################################"
echo
echo "Current OBSPROC version is ${obsproc_ver:-not set}"
echo
echo "####################################################################"
echo
set -x

#####################################################################
# List modules loaded for this job
#####################################################################
set +x
echo
echo "####################################################################"
echo
      module list
echo
echo "####################################################################"
echo
set -x

######################
# Specify NET and RUN
######################
export obsNET=${obsNET:-obsproc}
export NET=${NET:-nam}
export RUN=${RUN:-nam}
set +x
echo
echo obsNET is ${obsNET:?"###FATAL ERROR \"obsNET\" is not set"}
echo NET is ${NET:?"###FATAL ERROR \"NET\" is not set"}
echo RUN is ${RUN:?"###FATAL ERROR \"RUN\" is not set"}
echo
set -x

################
# Specify cycle
################
export cycle=t${cyc}z

###############################
# Create new working directory
###############################
jobid=${jobid:-${job}.$(hostname -s).$$}
export DATA=${DATA:-$DATAROOT/${jobid}}
rm -rf $DATA
mkdir -p $DATA
cd $DATA
export pgmout=OUTPUT.$$

######################################
# Set job log file for postmsg output
######################################
jlogfile=${jlogfile:-${DATA}/jlogfile}

#################################################
# SENDCOM      - Copy files to $COMOUT directory
# SENDECF      - Flag Events on ECFLOW
# SENDDBN      - Alert output file to TOC
#################################################
export SENDCOM=${SENDCOM:-YES}
export SENDECF=${SENDECF:-YES}
export SENDDBN=${SENDDBN:-YES}

##########################
# Specify Execution Areas
##########################

# ----------------------------------
# Paths for obsproc & prepobs items
# ----------------------------------
export HOMEobsproc=${HOMEobsproc:\
-${PACKAGEROOT:?}/${obsNET}/${obsproc_ver:?\
"###FATAL ERROR \"obsproc_ver\" is not set"}}

export SCRIPTSprepobs=${SCRIPTSprepobs:\
-$HOMEprepobs/scripts}

export EXECobsproc=${EXECobsproc:-$HOMEobsproc/exec}
export USHobsproc=${USHobsproc:-$HOMEobsproc/ush}

#########################################################################
# Add some prod utilities to working directory
#########################################################################
echo "step ############# break ##############################" > ./break
cp $UTILROOT/ush/err_chk   .; chmod +x err_chk
cp $UTILROOT/ush/err_exit  .; chmod +x err_exit
cp $UTILROOT/ush/prep_step .; chmod +x prep_step
cp $UTILROOT/ush/postmsg   .; chmod +x postmsg
cp $UTILROOT/ush/setpdy.sh .; chmod +x setpdy.sh

##########################################
# Run setpdy and initialize PDY variables
##########################################
set +x; echo -e "\n---> path to setpdy.sh below is: `which setpdy.sh`"; set -x
if [ -z "$PDY" ]; then
  if test "$cyc" = "12"
  then
    if test "$tmmark" != "tm00"
    then
      # Since the 12Z catch-up cycle (tm06 through tm01) jobs run before the 12 UTC
      # PROD_SETUP job runs we need to use the ${COMDATEROOT}/date/t00z file for our PDY
      export cycle=t00z
      setpdy.sh
      . $DATA/PDY
      export cycle=t12z
      setpdy.sh
      . $DATA/PDY
    else
      # The 12Z on-time cycle (tm00) runs after the 12 UTC PROD_SETUP job
      setpdy.sh
      . $DATA/PDY
    fi
  elif test "$cyc" = "00"
  then
    if test "$tmmark" != "tm00"
    then
      # Check to see if we are running a 00Z catch-up cycle (tm06 through tm01) job
      # prior to run of 00 UTC PROD_SETUP - if so, update PDY by 1-day
      setpdy.sh
      . $DATA/PDY
      edate=`cat ${COMDATEROOT}/date/t12z | cut -c7-14`
      if [ $edate -eq $PDY ]
      then
        export PDY=`$utilscript/finddate.sh $PDY d+1`
        setpdy.sh
        . $DATA/PDY
      fi
    else 
      # The 00Z on-time cycle (tm00) runs after the 00 UTC PROD_SETUP job
      setpdy.sh
      . $DATA/PDY
    fi
  else
    # The 06 & 18Z jobs at all cycles after the 00 & 12 UTC PROD_SETUP jobs, resp.
    setpdy.sh
    . $DATA/PDY
  fi
else
# If PDY was previously specified (checkout only) fall through to here
  setpdy.sh
  . $DATA/PDY
fi

#########################
# Define COM directories
#########################
export COMIN_ROOT=${COMIN_ROOT:-${COMROOT:-""}}

if [[ "$RUN_ENVIR" == nco ]]; then
  export COMIN=${COMIN:-$(compath.py ${envir}/${obsNET}/${obsproc_ver}/${RUN}.${PDY})}
  export COMOUT=${COMOUT:-$(compath.py -o ${obsNET}/${obsproc_ver}/${RUN}.${PDY})}
  mkdir -m 775 -p $COMOUT
else
  export COMIN=${COMIN:-${COMIN_ROOT:?}/${obsNET}/${obsproc_ver}/${RUN}.${PDY}}


# COMOUT_ROOT for developers defaults to unique $DATA/com directory as root to
#  prevent job from writing into a non-unique directory that might already have
#  output in it from a previous checkout run for the same cycle
# -----------------------------------------------------------------------------
  export COMOUT_ROOT=${COMOUT_ROOT:-${DATA}/com}
  export COMOUT=${COMOUT:-${COMOUT_ROOT}/${obsNET}/${obsproc_ver}/${RUN}.${PDY}}
  mkdir -m 755 -p $COMOUT
fi

########################################################
# SETUP NAM POST-ANALYSIS PREPBUFR PROCESSING VARIABLES
########################################################

#########################################################
# Specify variables specific to this execution of script
#########################################################
export PROCESS_REMOREST=${PROCESS_REMOREST:-YES}
export PROCESS_TIMETWINS=NO  # can never run here
export PROCESS_ALL_REPORT_COUNTS=NO  # can never run here

env

#####################
# Execute the script
#####################
$SCRIPTSprepobs/exprep_post.sh
eval err_${RUN}_prep_post=$?
eval [[ \$err_${RUN}_prep_post -ne 0 ]] && $DATA/err_exit

echo "$SITE `hostname`  --  `date -u`" > $COMOUT/where_${tmmark}_${cycle}_${RUN}_prep_post_ran

> $COMOUT/obsproc_version_for_${tmmark}_${cycle}_${RUN}_prep_post_run
[ -n "$obsproc_ver" ]  &&  \
 echo "Current OBSPROC version is $obsproc_ver" >> \
 $COMOUT/obsproc_version_for_${tmmark}_${cycle}_${RUN}_prep_post_run

if [ "$KEEPDATA" != YES ]; then
   cd $DATAROOT
   rm -rf $DATA
fi
date -u

exit
