#!/bin/csh -f
# make_index_htm_from_jpgs
# -------------------------------------------------------------------------
# Shell script to create an index.htm file that shows thumbnails of all
# images (JPG, GIF, PNG, etc.) in the current directory, pulling them 
# from the "thumbs" subdirectory where they are assumed to exist, and 
# having each thumbnail be a link to the full-sized image in this 
# directory.  Output is written to stdout, so you may want to redirect 
# it to index.htm.  Also, automatically prepends and appends the 
# optional files prependToIndex.htm and appendToIndex.htm.
# -------------------------------------------------------------------------
# Revision History:
# $Log$
# -------------------------------------------------------------------------

# Collect command line options
set option_reverse_string = ""
while ($#argv > 0)
    if ("$1" == "-h" || "$1" == "--help") then
        echo "Usage: $0:t [options] > index.htm"
        echo "Options:"
        echo "  -h             = Show this help text"
        echo "  --help         = Show this help text"
        echo "  -r             = Reverse sort order of images"
        echo "  --reverse      = Reverse sort order of images"
        exit 1
    else if ("$1" == "-r" || "$1" == "--reverse") then
        shift
        set option_reverse_string = "-r"
    else if ("-" == "`echo $1:q | cut -c 1`") then
        echo "Error: Invalid option: $1:q"
        $0 --help
        exit 1
    else
        # Not a recognized option.  Assume it's the first argument
        break
    endif
end
# No arguments defined, so allow none
if ($#argv > 0) then
    $0 -h
    exit 1
endif

ls *.[jJ][pP][gG] *.[gG][iI][fF] *.[pP][nN][gG] >& /dev/null
if ($status) then
    echo "No JPG, GIF or PNG files found.  Nothing to do.  Aborting..."
    exit 1
endif

# Echo the following lines while expanding dollar sign vars
# ?? Add below: <!DOCTYPE html>
cat <<EOF
<html>
 <head>
  <link rel='stylesheet' type='text/css' href='js/com_bristle_jslib_Presentation.css?version=$VERSION_NUMBER' />
  <script type='text/javascript' src='js/com.bristle.jslib.Util.js?version=$VERSION_NUMBER'></script>
  <script type='text/javascript' src='js/com.bristle.jslib.Exception.js?version=$VERSION_NUMBER'></script>
  <script type='text/javascript' src='js/com.bristle.jslib.Event.js?version=$VERSION_NUMBER'></script>
  <script type='text/javascript' src='js/com.bristle.jslib.Image.js?version=$VERSION_NUMBER'></script>
  <script type='text/javascript'>
    function onKeyPress(evt, obj)
    {
        var intKeyCode = com.bristle.jslib.Event.getEventKeyCode(evt);
        if (intKeyCode == "s".charCodeAt(0))
        {
            // Note: Use "slideshow/index.htm" here, not just "slideshow".
            //       See Note below for reasons.
            window.location = 'slideshow/index.htm?version=$VERSION_NUMBER';
        }
        return true;
    }
 </script>
 </head>
 <body onKeyPress='return onKeyPress(event, this)'>
EOF

if (-e prependToIndex.htm) then 
   cat prependToIndex.htm
endif

# Echo the following lines while expanding dollar sign vars
cat <<EOF
  <!-- Note: Use "slideshow/index.htm" here, not just "slideshow".
             Either works fine remotely ("http:" URL) since the default 
             action is to load the index.htm file.  But locally 
             ("file:" URL) it works better to be explicit because 
             there's no such default.
  -->
  <br>
  <a class='com_bristle_jslib_css_Presentation_button'
     href='slideshow/index.htm?version=$VERSION_NUMBER'
     title='View slideshow (s)'
  >
   Slideshow
  </a>
  <!-- Note: Do NOT use "../index.htm" here.  That works better locally, as 
             described above.  But it fails both locally ("file:" URL) and
             remotely ("http:" URL) when in a nested subfolder whose parent 
             does not have an index.htm file.  In that case, it's better to 
             just use ".." because the default action is to show the folder
             contents (locally always, and remotely when there's no index.htm
             file).  That allows the user to see links including the "Parent
             Directory" link, and continue navigating up the tree.
  -->
  <a class='com_bristle_jslib_css_Presentation_button'
     href='..?version=$VERSION_NUMBER'
     title='View other albums'
  >
   Other Albums
  </a>
  <br>
  <br>
EOF

foreach image_file ("`ls $option_reverse_string *.[jJ][pP][gG] *.[gG][iI][fF] *.[pP][nN][gG]`")
   set txt_file = "${image_file:r}.txt"
   echo "<!--"
   echo "*******************************************************************"
   echo "$image_file"
   echo "*******************************************************************"
   echo "-->"
   make_html_for_image_and_caption --thumbs "$image_file" "$txt_file"
   echo ""
   echo ""
   echo ""
   echo ""
   echo ""
   echo ""
   echo ""
end

set txt_file = overview.htm
if (-e $txt_file:q) then 
   echo    "  <br />"
   cat $txt_file:q
endif
set txt_file = overview.txt
if (-e $txt_file:q) then 
   echo    "  <br />"
   echo    "  <xmp>"
   cat $txt_file:q
   echo    "  </xmp>"
endif

if (-e appendToIndex.htm) then 
   cat appendToIndex.htm
endif

# Echo the following lines while expanding dollar sign vars
cat <<EOF
  <!-- Fix the rotations of images to respect the EXIF info in their image"
       files, if the browser didn't already do it."
  -->
  <script type='text/javascript'>
    com.bristle.jslib.Image.fixMissingExifImageRotations();
  </script>
 </body>
</html>
EOF