#! /bin/sh
#----------------------------------------------------------------------
# This script edits satellite image archive pages. By default it
# assumes that the current month and yyyy apply. If a different month
# is specified, and that month is later in the year than the current 
# month, then the previous year is assumed.
#
# Syntax is:
#
#    satimg_day_arch.edit.sh  [sattype] [-mo=month] [-yy=year]
#
# where:
#
#    sattype     is the type of satellite image (gwir, geir, gwvis, or
#                   gevis; default is all four);
#   (-mo=)month  is the month of the year (three-letter truncated; 
#                   default is the current month);
#   (-yy=)year  is the year (four digits; default is the current year).
#----------------------------------------------------------------------
# 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
#----------------------------------------------------------------------
cd /Library/WebServer/Documents/crws/archive

sattypes=""
curday=`date -u +%d`
ddcolmnlast=${curday}
curmmname=`date -u +%b`
mmfullname=`date -u +%B`
curmm=`date -u +%m`
curyyyy=`date -u +%Y`
curyy=`date -u +%y`
mm=${curmm}
mmname=${curmmname}
yyyy=${curyyyy}
yy=${curyy}
sizelist="big small"
smallonly=""
skip=""

while [ -n "$1" ]; do
  case $1 in
    gwir | geir | gwvis | gevis)
       sattypes="${sattypes} $1"
       shift
    ;; 

    -mo*=*)
       mmname=`echo $1 | awk -F= '{print$2}'`
       shift
    ;;
    -mo*)
       mmname=$2
       shift; shift
    ;;

    -year*=* | -yy=*)
       yyyy=`echo $1 | awk -F= '{print$2}'`
       yy=`echo ${yyyy} | cut -c 3-4`
       shift
    ;;
    -year* | -yy)
       yyyy=$2
       yy=`echo ${yyyy} | cut -c 3-4`
       shift; shift
    ;;

    *)
       echo "Do not understand parameter $1; skipping it."
       shift
    ;; 
  esac
done
#
# Determine the last day of the specified month.
#
case ${mmname} in
  Feb)
    if [ `expr ${yyyy} \/ 4 \* 4` -eq ${yyyy} ]; then
       ddlast=29
    else
       ddlast=28
    fi
  ;;
  Apr | Jun | Sep | Nov)
     ddlast=30
  ;;
  Jan | Mar | May | Jul | Aug | Oct | Dec)
     ddlast=31
  ;;
esac

if [ "${mmname}" != "${curmmname}" -o "${yyyy}" != "${curyyyy}" ]; then

   olddate=yes
   ddcolmnlast=${ddlast}
#
# For month and/or year other than the default, redefine values of the 
# month number, etc.
#
  case ${mmname} in
    Jan)
       mm=01
       mmfullname=January
    ;;
    Feb)
       mm=02
       mmfullname=February
    ;;
    Mar)
       mm=03
       mmfullname=March
    ;;
    Apr)
       mm=04
       mmfullname=April
    ;;
    May)
       mm=05
       mmfullname=May
    ;;
    Jun)
       mm=06
       mmfullname=June
    ;;
    Jul)
       mm=07
       mmfullname=July
    ;;
    Aug)
       mm=08
       mmfullname=August
    ;;
    Sep)
       mm=09
       mmfullname=September
    ;;
    Oct)
       mm=10
       mmfullname=October
    ;;
    Nov)
       mm=11
       mmfullname=November
    ;;
    Dec)
       mm=12
       mmfullname=December
    ;;
  esac
#
# If the specified month occurred in the previous year, set the current
# year to the previous year.
#
  if [ ${mm} -gt ${curmm} -a ${yyyy} -eq ${curyyyy} ]; then
     yyyy=`expr ${curyyyy} - 1`
     yy=`expr ${curyy} - 1`
    if [ ${yy} -le 9 ]; then
        yy=0${yy}
    fi
  fi

else
#
# The requested month and year is the current month and year. The end
# of the last column of links will be for the current day.
#
   olddate=""   

fi

fullenddays="15 ${ddlast}"

yymm=${yy}${mm}
#
# Specify default list of satellite image types.
#
if [ -z "${sattypes}" ]; then
   sattypes="gwir gwvis geir gevis"
