#!/usr/local/gnu/bin/bash
#
# This script scans the given JPEG files (whose names
# must end in .jpg), generates a thumbnail in PNG and a reduced
# version in JPEG of each, and generates an HTML page with all the
# thumbnails and links to the reduced version and the original.
#
# If there is a file with the same name as the JPEG file, but with
# "-d.html" instead of ".jpg", it will copy its contents underneath
# the thumbnail.
#
# The HTML page with thumbnails is written to stdout, so you probably
# want to call the script with a redirect:
#
#    thumbnails *.jpg >index.html
#
# This script relies on the PBM and JPEG tools to be installed.
#
# Author: Bert Bos <bert@w3.org>
# Date: 8 July 1999
#
# Copyright © 1995-2000 World Wide Web Consortium, (Massachusetts
# Institute of Technology, Institut National de Recherche en
# Informatique et en Automatique, Keio University). All Rights
# Reserved.
# See http://www.w3.org/Consortium/Legal/copyright-software.html

# Change the copyright messages:
# - copyshort = plain text to be embedded in the generated PNG & JPEG files
# - copylong = HTML text to be added at the end of the thumbnail page

copyshort='Copyright © '`date +%Y`' Bert Bos <bert@w3.org>'
copylong="Copyright &copy; "`date +%Y`" <a href='mailto:bert@w3.org'>Bert Bos</a><br>
Photos may be used freely, if copyright is kept with photo."

# Other parameters

w1=100				# Width of thumbnail
w2=150				# Width of DIV (>= w1)
h1=100				# Height of thumbnail
h2=195				# Height of DIV (> h1 + 2*border)
border=4			# Width of image border
smallwd=640			# Width of smaller version
smallht=480			# Height of smaller version
thdir=thumb			# Name of thumbnail directory
smdir=small			# Name of smaller images directory

# Create a temporary file to hold the text for the copyright

TMP=/tmp/copyright-$$
echo "$copyshort" >$TMP
trap "rm $TMP" 0

# Create the directories for the thumbnails and the reduced JPEGs

mkdir $thdir
mkdir $smdir

# Write the header of the HTML file.
# Note: 'float: left' causes a bug in Netscape 4 and Opera 3.5 & 3.6
# (all images are in a single row, instead of as many as needed).
# You may want to comment it out until there are newer versions of
# those browsers.

wrapht=`expr $border + $h1 + $border`

echo "<html><title>Thumbnails</title>"
echo "<style type=\"text/css\">"
echo "  @media all {"
echo "    .thumb {"
echo "      float: left;"
echo "      width: ${w2}px; height: ${h2}px;"
echo "      padding: 6px; text-align: center;"
echo "      margin: 8px; background: black; /*border: thin inset;*/"
echo "      font-size: small; overflow: auto}"
echo "    IMG {border: ${border}px solid white; margin-bottom: 4px}"
echo "  }"
echo "  BODY {background: #666; color: white}"
echo "  .imgwrap {height: ${wrapht}px; margin-bottom: 0.5em}"
echo "  A {text-decoration: none}"
echo "  A:link {color: white}"
echo "  A:visited {color: silver}"
echo "  .size {background: #060}"
echo "  HR {clear: both}"
echo "</style>"
echo "<body>"

# Loop over all arguments

for f; do
  echo -e "Processing $f, \c" >&2
  typeset -i size=`ls -l $f | cut -c30-40`
  size=`expr $size / 1024`
  echo -e "(${size} Kb), \c" >&2
  h=`basename $f .jpg`		# Basename of original image
  g=$thdir/${h}			# Basename of thumbnail
  j=${g}.png			# Full name of thumbnail
  i=${h}-d.html			# Full name of description
  smallbase=$smdir/${h}		# Basename of 640x480 version
  smallname=$smallbase.jpg	# Full name of 640x480 version
  echo -e "thumbnail, \c" >&2
  if [[ ! -f $j || $f -nt $j ]]; then
    djpeg $f | pnmscale -xysize $w1 $h1 | pnmtopng -text $TMP >$j
  fi
  echo -e "small \c" >&2
  if [[ ! -f $smallname || $f -nt $smallname ]]; then
    djpeg $f | pnmscale -xysize $smallwd $smallht | cjpeg |\
     wrjpgcom -cfile $TMP >$smallname
  fi
  typeset -i smallsize=`ls -l $smallname | cut -c30-40`
  smallsize=`expr $smallsize / 1024`
  echo "(${smallsize} Kb)" >&2
  fileinfo=`pngtopnm $j | pnmfile`
  thumbwd=`echo $fileinfo | cut -d, -f2 | cut -d' ' -f2`
  thumbht=`echo $fileinfo | cut -d, -f2 | cut -d' ' -f4`
  echo "<div class=thumb>"
  echo "  <div class=imgwrap>"
  echo "    <a href='$f'><img src='$j'"
  echo "      title='$h' width=$thumbwd height=$thumbht></a>"
  echo "  </div>"
  echo "  <a href='$f'><span class=size>${size} Kb</span></a> -"
  if [ $smallsize -lt $size ]; then
    echo "  <a href='$smallname'><span class=size>${smallsize} Kb</span></a> -"
  else
    rm $smallname
  fi
  echo "  <a href='$f'>"
  if [ -f "$i" ]; then
    cat $i
  else
    echo "  $h"
  fi
  echo "</a></div>"
done

# Add copyright message at the end

echo "<hr>"
echo "<address>"
echo "$copylong"
echo "</address>"
echo "</html>"
echo "<!-- generated: $0 " "$@" " -->"
