#! /bin/sh
#----------------------------------------------------------------------
# This script edits the 700 mb temperature Mollweide map archive page.
#
# Usage:
#
#   temp700mw_arch.edit.sh [-help] [-override]
#
# where:
#
#   -help        prints this usage message and exits.
#
#   -override    forces the script to run even if it isn't the 
#                  first day of the UTC month (after at least one image 
#                  has arrived).
#----------------------------------------------------------------------
# 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
override=""

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

     -over*)
        override=yes
        shift
     ;;

     *)
        echo "Parameter $1 not recognized; 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

arch=temp700mw

# 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

# Calculate the number of months available in the archive.
#
if [ -r files.list ]; then
   \rm files.list
fi
yymmdirs=`/bin/ls /data/crws/archive/mw/temperature_700 | grep -v MONTH` 
touch files.list
for yymmdir in ${yymmdirs}; do
   ls /data/crws/archive/mw/temperature_700/${yymmdir}/* >> files.list
done
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}
 mm=01
 yymmstart=${yystart}${mmstart}
 mmend=${mmlast}

echo "   Defining links for ${arch} images: 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

     yyyy=20${yy}

     echo "          <LI> <A HREF="'"'"http://virga.sfsu.edu/pub/mw/temperature_700/${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

# Append CRWS main menu.
#
cat pieces/mainmenu.html >> ${arch}_arch.html
chmod 664 ${arch}_arch.html

