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

set -x

export PS4='$SECONDS + '
export tmmark=tm00

hour=00
typeset -Z2 hour

if [ $domain = firewx ]
then
  TEND=36
  TCP=37
  TEND_HOURLY=36
else
  TEND=60
  TCP=61
  TEND_HOURLY=60
fi

if [ -e posthours_${numdomain} ]; then
   rm -f posthours_${numdomain}
fi

while [ $hour -lt $TCP ]; 
do
  echo $hour >>posthours_${numdomain}
  let "hour=hour+1"
done

postjobs=`cat posthours_${numdomain}`

#
# Wait for all fcst hours to finish 
#
icnt=1
while [ $icnt -lt 1500 ]
do
  for fhr in $postjobs
  do
    if [ -s $FCSTDATA/fcstdone.${numdomain}.0${fhr}h_00m_00.00s ]
    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 720 ]
  then
    msg="ABORTING after 2.0 hours of waiting for Nam ${domain}nest FCST hours $postjobs."
    err_exit $msg
  fi

done

echo Exiting $0

exit
