#!/bin/csh -f
#
# modhtml1.bisagni script
# 5/23/95 RCG
# From G. Flierl, 2/16/95
# Used with defexec method for serving satellite images.  Script
# generates the list of available satellite images, according to
# their dates (after preselecting by type based on passes parameter
# $1).  The user will see each line as a hypertext link.  When
# clicked, the cgi that is called will convert the specified image
# to gif format and pass to caller.
#


set image_type=$1
#echo "image_type = $image_type"
set initpath="/data/jbisagni/avhrr"
if (! -e $initpath ) then
  echo " *Cannot continue with displaying satellite image view"
  echo "$initpath is not directory or does not exist"
  exit
endif

cd $initpath
#echo "Change directory to: `pwd`"

echo "#"
echo "file_date[year month hrmnss]"

# scan for year and then scan within each of these subdirectories

set years=`ls -1p | grep "/" `
#echo "years = $years"

if ( ${#years} == 0) then
  echo " no year subdirectories data"
else

  foreach year ($years)
# echo "year = $year"

# Check for directory validity
    if (-d $year) then
      set months=`ls -1p $year | grep "/" `
#     echo "months = $months"

# Find months
      if (${#months} > 0 ) then
        foreach month ($months)
#       echo "month = $month"
          set files=$initpath/$year$month${image_type}*.nav.Z
#         echo "files = $files"
          foreach file ($files)
#           echo "file = $file"
            set filetail=$file:t
#           echo "filetail = $filetail"
            set year_day=`echo $filetail | awk '{print substr($filetail,4,3)}' `
#           echo "year_day = $year_day"
            set time=`echo $filetail | awk '{print substr($filetail,7,6)}' `
#           echo "time = $time"
            set file_date=$year"  "$month"  "$time
#           echo "file_date(before substitution) = $file_date"
            set file_date=`echo $file_date | sed 's/\// /g'`
#           echo "file_date = $file_date"
#           echo " "
#           echo " "
            echo \"\<a href=\"http://globec.whoi.edu/jg/dsp2gif\?${file}\"\>$file_date\</a\>\"
          end
        end
      endif
    else
      echo "Directory $year not valid"
    endif
  end
endif
