#!/bin/sh

date
export PS4='+ $SECONDS + '
set -x

export cycle=${cycle:-t${cyc:?}z}
export storm_num=${storm_num:-1}
export stormlabel=${stormlabel:-storm$storm_num}
export hafs_ver=${hafs_ver:?}
export HAFS_VERSION=${hafs_ver/v/}
export NET=${NET:?}
export RUN=${RUN:?}
export RUN_ENVIR=${RUN_ENVIR:?}

export SENDCOM=${SENDCOM:-YES}
export SENDDBN=${SENDDBN:-NO}
export SENDECF=${SENDECF:-NO}
export EMAIL_SDM=${EMAIL_SDM:-NO}
export SCRUBDATA=${SCRUBDATA:-YES}
# HAFS workflow jobs use shared working dir, and the CLEANUP or SCRUB job will clean up WORKhafs
#export KEEPDATA=${KEEPDATA:-YES}
export KEEPDATA=YES

export HOMEhafs=${HOMEhafs:-${OPSROOT:?}/hafs.${hafs_ver:?}}
export USHhafs=$HOMEhafs/ush
export EXEChafs=$HOMEhafs/exec
export PARMhafs=$HOMEhafs/parm
export FIXhafs=$HOMEhafs/fix

export WORKhafs=${WORKhafs:-${DATAROOT:?}/${RUN:?}${storm_num:?}_${cyc:?}_${envir:?}_${hafs_ver:?}}
mkdir -p ${WORKhafs}
cd ${WORKhafs}

if [ "${RUN_ENVIR^^}" != "NCO" ]; then
  # Detect platform and load modules if not run by NCO
  source ${USHhafs}/hafs_pre_job.sh.inc
  export machine=${WHERE_AM_I:-wcoss2}
  module list
else
  # Run by NCO on WCOSS2
  export machine=${WHERE_AM_I:-wcoss2}
  # Prepend $USHhafs to python package search path
  export PYTHONPATH=$USHhafs${PYTHONPATH:+:$PYTHONPATH}
  # Initialize PDY
  setpdy.sh # only needed for the launch job
  # Get PDY
  source ${WORKhafs}/PDY
fi

source ${USHhafs}/hafs_runcmd.sh.inc

env

export COMhafs=${COMhafs:-$(compath.py ${envir}/${NET}/${hafs_ver})/${RUN}.${PDY}/${cyc}}
export CONFhafs=$COMhafs/$stormlabel.conf
export HOLDVARS=${HOLDVARS:-$COMhafs/$stormlabel.holdvars.txt}

#source ${HOLDVARS} # This launch job generates HOLDVARS

export YMDH=${YMDH:-$PDY$cyc}
YMDHm6=$($NDATE -6 $YMDH)
export COMOLD=${COMOLD:-$(compath.py ${envir}/${NET}/${hafs_ver})/$RUN.${YMDHm6:0:8}/${YMDHm6:8:2}}
export COMIN=${COMIN:-$(compath.py ${envir}/${NET}/${hafs_ver})/$RUN.$PDY/$cyc}
export COMOUT=${COMOUT:-$(compath.py ${envir}/${NET}/${hafs_ver})/$RUN.$PDY/$cyc}

export COMINnhc=${COMINnhc:-${DCOMROOT}/nhc/atcf/ncep}
export COMINjtwc=${COMINjtwc:-${DCOMROOT}/${PDY}/wtxtbul/storm_data}
export COMINobs=${COMINobs:-$(compath.py ${envir}/obsproc/${obsproc_ver})}
export COMINgfs=${COMINgfs:-$(compath.py ${envir}/gfs/${gfs_ver})}
export COMINgdas=${COMINgdas:-$(compath.py ${envir}/gfs/${gfs_ver})}
export COMINarch=${COMINarch:-$(compath.py ${envir}/gfs/${gfs_ver})/syndat}
export COMINrtofs=${COMINrtofs:-$(compath.py ${envir}/rtofs/$rtofs_ver)}
export COMINmsg=${COMINmsg:-$(compath.py ${envir}/${NET}/${hafs_ver})/inp${RUN}}

# Decide whether this storm runs
abort_reason=""  # if non-empty, storm does not run
what="message$storm_num" # what storm is being run or aborted?
messagefile=$COMINmsg/message$storm_num

if [ -s $COMINmsg/nstorms ]; then
  nstorms=$( head -1 $COMINmsg/nstorms ) # number of storms: 0-7
fi
if [ -s $messagefile ]; then
  message=$( head -1 "$messagefile" ) # full message contents
fi

# If nstorms file is missing, use message contents to decide:
nstorms=${nstorms:-7}

if [[ ! ( "$storm_num" -le "$nstorms" ) ]]; then
  abort_reason="I am storm $storm_num but only $nstorms storms are requested."
elif [[ -z "$message" ]]; then
  abort_reason="Message file empty or missing: $messagefile"
else
  #000000000011111111112222222222333333333344444444445555555555
  #012345678901234567890123456789012345678901234567890123456789
  #NHC  13L LAURA     200825 1200 233N 0863W 290 077 0990 1008 ...
  center=$( echo ${message:0:4} ) # echo strips the trailing space in "NHC "
  storm=${message:5:3} # 13L
  basin1=${message:7:1} # L
  name=$( echo ${message:9:9} ) # echo strips the trailing spaces
  mYMDH=${PDY:0:2}${message:19:6}${message:26:2} # 2020082512
  what="$storm $name"
  if [[ "$mYMDH" != "$PDY$cyc" ]]; then
    abort_reason="Message file cycle $mYMDH is not current cycle $PDY$cyc."
  fi
  # Under NCO environment, HFSB only runs NHC/CPHC storms
  if [[ "${RUN_ENVIR^^}" = "NCO" ]] && [[ "${RUN^^}" = "HFSB" ]] && [[ "$center" != "NHC" ]]; then
    abort_reason="The storm in $message file is not a NHC storm for ${RUN^^} to run."
  fi
fi

if [[ -n "$abort_reason" ]]; then
  ecflow_client --label $stormlabel "$what: canceled: $abort_reason"
  ecflow_client --event NoStorm
  exit 0 # End processing here.
  # Note: no "--complete recursive" because of new complete expressions in def
else
  ecflow_client --label $stormlabel "$what: run $center storm"
fi

# Create DATA dir
export DATA=${WORKhafs}/launch

if [ "${SCRUBDATA:-YES}" = "YES" ]; then
  rm -rf $DATA
fi
mkdir -p $DATA
cd $DATA

# Set extra config options here
# NOTE: None needed yet.
HAFS_LAUNCH_EXTRA_CONFIG="${HOMEhafs}/parm/${RUN}.conf"
configmore=" ${HAFS_LAUNCH_EXTRA_CONFIG:-}"

# Execute ex-script
${HOMEhafs}/scripts/exhafs_launch.py "$PDY$cyc" "$storm" FORECAST $PARMhafs $configmore
export err=$?; err_chk

# Cleanup DATA dir
cd ${WORKhafs}
if [ "${KEEPDATA:-YES}" != "YES" ]; then
  rm -rf $DATA
fi

date