#!/bin/sh
###############################################################################
#                                                                             #
# This script finds the most recent restart file for a wave model and         #
# determines the corresponding initial time of the model.                     #
#                                                                             #
# Remarks :                                                                   #
#  - The ymdh of corresponding to the restart file is copied to wavestart.out #
#  - Note that the start time is offset to assure that the hindcasts use      #
#    final FGS products only.                                                 #
#  - This script is expected to operate quietly.                              #
#                                                                             #
#  Update record :                                                            #
#                                                                             #
# - Origination:                                               01-Mar-2007    #
#                                                                             #
# Last update : 02-29-2012                                                    #
#                                                                             #
###############################################################################
#
# --------------------------------------------------------------------------- #
# 0.  Preparations
#     Check necessary export variables.

  # set execution trace prompt.  ${0##*/} adds the script's basename
  PS4=" \${SECONDS} ${0##*/} L\${LINENO} + "
  set -x

  # Use LOUD variable to turn on/off trace.  Defaults to YES (on).
  export LOUD=${LOUD:-YES}; [[ $LOUD = yes ]] && export LOUD=YES
  [[ "$LOUD" != YES ]] && set +x

  if [ -z "$YMDH" ] || [ -z "$comdirIn" ] || [ -z "$modID" ] ||\
     [ -z "$utilexec" ] || [ -z "$nback" ] || [ -z "$RUN" ]
  then
    set +x
    echo ' '
    echo '*******************************************************'
    echo '*** EXPORTED VARIABLES IN multiwavestart.sh NOT SET ***'
    echo '*******************************************************'
    echo ' '
    [[ "$LOUD" = YES ]] && set -x
    postmsg "$jlogfile" "EXPORTED VARIABLES IN multiwavestart.sh NOT SET"
    exit 1
  fi

  off_hour=3
  stp_hour=6

# --------------------------------------------------------------------------- #
# 1.  Loop to find file

  ymdh=`$utilexec/ndate +${stp_hour} ${YMDH}`
  iback=0

  while [ "$iback" -le "$nback" ]
  do
    ymdh=`$utilexec/ndate -${stp_hour} ${ymdh}`
    date=`echo $ymdh | cut -c 1-8`
    cycle=t`echo $ymdh | cut -c 9-10`z

    dir=${comdirIn}/${RUN}.${date}

    if [ -d "$dir" ]
    then
      set +e
      nr=`ls ${dir}/${modID}.*.${cycle}.restart 2> /dev/null | wc -l | awk '{print $1}'`
      set -e
      if [ "$nr" -gt '0' ]
      then
        iback=${nback}
      fi
    fi

    iback=`expr $iback + 1`

  done

# --------------------------------------------------------------------------- #
# 2.  Write file

  ymdh=`$utilexec/ndate -${stp_hour} ${ymdh}`
  ymdh=`$utilexec/ndate -${off_hour} ${ymdh}`
  echo $ymdh > multiwavestart.out

# End of multiwavestart.sh -------------------------------------------------- #
