# This script generates the date files on the IBM.  The files are Y2K 
# compliant, containing a 4 digit year.  They are put in $NEW_DIR.  
# The $CYL indicates which cycle (00Z or 12Z) files to generate.  
#
# The algorithm used to generate the date/time is as follows:
#  12Z cycle:  The system clock is used for the year month and day.
#              (not too complicated)
#  00Z cycle:  (now the fun stuff)  The system clock is checked to 
#              determine if we should use todays date or tomorrows.
#              if the GMT time is > 20Z then we use tomorrows date.
#              To determine tomorrows date we simply add one to the
#              current day.  However as we all know we must now deal
#              with the end of the month and end of the year, not to
#              mention leap years.....  This is all handled in the case
#              statement below.
#              The case statement basically says:  if the MMDD string
#              matches the end of the month plus one day, set them to the
#              next month day 1.  We also handle the new year when the 
#              month is 12. 


# handle the 12Z cycle first, since it is pretty easy
if test "$cyc" = "12"
then
  for HH in 12 13 14 15 16 17 18 19 20 21 22 23
  do
    echo `date -u +"DATE\040  %Y%m%d${HH}0000WASHINGTON\040\040"` > $NEW_DIR/t${HH}z
  done
elif test "$cyc" = "00"
  # now deal with 00Z
then
  HH=`date -u +%H`
  DD=`date -u +%d`
  MM=`date -u +%m`
  YY=`date -u +%y`
  YYYY=`date -u +%Y`
  # do we want todays date or tomorrows?
  if test $HH -gt 20 
  then
    DD=`expr $DD + 1`
    if test "$DD" -lt '10'
    then
      DD=0$DD
    fi
  fi
  # use a varaible for FEB so we can deal with leap years.
  FEB="0229"
  LEAP=`expr $YYYY % 4`
  if test "$LEAP" -eq 0
  then
    FEB="0230"
  fi
#  echo "DD=$DD, FEB= $FEB"

  # now check for the end of month and end of year wrap
  case "${MM}${DD}" in
  "0132")         # Jan 32?
      DD=01
      MM=02
      ;;
  "$FEB")         # Fep 29 (or 30 if leap year)?
      DD=01
      MM=03
      ;;
  "0332")         # Mar 32?
      DD=01
      MM=04
      ;;
  "0431")         # Apr 31?
      DD=01
      MM=05
      ;;
  "0532")         # May 32?
      DD=01
      MM=06
      ;;
  "0631")         # Jun 31?
      DD=01
      MM=07
      ;;
  "0732")         # Jul 32?
      DD=01
      MM=08
      ;;
  "0832")         # Aug 32?
      DD=01
      MM=09
      ;;
  "0931")         # Sep 31?
      DD=01
      MM=10
      ;;
  "1032")         # Oct 32?
      DD=01
      MM=11
      ;;
  "1131")         # Nov 31
      DD=01
      MM=12
      ;;
  "1232")         # Dec 32?
      DD=01
      MM=01
      YY=`expr $YY + 1`
      YYYY=`expr $YYYY + 1`
      ;;
   esac

  for HH in 00 01 02 03 04 05 06 07 08 09 10 11
  do
    echo "DATE  ${YYYY}${MM}${DD}${HH}0000WASHINGTON  " > ${NEW_DIR}/t${HH}z
  done
else
  echo "Invalid cycle time"
  exit -1
fi

exit 0

