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

set -x

export PS4='$SECONDS + '

hour=00
typeset -Z2 hour
TEND=384
TCP=385

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

while [ $hour -lt $TCP ]; 
do
  echo $hour >>posthours
  if [ $hour -lt 192 ]
  then
     if [ $hour -eq 99 ]
     then
       typeset -Z3 hour
     fi
     if [ $hour -lt 12 ]
     then
       let "hour=hour+1"
     else
       let "hour=hour+3"
     fi
  else
     let "hour=hour+12"
  fi
done
postjobs=`cat posthours`

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

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

done

echo Exiting $0

exit