fi
#
# Set variables for editing header panel.
#
for sattype in ${sattypes}; do
  case ${sattype} in
    gwir)
      satloc=West
      imgtype="Infrared (IR)"
      geoloc1="Eastern Pacific"
      geoloc2="Western North America"
      maploc=W_map
    ;;
    gwvis)
      satloc=West
      imgtype=Visible
      geoloc1="Eastern Pacific"
      geoloc2="Western North America"
      maploc=W.tiny
    ;;
    geir)
      satloc=East
      imgtype="Infrared (IR)"
      geoloc1="North America"
      geoloc2="North Atlantic"
      maploc=E_map
    ;;
    gevis)
      satloc=East
      imgtype=Visible
      geoloc1="North America"
      geoloc2="North Atlantic"
      maploc=E.tiny
    ;;
  esac

  if [ -n "${olddate}" ]; then
#
# Check to see whether small images exist at the requested time.
#
    if [ -r files.list ]; then
       \rm files.list
    fi
     yymmdirs=`ls /data/crws/archive/satimgs/${sattype}/small | grep -v MONTH` 
    for yymmdir in ${yymmdirs}; do
       ls /data/crws/archive/satimgs/${sattype}/small/${yymmdir}/*/* >> files.list
    done
     yymmstart=`cat files.list | sort | head -1 | awk -F\/ '{print$NF}' | cut -c 1-4`
    if [ -r files.list ]; then
       \rm files.list
    fi
    if [ ${yymm} -lt ${yymmstart} ]; then
       echo "   ${sattype} images at requested date (${yymm}) do not exist in the archive."
       skip=yes
    else
       skip=""
       ls /data/crws/archive/satimgs/${sattype}/big/${yymm}/*/* > files.list
       yymmstart_big=`cat files.list | head -1 | awk -F\/ '{print$NF}' | cut -c 1-4`
       yyyystart_big=20`echo ${yymmstart_big} | cut -c 1-2`
       mmstart_big=`echo ${yymmstart_big} | cut -c 3-4`
      if [ -r files.list ]; then
         \rm files.list
      fi
    
      if [ ${yyyy} -lt ${yyyystart_big} -o ${yyyy} -eq ${yyyystart_big} -a ${mm} -lt ${mmstart_big} ]; then
# 
# For dates before the "big" images were archived create four columns 
# of links to small images only, with 5-8 links per column (the length 
# of the last column depends on the month).
# 
         sizelist=small
         smallonly=yes
         fullenddays="08 16 24 ${ddlast}"
      fi

# End of check for existence of files at specified old date.
#
    fi
  fi

# Determine last day of the last column of links.
#
   enddays=""
   flag=0
  for fullendday in ${fullenddays}; do
    if [ ${fullendday} -lt ${ddcolmnlast} ]; then
       enddays="${enddays} ${fullendday}"
    elif [ ${flag} -eq 0 ]; then
       enddays="${enddays} ${ddcolmnlast}"
       flag=1
    fi
  done

# Establish links to files for individual days.
#
   piececount=1
  if [ -r pieces/satimgs_day_arch.template.${piececount}.html -a \
       -z "${skip}" ]; then

     filename=${sattype}_${mmname}_${yyyy}_arch.html

    if [ -r ${filename} ]; then
#       curmmname=`date -u +%b`
#       curyyyy=`date -u +%Y`
#      if [ -r satimgs_day_arch.${curyyyy}${curmmname}.html ]; then
         \rm ${filename}
#      else
#         mv satimgs_day_arch.html satimgs_day_arch.${curyyyy}${curmmname}.html
#      fi
    fi

