#!/bin/ksh
######################################################
echo "-----------------------------------------------" 
echo "     exhwrf_pre_master.sh.sms                  "
echo "   Hurricane model control script              "
echo "-----------------------------------------------" 
######################################################
# This script is based on the GFDL model exgfdl_pre_master.sh.sms
# script written by Tim Marchok.   
# Mar 2007 Modified for HWRF by Tim Marchok
# NCEP contact person William O'Connor 
######################################################
set -x

PS4=' + exhwrf_pre_master.sh.sms line $LINENO: ' 

cd $DATA
pwd 

if [[ "$PARAFLAG" == YES ]] ; then
    # Disable SMS stuff to avoid having non-NCO run some NCO-specific
    # commands in this file.
    SENDSMS=NO
fi

#####################################################################
#
# START FLOW OF CONTROL
#
# 1) Get the date from the environmental variables and compare it 
#    with the date in the stormdate file in $mesagdir.
#
#####################################################################

set -x
if [ ${SENDSMS} = 'YES' ]; then
NETNAME=`echo $SMSNAME | sed "s+/hwrf${storm_num}/prep/jhwrf${storm_num}_pre_master_${cyc}++g"`
echo "login -t 60 $SMSNODE $SMSNAME $SMSPASS" > cdp_cmds
fi

export kyodate=`echo $PDY$cyc | cut -c3-10`

################################################################
#    Decide how many storms to run.  Write to storm files
#    with the status of running the storm.
################################################################
if test $cyc -lt 12
then
   prodcyc=00
else
   prodcyc=12
fi

stormdate=`cat $mesagdir/stormdate`

if test "$kyodate" = "$stormdate"
then

   export nstms=`cat $mesagdir/nstorms`
   awk '{print $2 " " $3}' $mesagdir/storms.nhc > mlabel
   numlines=`wc -l mlabel | cut -c8-8`

   #################################################
   # Write to storm file to keep track of status
   #################################################
   if test -a $COMOUT/$stormlabel.$cycle
   then
      label="Rerun $stormlabel"
      echo "RERUN $stormlabel" > $COMOUT/$stormlabel.$cycle
   else
      echo "$stormlabel will run" > $COMOUT/$stormlabel.$cycle
   fi

   #################################################
   # Get proper storm number label for the input 
   # storm and then initiate SMS calls for an HWRF 
   # run.
   #################################################
   case $stormlabel in
     storm1)label=`sed 2,${numlines}d mlabel`
            if test $nstms -gt 0
            then
               msg="HWRF $stormlabel will run. SMS taking control."
               postmsg "$jlogfile" "$msg"
               if test "$SENDSMS" = "YES"
               then
                 $SMSBIN/smslabel $stormlabel "$label"
               fi
               if [ ${PARAFLAG} = 'YES' ]
               then
                  ${USHhwrf}/hwrf_pre_master_start.sh
               else
                  ${USHhwrf}/hwrf_pre_master_start.sh
#                  /nw${envir}/ush/hwrf_pre_master_start.sh
                  err=$?; echo "err= $err"
                  if [ ${err} -eq 119 ]
                  then
                    if test "$SENDSMS" = "YES"
                    then
                       # Domain in southern hemisphere will be aborted.
                         $SMSBIN/smslabel $stormlabel "$label has been cancelled"
                         SMSPASS=FREE
                         echo "force -r complete $NETNAME/hwrf1" >> cdp_cmds
                     fi
                  fi
               fi
            else
               label="No $stormlabel"
               echo $label  >$COMOUT/$stormlabel.$cycle
               if test "$SENDSMS" = "YES"
               then
                 $SMSBIN/smslabel $stormlabel "$label"
                 SMSPASS=FREE
                 #echo "login -t 60 $SMSNODE $SMSNAME $SMSPASS" > cdp_cmds
                 #echo "smslabel $stormlabel "$label" >> cdp_cmds
                 echo "force -r complete $NETNAME/hwrf1" >> cdp_cmds
               fi
            fi
            ;;
     storm2)sed 1d mlabel > tmplabel
            label=`sed 2,${numlines}d tmplabel`
            if test $nstms -gt 1
            then
               msg="HWRF $stormlabel will run. SMS taking control."
               postmsg "$jlogfile" "$msg"
               if test "$SENDSMS" = "YES"
               then
                 $SMSBIN/smslabel $stormlabel "$label"
               fi
               if [ ${PARAFLAG} = 'YES' ]
               then
                  ${USHhwrf}/hwrf_pre_master_start.sh
               else
                  ${USHhwrf}/hwrf_pre_master_start.sh
#                  /nw${envir}/ush/hwrf_pre_master_start.sh
                  err=$?; echo "err= $err"
                  if [ ${err} -eq 119 ]
                  then
                    if test "$SENDSMS" = "YES"
                    then
                       # Domain in southern hemisphere will be aborted.
                         SMSPASS=FREE
                         echo "force -r complete $NETNAME/hwrf2" >> cdp_cmds
                     fi
                  fi
               fi
            else
               label="No $stormlabel"
               echo $label  >$COMOUT/$stormlabel.$cycle
               if test "$SENDSMS" = "YES"
               then
                 $SMSBIN/smslabel $stormlabel "$label"
                 SMSPASS=FREE
                 echo "force -r complete $NETNAME/hwrf2" >> cdp_cmds
               fi
            fi
            ;;
     storm3)sed '1,2d' mlabel > tmplabel
            numlines=`wc -l tmplabel | cut -c8-8`
            label=`sed 2,${numlines}d tmplabel`
            if test $nstms -gt 2
            then
               msg="HWRF $stormlabel will run. SMS taking control."
               postmsg "$jlogfile" "$msg"
               if test "$SENDSMS" = "YES"
               then
                 $SMSBIN/smslabel $stormlabel "$label"
               fi
               if [ ${PARAFLAG} = 'YES' ]
               then
                  ${USHhwrf}/hwrf_pre_master_start.sh
               else
                  ${USHhwrf}/hwrf_pre_master_start.sh
