#! /bin/sh
#----------------------------------------------------------------------
# This script edits the six jet stream archive pages.
#
# Usage:
#
#   jet_arch.edit.sh [-help] [jettypes] [-override] 
#
# where:
#
#   -help        prints this usage message and exits.
#
#    jettype     is one or more of the types of jet stream maps, 
#                  including (default is all of them):
#                     jet_nh jet_sh
#                     jet jet_pac jet_atl
#                     jetsat jetsat_pac jetsat_atl
#
#   -override    forces the script to run even if it isn't the 
#                  first day of the UTC (after at least one image 
#                  should have been deposited in the archive).
#----------------------------------------------------------------------
# Define environment variables.
#
PATH=./:/usr/local/bin:/usr/local/unidata/wxp/bin:/usr/local/unidata/wxp/scripts:/usr/local/unidata/ldm/scripts:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/X11R6/bin:/opt/local/bin:/sw/bin:/sw/sbin
export PATH
#----------------------------------------------------------------------
scriptdir=/Library/WebServer/Documents/crws/archive
cd $scriptdir

archs=""
override=""

while [ -n "$1" ]; do
  case $1 in
    -h* | help)
        cat ${scriptdir}/`echo $0 | cut -c 4-` | head -23 | tail -22
        exit
    ;;

    -over*)
       override=yes
       shift
    ;;

    jet_nh | jet_sh | jet | jet_pac | jet_atl | jetsat | jetsat_pac | jetsat_atl)
       archs="${archs} $1"
       shift
    ;;

    jet_norhem | jetstream_norhem)
       archs="${archs} jet_nh"
       shift
    ;; 

    jet_sohem | jetstream_sohem)
       archs="${archs} jet_sh"
       shift
    ;; 

    jetstream)
       archs="${archs} jet"
       shift
    ;; 
    
    jetstream_pac)
       archs="${archs} jet_pac"
       shift
    ;; 

    jetstream_atl)
       archs="${archs} jet_atl"
       shift
    ;; 

    *)
       echo "Do not understand parameter $1; skipping it."
       shift
    ;; 
  esac
done

# Check to see if it's the last day of the UTC month. If it's not, exit 
# unless override=yes is set.
#
message=""
if [ -z "${override}" ]; then

   month=`date -u +%m`
   day=`date -u +%d`
   year=`date -u +%Y`

  if [ ${day} -ne 1 ]; then
     message="  Not first day of the UTC month. Exiting."
     echo $message
     exit
  fi
fi

if [ -z "${archs}" ]; then
   archs="jet_nh jet_sh jet jet_pac jet_atl jetsat jetsat_pac jetsat_atl"
fi
#
# Cycle through jet stream maps, adding links to them.
#

for arch in ${archs}; do
#
# Remove the existing monthly menu file.
#
  if [ -r ${arch}_arch.html ]; then
#     curmonth=`date -u +%b`
#     curyear=`date -u +%Y`
#    if [ -r ${arch}_arch.${curyear}_${curmonth}.html ]; then
       \rm ${arch}_arch.html 
#    else
#       mv ${arch}_arch.html ${arch}_arch.${curyear}${curmonth}.html
#    fi
  fi
#
# Copy the first piece of HTML template to start a new version of the 
# monthly menu file.
#
   piececount=1
  if [ -r pieces/${arch}_arch.template.${piececount}.html ]; then
     echo "Modifying/creating ${arch}_arch.html"
     cp -p pieces/${arch}_arch.template.${piececount}.html ${arch}_arch.html
  fi

  case ${arch} in
     jet)
        subdirs="jetstream/big jetstream/small"
     ;;
     jet_pac)
        subdirs="jetstream_pac/big jetstream_pac/small"
     ;;
     jet_atl)
        subdirs="jetstream_atl/big jetstream_atl/small"
     ;;
     jetsat)
        subdirs="jetsat/www jetsat/small"
     ;;
     jetsat_pac)
        subdirs="jetsat_pac/www jetsat_pac/small"
     ;;
     jetsat_atl)
        subdirs="jetsat_atl/www jetsat_atl/small"
     ;;
     jet_nh)
        subdirs="jetstream_norhem"
     ;;
     jet_sh)
        subdirs="jetstream_sohem"
     ;;
  esac
