#! /bin/ksh
#
# Script name:         exrap_pmgr.sh.sms
#
#  This script monitors the progress of the rap_fcst job
#

set -x

cd $DATA

export PS4='$SECONDS + '

hour=00
typeset -Z2 hour
TEND=18
TCP=19

if [ -e posthours ]; then
   rm -f posthours
fi

while [ $hour -lt $TCP ]; 
do
  echo $hour >>posthours
     let "hour=hour+1"
done
postjobs=`cat posthours`

# Compute date & time components for the forecast
START_TIME=${START_TIME:-$PDY$cyc}
syyyy=`echo ${START_TIME} | cut -c1-4`
smm=`echo ${START_TIME} | cut -c5-6`
sdd=`echo ${START_TIME} | cut -c7-8`
shh=`echo ${START_TIME} | cut -c9-10`

#
# Wait for all fcst hours to finish 
#
icnt=1
while [ $icnt -lt 1000 ]
do
  for fhr in $postjobs
  do
    if [ -s $FCSTDIR/fcstdone${fhr}.${shh} ]
    then
      $SMSBIN/setev release_post${fhr}
      # Remove current fhr from list
      postjobs=`echo $postjobs | sed s/${fhr}//g`
    fi
  done
  
  result_check=`echo $postjobs | wc -w`
  if [ $result_check -eq 0 ]
  then
     break
  fi

  sleep 10
  icnt=$((icnt + 1))
  if [ $icnt -ge 180 ]
  then
    msg="ABORTING after 30 minutes of waiting for RAP FCST hours $postjobs."
    err_exit $msg
  fi

done

echo Exiting $0

exit
