# -u/bin/sh

$SMSBIN/smsinit $LOADL_STEP_ID

set -xa

###### 02/02/2004 ####################################################
echo "------------------------------------------------"
echo "XXXX -  HRLYMON"
echo "------------------------------------------------"
echo "Author: V. Krishna Kumar      "
echo "NCO/Data Management & Quality Assessment Branch "
echo "History: SEP 2004 - First implementation of this new script."
echo "This script should be used for the very first time to create "
echo "the relevant files for $NET=hourly and ruc for PDY (today)" 
echo "and cyc (current hour) to create /com/realtime/prod/$NET.${PDY}" 
echo "as well as /com/realtime/prod/$NET.${PDYm1}"
echo "for PDYm1 (yesterday) with the previous (24 - current hour cyc) "  
echo "cyc directories. If for any reason, the hourly/ruc cycle gets broken"
echo "in the production this should script should be run to create the"
echo "relevant files for the current $PDY and $PDYm1 in the appropriate"
echo "directories"
#####################################################################

####################################
# obtain unique process id (pid) and make temp directory
####################################
#
#Give the current cyc hour
#
export cyc=$1
export job=startup
export pid=$$
#
# Give the model NET - either hourly or ruc
#
export NET=$2
export cycle=t${cyc}z

##########################
# Specify RUN Name
##########################
export RUN=realtime

export DATA=/tmpnwprd/${job}.${pid}
mkdir -p $DATA
cd $DATA

#############################
# Set up the UTILITIES & scripts
##############################
export HOMESCRIPTS=/nw${envir}/scripts
export ushscript=/nw${envir}/ush
export utilscript=/nwprod/util/ush
export utilparm=/nwprod/util/parm
export utilexec=/nwprod/util/exec
#############################
# SENDCOM  - Copy files to /com directory
export SENDCOM=YES
#############################

##############################
# Run setup to initialize working directory and utility scripts
##############################
sh $utilscript/setup.sh

##############################
# Run setpdy and initialize PDY variables
##############################
sh $utilscript/setpdy.sh
. PDY
export COMIN=/com/${RUN}/${envir}/${NET}.${PDY}
export COMOUT=/com/web/${envir}/${RUN}/${NET}/${cycle}
export COM1OUT=/com/web/${envir}/${RUN}
mkdir -p $COMIN
mkdir -p $COMOUT

###
### Today's file creation follows
###
hour=$cyc

export SWITCH=YES
export todaysdate=$PDY
if [ $hour -gt 00 ]; then
   ibeg=`expr 0 - 1`
   iend=$hour
else
   ibeg=`expr $hour - 1`
   iend=$hour
fi
while [ "$ibeg" -lt "$iend" ]
do
   ibeg=`expr $ibeg + 1`
   if [ "$ibeg" -lt 10 ]; then
      ibeg='0'$ibeg
   fi

   export cyc=$ibeg
   msg="Running $cyc Z JHRLY_REALTIME.sms.prod script for $NET for $todaysdate"
   sh $HOMESCRIPTS/JHRLY_REALTIME.sms.prod $todaysdate
done

scycle=t${ibeg}z
echo "Starting cycle is $scycle"

###
### Yesterday's file creation follows
###
export yesterdaysdate=$PDYm1
   ibeg=`expr $hour - 1`
   idif=`expr $iend - $hour + 1`
   iend=`expr 24 - $idif`
while [ "$ibeg" -lt "$iend" ]
do
   ibeg=`expr $ibeg + 1`
   if [ "$ibeg" -lt 10 ]; then
      ibeg='0'$ibeg
   fi

   export cyc=$ibeg
   msg="Running $cyc Z JHRLY_REALTIME.sms.prod script for $NET $yesterdaysdate"
   sh $HOMESCRIPTS/JHRLY_REALTIME.sms.prod $yesterdaysdate
done

###
### Copies the $PDY $cyc of the current hour html summary file as 
### body.hrly.html to $COMOUT directory
###
if test "$SENDCOM" = 'YES'
then
  if [ $NET = "hourly" ] ; then
     cp $COMIN/hly_${scycle} $COM1OUT/hly_latest
     cp $COMIN/hly_sumtable_${scycle} $COM1OUT/hly_sumtable
###
### for startup only to generate ${NET}_latest files
###
     cp $COM1OUT/hly_latest $COM1OUT/ruc_latest
     cp $COM1OUT/hly_latest $COM1OUT/nam_latest
     cp $COM1OUT/hly_latest $COM1OUT/gfs_latest  
     cp $COM1OUT/hly_latest $COM1OUT/gds_latest
  else
     cp $COMIN/ruc_${scycle} $COM1OUT/ruc_latest
     cp $COMIN/ruc_sumtable_${scycle} $COM1OUT/ruc_sumtable
###
### for startup only to generate ${NET}_latest files
###
     cp $COM1OUT/ruc_latest $COM1OUT/hly_latest
     cp $COM1OUT/ruc_latest $COM1OUT/nam_latest
     cp $COM1OUT/ruc_latest $COM1OUT/gfs_latest
     cp $COM1OUT/ruc_latest $COM1OUT/gds_latest
###
  fi
fi
#
exit