#                  /nw${envir}/ush/hwrf_pre_master_start.sh
                  err=$?; echo "err= $err"
                  if [ ${err} -eq 119 ]
                  then
                    if test "$SENDSMS" = "YES"
                    then
                       # Domain in southern hemisphere will be aborted.
                         SMSPASS=FREE
                         echo "force -r complete $NETNAME/hwrf3" >> cdp_cmds
                     fi
                  fi
               fi
            else
               label="No $stormlabel"
               echo $label  >$COMOUT/$stormlabel.$cycle
               if test "$SENDSMS" = "YES"
               then
                 $SMSBIN/smslabel $stormlabel "$label"
                 SMSPASS=FREE
                 echo "force -r complete $NETNAME/hwrf3" >> cdp_cmds
               fi
            fi
            ;;
     storm4)sed '1,3d' mlabel > tmplabel
            numlines=`wc -l tmplabel | cut -c8-8`
            label=`sed 2,${numlines}d tmplabel`
            if test $nstms -gt 3
            then
               msg="HWRF $stormlabel will run. SMS taking control."
               postmsg "$jlogfile" "$msg"
               if test "$SENDSMS" = "YES"
               then
                 $SMSBIN/smslabel $stormlabel "$label"
               fi
               if [ ${PARAFLAG} = 'YES' ]
               then
                  ${USHhwrf}/hwrf_pre_master_start.sh
               else
                  ${USHhwrf}/hwrf_pre_master_start.sh
#                  /nw${envir}/ush/hwrf_pre_master_start.sh
                  err=$?; echo "err= $err"
                  if [ ${err} -eq 119 ]
                  then
                    if test "$SENDSMS" = "YES"
                    then
                       # Domain in southern hemisphere will be aborted.
                         SMSPASS=FREE
                         echo "force -r complete $NETNAME/hwrf4" >> cdp_cmds
                     fi
                  fi
               fi
            else
               label="No $stormlabel"
               echo $label  >$COMOUT/$stormlabel.$cycle
               if test "$SENDSMS" = "YES"
               then
                 $SMSBIN/smslabel $stormlabel "$label"
                 SMSPASS=FREE
                 echo "force -r complete $NETNAME/hwrf4" >> cdp_cmds
               fi
            fi
            ;;
     storm5)sed '1,4d' mlabel > tmplabel
            numlines=`wc -l tmplabel | cut -c8-8`
            label=`sed 2,${numlines}d tmplabel`
            if test $nstms -gt 4
            then
               msg="HWRF $stormlabel will run. SMS taking control."
               postmsg "$jlogfile" "$msg"
               if test "$SENDSMS" = "YES"
               then
                 $SMSBIN/smslabel $stormlabel "$label"
               fi
               ${USHhwrf}/hwrf_pre_master_start.sh
               err=$?; echo "err= $err"
               if [ ${err} -eq 119 ]
               then
                  if test "$SENDSMS" = "YES"
                  then
                     # Domain in southern hemisphere will be aborted.
                     SMSPASS=FREE
                     echo "force -r complete $NETNAME/hwrf5" >> cdp_cmds
                  fi
               fi
            else
               label="No $stormlabel"
               echo $label  >$COMOUT/$stormlabel.$cycle
               if test "$SENDSMS" = "YES"
               then
                 $SMSBIN/smslabel $stormlabel "$label"
                 SMSPASS=FREE
                 echo "force -r complete $NETNAME/hwrf5" >> cdp_cmds
               fi
            fi
            ;;
     *) ;;
   esac

   echo $label > $DATA/label

   if test "$SENDSMS" = "YES"
   then
   echo "exit" >> cdp_cmds
     $SMSBIN/cdp < cdp_cmds
   fi

else
   ##############################################
   # Condition if no storms are to be run
   ##############################################
   msg="No HWRF hurricane to run. SMS taking control."
   postmsg "$jlogfile" "$msg"
   label="No $stormlabel"
   if test "$SENDSMS" = "YES"
   then
     $SMSBIN/smslabel $stormlabel "$label"
   fi
   echo $label  >$COMOUT/$stormlabel.$cycle
   if test "$SENDSMS" = "YES"
   then
     echo "force -r complete $NETNAME/hwrf${storm_num}" >> cdp_cmds
     echo "exit" >> cdp_cmds
     $SMSBIN/cdp < cdp_cmds
   fi
fi

#####################################################################
# GOOD RUN
set +x
echo "**************JOB $job COMPLETED NORMALLY "
echo "**************JOB $job COMPLETED NORMALLY "
echo "**************JOB $job COMPLETED NORMALLY "
set -x
#####################################################################
