#!/bin/sh
###############################################################################
#                                                                             #
# This script finds the most recent restart file for a wave model.            #
#                                                                             #
# Remarks :                                                                   #
#  - The ymdh of the restart file is copied to wavestart.out                  #
#  - This script is expected to operate quietly.                              #
#                                                                             #
#                                                        August, 2002         #
#                                                                             #
###############################################################################
#
# --------------------------------------------------------------------------- #
# 0.  Preparations
#     Check necessary export variables.

  if [ -z "$YMDH" ] || [ -z "$com" ] || [ -z "$modID" ] ||\
     [ -z "$utilexec" ] || [ -z "$nback" ] || [ -z "$RUN" ]
  then
    echo ' '
    echo '**************************************************'
    echo '*** EXPORTED VARIABLES IN wavestart.sh NOT SET ***'
    echo '**************************************************'
    echo ' '
    postmsg "$jlogfile" "EXPORTED VARIABLES IN wavestart.sh NOT SET"
    exit 1
  fi

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

  ymdh=$YMDH
  iback=1
  mback=`expr $nback + $nback`

  while [ "$iback" -le "$mback" ]
  do
    ymdh=`$utilexec/ndate -6 ${ymdh}`
    pdate=`echo $ymdh | cut -c 1-8`
    pcycle=t`echo $ymdh | cut -c 9-10`z

    dir=${com}/${RUN}.${pdate}
    file=${modID}.${pcycle}.next_restart

    if [ -d "$dir" ]
    then
      if [ -f "${dir}/$file" ]
      then
        iback=${mback}
      fi
    fi

    iback=`expr $iback + 1`

  done

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

  echo $ymdh > wavestart.out

# End of wavestart.sh ------------------------------------------------------- #
