#! /bin/sh
#----------------------------------------------------------------------
# This script edits the satellite image monthly archive page.
#
# Usage:
#
#   satimgs_month_arch.edit.sh [-help] [-override]
#
# where:
#
#   -help        prints this usage message and exits.
#
#   -override    forces the script to run even it it isn't late on the 
#                  last day of the PDT or PST month (the begining of
#                  the UTC year, 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
#----------------------------------------------------------------------
archdir=/data/crws/archive
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

# Remove the existing monthly satellite menu file.
#
if [ -r satimgs_month_arch.html ]; then
#   curmonth=`date -u +%b`
#   curyear=`date -u +%Y`
#  if [ -r satimgs_month_arch.${curyear}${curmonth}.html ]; then
     \rm satimgs_month_arch.html 
#  else
#     mv satimgs_month_arch.html satimgs_month_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/satimgs_month_arch.template.${piececount}.html ]; then

   echo "Modifying/creating satimgs_month_arch.html"
   cp -p pieces/satimgs_month_arch.template.${piececount}.html satimgs_month_arch.html
fi

# Cycle through satellite image types, adding links to them.
#
sattypes="gwvis gwir gevis geir mwir"

for sattype in ${sattypes}; do

# Calculate the number of months available in the archive.
#
  if [ "${sattype}" = "mwir" ]; then

    if [ -r files.list ]; then
       \rm files.list
    fi
     ls ${archdir}/mw/ir/*/* > 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

  else

    if [ -r files.big.list ]; then
       \rm files.big.list
    fi
    touch files.big.list

    yymms=`ls ${archdir}/satimgs/${sattype}/big/ | grep -v MONTH`

   for yymm in ${yymms}; do
     ls ${archdir}/satimgs/${sattype}/big/${yymm}/*/* | awk -F\/ '{print$NF}' | sort | head -1 >> files.big.list
   done

   if [ -r files.small.list ]; then
      \rm files.small.list
   fi
    touch files.small.list

    yymms=`ls ${archdir}/satimgs/${sattype}/small/ | grep -v MONTH`

   for yymm in ${yymms}; do
      ls ${archdir}/satimgs/${sattype}/small/${yymm}/*/* | awk -F\/ '{print$NF}' | sort | head -1 >> files.small.list
   done

     if [ -r files.list ]; then
        \rm files.list
     fi

     cat files.big.list | sort | head -1 | awk -F\/ '{print$NF}' > files.list 
     cat files.small.list | sort | head -1 | awk -F\/ '{print$NF}' >> files.list 
     start=`cat files.list | sort | head -1 | cut -c 1-8`
    if [ -r files.list ]; then
       \rm files.list
    fi

     cat files.big.list | tail -1 | awk -F\/ '{print$NF}' > files.list 
     cat files.small.list | tail -1 | awk -F\/ '{print$NF}' >> files.list 
     last=`cat files.list | sort | tail -1 | cut -c 1-8`
    if [ -r files.big.list ]; then
       \rm files.big.list
    fi
    if [ -r files.small.list ]; then
       \rm files.small.list
    fi
    if [ -r files.list ]; then
       \rm files.list
    fi

  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 ${sattype} images: yymm = ${yymmstart} to ${yymm}."

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

     yyyy=20${yy}
     echo "               <EM>${yyyy}</EM>" >> satimgs_month_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
  
      if [ "${sattype}" = "mwir" ]; then
         echo "          <LI> <A HREF="'"'"http://virga.sfsu.edu/pub/mw/ir/${yymm}"'"'">${mmname}, ${yyyy}</A></LI>" \
            >> satimgs_month_arch.html
      else
         echo "          <LI> <A HREF="'"'"${sattype}_${mmname}_${yyyy}_arch.html"'"'">${mmname}, ${yyyy}</A></LI>" \
            >> satimgs_month_arch.html
      fi

# 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>" >> satimgs_month_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/satimgs_month_arch.template.${piececount}.html \
      >> satimgs_month_arch.html

# End of loop through satellite image types.
#
done

# Append CRWS main menu.
#
cat pieces/mainmenu.html >> satimgs_month_arch.html

chmod 664 satimgs_month_arch.html