#
# Calculate the number of months available in the archive.
#
  for subdir in ${subdirs}; do

    if [ -r files.list ]; then
       \rm files.list
    fi
     yymmdirs=`ls /data/crws/archive/jetstream/${subdir} | grep -v MONTH` 
    for yymmdir in ${yymmdirs}; do
       ls /data/crws/archive/jetstream/${subdir}/${yymmdir}/* >> files.list
    done
#     ls /data/crws/archive/jetstream/${subdir}/*/* > files.list
     start=`cat files.list | head -1 | awk -F\/ '{print$NF}' | cut -c 1-8`
     last=`cat files.list |  tail -1 | awk -F\/ '{print$NF}' | cut -c 1-8`
    if [ -r files.list ]; then
       \rm files.list
    fi

     yystart=`echo ${start} | cut -c 1-2`
     mmstart=`echo ${start} | cut -c 3-4`
     mmlast=`echo ${last} | cut -c 3-4`
     yylast=`echo ${last} | cut -c 1-2`

     yymm=${yylast}${mmlast}
     yy=${yylast}
     yymmstart=${yystart}${mmstart}
     mmend=${mmlast}
    if [ ${yystart} = ${yylast} ]; then
       mm=${mmstart}
    else
       mm=01
    fi

echo "   Defining links for ${arch} images in ${subdir}: yymm = ${yymmstart} to ${yymm}."

    while [ ${yy} -ge ${yystart} ]; do

       yyyy=20${yy}
       echo "               <EM>${yyyy}</EM>" >> ${arch}_arch.html

      while [ ${mm} -le ${mmend} ]; do

         yymm=${yy}${mm}

        case ${mm} in
          01)
            mmname=Jan
          ;;
          02)
            mmname=Feb
          ;;
          03)
            mmname=Mar
          ;;
          04)
            mmname=Apr
          ;;
          05)
            mmname=May
          ;;
          06)
            mmname=Jun
          ;;
          07)
            mmname=Jul
          ;;
          08)
            mmname=Aug
          ;;
          09)
            mmname=Sep
          ;;
          10)
            mmname=Oct
          ;;
          11)
            mmname=Nov
          ;;
          12)
            mmname=Dec
          ;;
        esac
    
         echo "          <LI> <A HREF="'"'"http://virga.sfsu.edu/pub/jetstream/${subdir}/${yymm}"'"'">${mmname}, ${yyyy}</A></LI>" \
              >> ${arch}_arch.html
#
# Put a break following the end of data for each year (except the 
# earliest one).
#  
        if [ ${mm} = ${mmend} -a ${yy} -ne ${yystart} ]; then
           echo "<BR>" >> ${arch}_arch.html
        fi
#
# Increment the month.
#    
         mm=`expr ${mm} + 1`
        if [ ${mm} -lt 10 ]; then
           mm=0${mm}
        fi 

      done
#
# Decrement the year.
#
       yy=`expr ${yy} - 1`
      if [ ${yy} -lt 10 ]; then
         yy=0${yy}
      fi
#
# Reinitialize the month counter for the current year.
#
      if [ ${yy} -eq ${yystart} ]; then
         mm=${mmstart}
      else
         mm=01
      fi
#
# Set the last month of the current year for which there are data.
#
       mmend=12
#
# End of loop through years.
#
    done
#
# Increment the HTML code piece counter and append next piece of HTML 
# code.
#
     piececount=`expr ${piececount} + 1`
     cat pieces/${arch}_arch.template.${piececount}.html \
        >> ${arch}_arch.html
# 
# End of loop through jetstream map subdirectories.
#
  done
#
# Append CRWS main menu.
#
   cat pieces/mainmenu.html >> ${arch}_arch.html
   chmod 664 ${arch}_arch.html
#
# End of loop through jet stream map types.
#
done

