#!/bin/bash
#   picseq - Generate a html thumbnail index from a group of images
#

thumbnailGeometry="200x100"
webGeometry="900x700>"
outHtml="$(date +%Y%m%d.%H%M%S).html"

HAVE_J2H=$(which j2h 2> /dev/null > /dev/null ; echo $?)
if [ "${HAVE_J2H}" -eq "1" ]
then
   echo "\"j2h\" doesn't seem to be available: thumbnails in the index file"
   echo "will link directly to the .web.jpg files."

   ext=".web.jpg"
else
   ext=".html"
fi

if [ "$#" -eq "0" ]
then
   echo "   picseq <images>"
   echo ""
   echo "   picseq takes images (leaving the original"
   echo "   alone) and generates two jpgs, one ${webGeometry} max"
   echo "   for web display, and another ${thumbnailGeometry} max for a web thumbnail,"
   echo "   and an html wrapper for the larger image (if \"j2h\" is available)."
   echo "   It also generates an HTML file index linking the three."
   echo ""
   exit
fi

echo "
<html>
<head>
<title>${outHtml} Image Thumbnail Gallery</title>
</head>
<body>

<h1>${outHtml} Image Thumbnail Gallery</h1>

" > ${outHtml}

echo "$# images to process."

while [ "$1x" != "x" ]
do
   pic="$1"
   imgName="${pic%.*}"
   extension="${pic##*.}"
   #   "convert" leaves the original alone but mogrify overwrites the original:
   if [ ${extension} = "jpg" ] || [ ${extension} = "jpeg" ]
   then
      cp ${imgName}.jpg ${imgName}.web.jpg
      cp ${imgName}.jpg ${imgName}.thumb.jpg
   else
      convert ${pic} ${imgName}.jpg
      cp ${imgName}.jpg ${imgName}.web.jpg
      mv ${imgName}.jpg ${imgName}.thumb.jpg
   fi
   mogrify -geometry ${webGeometry}       ${imgName}.web.jpg
   mogrify -geometry ${thumbnailGeometry} ${imgName}.thumb.jpg
   #  Put out a progress indicator for each image:
   echo -n "."
   if [ "${HAVE_J2H}" -eq "0" ]
   then
      j2h ${imgName}.web.jpg
   fi
   echo "<a href=\"${imgName}${ext}\"><img src=\"${imgName}.thumb.jpg\"></a>" >> ${outHtml}
   shift
done

echo "
</body>
</html>

" >> ${outHtml}

echo ""
echo "Index file is ${outHtml}"
echo ""