# Copy and edit the first two pieces of HTML template to start a new 
# version of the monthly menu file.
#
     echo "   Updating/creating ${filename}"
     cat pieces/satimgs_day_arch.template.${piececount}.html | \
     sed -e "s/SATLOC/${satloc}/" \
         -e "s/IMGTYPE/${imgtype}/" \
         -e "s/SATTYPE/${sattype}/" \
         -e "s/GEOLOC1/${geoloc1}/" \
         -e "s/GEOLOC2/${geoloc2}/" \
         -e "s/MAPLOC/${maploc}/" \
       > ${filename}

     piececount=`expr ${piececount} + 1`

    if [ "${sizelist}" = "small" ] ;then
       ext=a
    else
       ext=b
    fi  
     cat pieces/satimgs_day_arch.template.${piececount}${ext}.html | \
     sed -e "s/MMFULLNAME/${mmfullname}/" \
         -e "s/YYYY/${yyyy}/" \
       >> ${filename}
#
# Loop through image sizes.'
#
    for size in ${sizelist}; do
#
# Loop through individual days, establishing a link to each.
#
       dd=01

      for ddcolmnend in ${enddays}; do

        while [ ${dd} -le ${ddcolmnend} ]; do

           echo "          <LI> <A HREF="'"'"http://virga.sfsu.edu/pub/satimgs/${sattype}/${size}/${yymm}/${dd}"'"'">${mmname} ${dd}</A></LI>" \
              >> ${filename}
#
# Increment the day.
#
           dd=`expr ${dd} + 1`
          if [ ${dd} -lt 10 ]; then
             dd=0${dd}
          fi 

        done

        if [ ${ddcolmnend} -ne ${ddcolmnlast} ]; then
# 
# Append end of HTML table column and start the next one if there is
# another column of links to be appended.
#
            echo ' '                                      >> ${filename}
            echo '        </UL>'                          >> ${filename}
            echo '      </TD>'                            >> ${filename}
            echo ' '                                      >> ${filename}
            echo '      <TD VALIGN=TOP>'                  >> ${filename}
            echo '        <UL>'                           >> ${filename}
        fi
#
# End of loop through days of the month.
#
      done
#
# Append HTML table column end, insert blank table column, and start
# next table column.
#
      if [ ${ddcolmnlast} -le 15 ]; then
        if [ "${size}" = "big" ]; then
           echo ' '                                       >> ${filename}
           echo '        </UL>'                           >> ${filename}
           echo '      </TD>'                             >> ${filename}
           echo ' '                                       >> ${filename}
           echo '      <TD>'                              >> ${filename}
           echo '        &nbsp;&nbsp;'                    >> ${filename}
           echo '      </TD>'                             >> ${filename}
           echo ' '                                       >> ${filename}
           echo '      <TD WIDTH=15%>'                    >> ${filename}
           echo '        &nbsp;&nbsp;'                    >> ${filename}
           echo '      </TD>'                             >> ${filename}
           echo ' '                                       >> ${filename}
           echo '      <TD>'                              >> ${filename}
           echo '        &nbsp;&nbsp;'                    >> ${filename}
           echo '      </TD>'                             >> ${filename}
           echo ' '                                       >> ${filename}
           echo '     <TD VALIGN=TOP>'                    >> ${filename}
           echo '       <UL>'                             >> ${filename}
        fi
      else
        if [ "${size}" = "big" ]; then
           echo ' '                                       >> ${filename}
           echo '        </UL>'                           >> ${filename}
           echo '      </TD>'                             >> ${filename}
           echo ' '                                       >> ${filename}
           echo '      <TD WIDTH=15%>'                    >> ${filename}
           echo '        &nbsp;&nbsp;'                    >> ${filename}
           echo '      </TD>'                             >> ${filename}
           echo ' '                                       >> ${filename}
           echo '     <TD VALIGN=TOP>'                    >> ${filename}
           echo '       <UL>'                             >> ${filename}
        fi
      fi
#
# End of loop through image sizes.
#
    done
#
# Append piece of HTML code template.
#
       piececount=`expr ${piececount} + 1`
       cat pieces/satimgs_day_arch.template.${piececount}${ext}.html | \
       sed -e "s/SATTYPE/${sattype}/" \
          >> ${filename}
#
# Append CRWS main menu.
#
     cat pieces/mainmenu.html >> ${filename}

     chmod 664 ${filename}

  else
    if [ -z "${skip}" ]; then
       echo "HTML template file satimgs_day_arch.template.1.html not found."
    fi
  fi
#
# End of loop through satellite image types.
#
done

