#!/bin/bash
#
# This script scans the given JPEG files (whose names
# must end in .jpg), generates a thumbnail 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 the image contains PhotoRDF, it is used to generate a description.
#
# The HTML page with thumbnails is written to the first argument:
#
#    thumbnails index.html *.jpg 
#
# Options:
#
# -t title
#    Use title as the title of the thumbnails page (default:
#    "Thumbnails")
# -l language
#    Add lang=language to the generated HTML pages and translate the
#    generated canned phrases to that language. Currently supported
#    languages: "en" (=English, default), "nl" (=Dutch) and "fr"
#    (=French)
# -n name
#    The name to insert in the copyright message at the bottom
#    (default: "Bert Bos")
# -i
#    Interactive: prompts for the title, language and owner's name.
#
# This script relies on the PBM and JPEG tools to be installed.
#
# Author: Bert Bos <bert@w3.org>
# Created: 8 July 1999
# Version: $Date: 2002/05/30 00:06:14 $
#
# Copyright © 1995-2002 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

progname="${0##*/}"

# Other parameters

w1=100				# Width of thumbnail
w2=150				# Width of DIV (>= w1)
h1=100				# Height of thumbnail
h2=210				# 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
pagetitle="Thumbnails"		# Unless overridden by "-t"
language="en"			# Language of generated HTML pages
owner="Bert Bos"		# Copyright holder
email="bert@w3.org"		# Copyright holder's e-mail
interactive=false		# Whether to prompt for title, language, etc.

# Some constants for the interactive dialogs
#
d_backtitle="Thumbnails RDF"

# Constants (RDF field names). The [Dd] is there because between
# rdfpic 2.0 and 2.1, we changed the names of the properties to
# lowercase, as is the convention for RDF.
#
dcDescription=":[Dd]escription"
dcCoverage=":[Cc]overage"
dcDate=":[Dd]ate"
dcTitle=":[Tt]itle"
dcIdentifier=":[Ii]dentifier"
dcRelation=":[Rr]elation"
tcCamera=":[Cc]amera"
tcLens=":[Ll]ens"
tcFilm=":[Ff]ilm"
tcDevelDate=":[Dd]evel-date"
# If the programs xmlns and xselect (from
# http://www.w3.org/Tools/HTML-XML-utils/) are available, you can use
# the following, which is less not sensitive to errors due to RDF
# schemas with too similar element names.
#dc='\{http\:\/\/www.w3.org\/2000\/PhotoRDF\/dc-1-0\#\}'
#tc='\{http\:\/\/www\.w3\.org\/2000\/PhotoRDF\/technical-1-0\#\}'
#dcDescription="${dc}description"
#dcCoverage="${dc}coverage"
#dcDate="${dc}date"
#dcTitle="${dc}title"
#dcIdentifier="${dc}identifier"
#dcRelation="${dc}relation"
#tcCamera="${tc}camera"
#tcLens="${tc}lens"
#tcFilm="${tc}film"
#tcDevelDate="${tc}devel-date"

# Localization
languages=(\
 "en"\
 "nl"\
 "fr")
Legend=(\
 "Legend"\
 "Legenda"\
 "Légende")
Large=(\
 "large image"\
 "grote foto"\
 "grande photo")
Small=(\
 "small image (max 640x480)"\
 "kleine foto (max 640x480)"\
 "petite photo (640x480 max)")
Raw=(\
 "raw RDF metadata"\
 "ruwe RDF metadata"\
 "metadata RDF cru")
Previous=(\
 "Previous"\
 "Vorige"\
 "Précédent")
Index=(\
 "Index"\
 "Index"\
 "Index")
Next=(\
 "Next"\
 "Volgende"\
 "Suivant")
Image=(\
 "[image]"\
 "[foto]"\
 "[photo]")
Date=(\
 "Date:"\
 "Datum:"\
 "Date&nbsp;:")
Place=(\
 "Place:"\
 "Plaats:"\
 "Lieu&nbsp;:")
Series=(\
 "Series:"\
 "Serie:"\
 "Série&nbsp;:")
Number=(\
 "Number:"\
 "Nummer:"\
 "Numéro&nbsp;:")
Size=(\
 "Size:"\
 "Grootte:"\
 "Taille&nbsp;:")
Camera=(\
 "Camera:"\
 "Camera:"\
 "Appareil photo&nbsp;:")
Lens=(\
 "Lens:"\
 "Lens:"\
 "Objectif&nbsp;:")
Film=(\
 "Film:"\
 "Film:"\
 "Pellicule&nbsp;:")
DevelDate=(\
 "Development date:"\
 "Ontwikkeldatum:"\
 "Date de developpement&nbsp;:")


# usage -- print usage message and exit
function usage
{
  echo "Usage: $progname [-ih] [-t title] [-l language] [-n owner] [-m e-mail] output jpeg [jpeg...]"
  exit 1
}


# help -- explain the options
function help
{
  echo "$progname -- create thumbnails and description pages for JPEG images"
  echo "Options and arguments:"
  echo "  -l language"
  echo "	Output HTML in the given language. Currently supported"
  echo "	are \"en\" (English), \"fr\" (French) and \"nl\" (Dutch)."
  echo "	Default: en"
  echo "  -t title"
  echo "	Title of the generated thumbnails page"
  echo "	Default: \"Thumbnails\""
  echo "  -n name"
  echo "	The name of the copyright owner, which will be printed at"
  echo "	the bottom of the page. If the images do not contain RDF"
  echo "	metadata, a copyright message will be inserted into them"
  echo "	as well."
  echo "	Default: \"Bert Bos\""
  echo "  -i"
  echo "	Interactive. The user will be prompted for language,"
  echo "	title, name of owner, output file and image files."
  echo "  -h"
  echo "	Help. Shows this text."
  echo "  output"
  echo "	The name of the HTML file which will contain the"
  echo "	thumbnails. Use \"-\" to print to standard output."
  echo "  jpeg [jpeg...]"
  echo "	All remaining arguments are interpreted as JPEG files to"
  echo "	be processed. The program will create a small version"
  echo "	(max. 640x480) in the directory \"small\", unless the"
  echo "	original is already smaller than that. And it will create"
  echo "	a thumbnail (max. 100x100) in the directory \"thumb\". It"
  echo "	also creates one HTML file for each JPEG file with the"
  echo "	small version of the image and the description. If there"
  echo "	is no description in Photo-RDF format inside the JPEG,"
  echo "	the description will consist of just the file name."
  exit;
}


# extract -- get value of field $1 from the embedded PhotoRDF in $2
function extract
{
  sed \
   -e "/<[^\\/>]*$1>/!d" \
   -e "s/.*<[^\\/>]*$1>\\(.*\\)<\\/[^\\/>]*$1>/\\1/" $2

  # Correct method: uses both namespace and local name. The -i
  # (case-insensitive) is not correct, but works around a change in
  # rdfpic between 2.0 and 2.1, when the RDF properties lost their
  # initial capital, according to RDF convention. This method relies
  # on the HTML-XML-utils to be installed
  # (http://www.w3.org/Tools/HTML-XML-utils/)
  #
  # xmlns $2 | xselect -c -i "$1"
}


# escape1 -- make string $1 HTML-safe
#function escape1
#{
#  echo "$1" | sed -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g'
#}


# escape2 -- make string $1 HTML attribute-safe
#function escape2
#{
#  echo "$1" | sed -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g' \
#   -e 's/"/\&quot;/g'
#}

# oct2xml -- replace \-octal with &#-decimal
function oct2xml
{
  $awk '
   { while (match($0, /\\[0-7][0-7][0-7]/)) {
      n = 64 * substr($0, RSTART + 1, 1) + 8 * substr($0, RSTART + 2, 1) + \
       substr($0, RSTART + 3, 1);
      $0 = substr($0, 1, RSTART-1) sprintf("&#%d;", n) substr($0, RSTART+4);
     }
     print;
   }'
}

# getsize -- get size of file $1 in KB
function getsize
{
  #set -- `ls -s -k "$1"`	# We need the first word of the output
  #echo $1
  set -- `wc -c "$1"`
  expr $1 / 1024
}


# output_header -- Write the header part of the output
function output_header
{
  # 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.

  local wrapht=`expr $border + $h1 + $border`
  local title="$1"

  echo "<!doctype html public '-//W3C//DTD HTML 4.0//EN'"
  echo "  'http://www.w3.org/TR/REC-html40/strict.dtd'>"
  echo "<html lang=\"$language\">"
  echo "<title>$title</title>"
  echo "<style type=\"text/css\">"
  echo "  .thumb {padding: 6px; margin: 8px}"
  echo "  @media all {"
  echo "    .thumb {"
  echo "      float: left;"
  echo "      width: ${w2}px; height: ${h2}px; overflow: auto;"
  echo "      text-align: center;"
  echo "      /*background: black;*/ /*border: thin inset;*/"
  echo "      font-size: small; overflow: auto}"
  echo "    IMG {border: ${border}px solid white; margin-bottom: 4px}"
  echo "    .imgwrap {width: ${w2}px; display: table-cell;"
  echo "      vertical-align: bottom; padding: 0}"
  echo "  }"
  echo "  @media all {"
  echo "    .imgwrap {height: ${wrapht}px; margin-bottom: 0.5em}"
  echo "    /* Separate @media, because IE4 ignores only 1st rule in @... */"
  echo "  }"
  echo "  BODY {background: #033; color: white}"
  echo "  A {text-decoration: none}"
  echo "  A:link {color: white}"
  echo "  A:visited {color: silver}"
  echo "  .size {background: #060}"
  echo "  DIV.footer {clear: both; border-top: solid; padding-top: 1em}"
  echo "  H3 {margin: 0 0 0.2em 0; color: #FF0}"
  echo "  .rdf {background: #630}"
  #echo "  .legend {font-size: smaller}"
  echo "  ADDRESS {text-align: right}"
  echo "  .legend {float: left; width: 33%}"
  echo "  DT {display: compact}"
  echo "  DD {margin-top: 0.2em; margin-left: 4em}"
  echo "  P, DL {margin: 0}"
  echo "  .num, .date, .place {display: none}"
  echo "  @media print {"
  echo "    HTML, BODY {margin: 0}"
  echo "    .rdf, .legend {display: none}"
  echo "    .num, .date, .place {display: block}"
  echo "    .thumb {float: none; clear: left; text-align: left;"
  echo "      width: auto; height: auto; page-break-inside: avoid}"
  echo "    .imgwrap {display: block; float: left}"
  echo "  }"
  echo "</style>"
  echo
  echo "<body>"
  echo "<h1>$title</h1>"
}


# output_footer -- output copyright message, etc. at the end
function output_footer
{
  echo "<div class=footer>"
  echo "<div class=legend>"
  echo "<p><strong>${Legend[$lang]}:</strong>"
  echo "<dl><dt><span class=size>588&nbsp;KB</span><dd>${Large[$lang]}"
  echo "<dt><span class=size>37&nbsp;Kb</span><dd>${Small[$lang]}"
  echo "<dt><span class=rdf>RDF</span><dd>${Raw[$lang]}</dl>"
  echo "</div>"
  echo "<address>"
  echo "Copyright &copy; "`date +%Y`" <a href=\"mailto:$email\">$owner</a>"
  echo "</address>"
  echo "</div>"
  echo "</body>"
  echo "</html>"
}


# ask_pagetitle -- put up a dialog with an input box for the page title
function ask_pagetitle
{
  local TMP=/tmp/dialog-$$
  dialog --backtitle "$d_backtitle"\
   --inputbox "Title for thumbnails page" 10 60 "$pagetitle" 2>$TMP
  case $? in
    0) pagetitle="$(<$TMP)";;	# OK
    1|255) exit;;		# Cancel|ESC
    *) echo "Bug! Cannot happen!" >&2;;
  esac
  rm -f $TMP
}


# ask_owner -- put up a dialog with an input box for the copyright owner
function ask_owner
{
  local TMP=/tmp/dialog-$$
  dialog --backtitle "$d_backtitle"\
   --inputbox "Copyright owner" 10 60 "$owner" 2>$TMP
  case $? in
    0) owner="$(<$TMP)";;	# OK
    1|255) exit;;		# Cancel|ESC
    *) echo "Bug! Cannot happen!" >&2;;
  esac
  rm -f $TMP
}


# ask_images -- Ask for the names of all the JPEG images to process
function ask_images
{
  local TMP=/tmp/dialog-$$
  dialog --backtitle "$d_backtitle"\
   --inputbox "The JPEG images (e.g., \"*.jpg\")" 10 70 "$images" 2>$TMP
  case $? in
    0) images="$(<$TMP)";;	# OK
    1|255) exit;;		# Cancel|ESC
    *) echo "Bug! Cannot happen!" >&2;;
  esac
  rm -f $TMP
}


# ask_output -- put up a dialog for the name of the generated thumbnails page
function ask_output
{
  local TMP=/tmp/dialog-$$
  local again=true
  while [ $again = true ]; do
    dialog --backtitle "$d_backtitle"\
     --inputbox "Output file name (\"-\" for standard out)" 10 60 "$output" 2>$TMP
    case $? in
      0) output="$(<$TMP)";;	# OK
      1|255) exit;;		# Cancel|ESC
      *) echo "Bug! Cannot happen!" >&2;;
    esac
    if [ -z "$output" ]; then
      dialog --backtitle "$d_backtitle"\
       --msgbox "The file name cannot be empty. If you want output\
       to be printed to standard output, use a single dash (\"-\")"\
       12 60
      again=true
    else
      again=false
    fi
  done
  rm -f $TMP
}


# ask_language -- present the choice of output languages
function ask_language
{
  local TMP=/tmp/dialog-$$
  local en=off fr=off nl=off
  case "$language" in
    en) en=on;;
    fr) fr=on;;
    nl) nl=on;;
  esac
  dialog --backtitle "$d_backtitle"\
   --radiolist "Language for HTML pages" 12 40 3\
   en "English" $en\
   fr "French" $fr\
   nl "Dutch" $nl 2>$TMP
  case $? in
    0) language="$(<$TMP)";;	# OK
    1|255) exit;;		# Cancel|ESC
    *) echo "Bug! Cannot happen!" >&2;;
  esac
  rm -f $TMP
}


# Parse options

while getopts "hit:l:n:m:" flag; do
  case $flag in
    "i") interactive=true;;
    "t") pagetitle="$OPTARG";;
    "l") language="$OPTARG";;
    "n") owner="$OPTARG";;
    "m") email="$OPTARG";;
    "h") help;;
    "?") usage;;
  esac
done

# Parse the name of the HTML file to output to

$interactive || [[ "${!OPTIND}" != "" ]] || usage
output="${!OPTIND}"
shift $OPTIND
images="$*"

# Check which awk is present
#
awk=`type -p gawk 2>/dev/null`
[[ -x "$awk" ]] || awk=`type -p nawk 2>/dev/null`
[[ -x "$awk" ]] || awk=awk

# Check if the dialog program is present.
#
DIALOG=`type -p dialog 2>/dev/null`

# If interactive, prompt for all parameters. Loop until user is
# satisfied.
#
while $interactive; do
  if [ -x "$DIALOG" ]; then
    ask_pagetitle
    ask_language
    ask_owner
    ask_output
    ask_images
    if dialog --backtitle "$d_backtitle" --yesno\
     "Are these correct?\n\
     Title = \"$pagetitle\"\n\
     Language = \"$language\"\n\
     Owner = \"$owner\"\n\
     Output = \"$output\"\n\
     Images = $images" 20 72; then
      interactive=false
    fi

  else				# No dialog, use read instead
    read -e -p "Page title? [$pagetitle] "
    [[ -z "$REPLY" ]] || pagetitle="$REPLY"
    read -e -p "Language? [$language] "
    [[ -z "$REPLY" ]] || language="$REPLY"
    read -e -p "Owner? [$owner] "
    [[ -z "$REPLY" ]] || owner="$REPLY"
    read -e -p "Output file? ${output:+[$output]} "
    [[ -z "$REPLY" ]] || output="$REPLY"
    read -e -p "Images? [$images] "
    [[ -z "$REPLY" ]] || images="$REPLY"
    if [ -z "$output" ]; then
      echo
      echo "** Sorry, the output file must be specified."
      echo "** Use \"-\" if you want to print to standard output."
      echo
    else
      echo
      echo "Page title : $pagetitle"
      echo "Language   : $language"
      echo "Owner      : $owner"
      echo "Output     : $output"
      echo "Images     : $images"
      echo
      read -e -p "Is this OK? [Y] "
      if [ "$REPLY" = "y" -o "$REPLY" = "Y" -o "$REPLY" = "" ];then
	interactive=false
      fi
    fi
  fi
done

# Redirect generated HTML output to file, unless it is "-" (stdout)

[[ "$output" = "-" ]] || exec >"$output"

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

TMP=/tmp/copyright-$$
echo "Copyright © "`date +%Y`" $owner <$email>" >$TMP
RDFFILE=/tmp/rdf-$$
TMP3=/tmp/text-$$
trap "rm -f $TMP $RDFFILE $TMP3" 0

# Translate name of the language into an index into language array

typeset -i lang
((lang = ${#languages[*]} - 1))
while [ "${languages[$lang]}" != "$language" -a $lang -gt 0 ]; do
  ((lang = $lang - 1))
done

# Create the directories for the thumbnails and the reduced JPEGs

[[ -d $thdir ]] || mkdir $thdir
[[ -d $smdir ]] || mkdir $smdir

# Write the header of the HTML file.

output_header "$pagetitle"

# Loop over all arguments

unset mainpage
set -- $images
while [ $# -gt 0 ]; do

  prev="$mainpage"		# For "prev" link
  f="$1"			# Image to process
  if [ -z "$2" ]; then unset next; else next=`basename "$2" .jpg`.html; fi
  mainpage=`basename "$f" .jpg`.html # HTML page for small image
  shift

  echo -e "Processing $f \c" >&2

  h=`basename "$f" .jpg`	# Basename of original image
  g="$thdir/${h}"		# Basename of thumbnail
  j="${g}.jpg"			# Full name of thumbnail
  smallbase="$smdir/${h}"	# Basename of 640x480 version
  smallname="$smallbase.jpg"	# Full name of 640x480 version

  typeset -i size=`getsize "$f"`
  echo -e "(${size} KB), \c" >&2

  # Check for PhotoRDF

  rdjpgcom "$f" | oct2xml >$RDFFILE
  description=`extract "$dcDescription" $RDFFILE`
  title=`extract "$dcTitle" $RDFFILE`
  date=`extract "$dcDate" $RDFFILE`
  place=`extract "$dcCoverage" $RDFFILE`
  series=`extract "$dcRelation" $RDFFILE`
  number=`extract "$dcIdentifier" $RDFFILE`
  camera=`extract "$tcCamera" $RDFFILE`
  lens=`extract "$tcLens" $RDFFILE`
  film=`extract "$tcFilm" $RDFFILE`
  develdate=`extract "$tcDevelDate" $RDFFILE`
  [[ -z "$description" ]] && description="$f"

  # Generate small version, unless an up-to-date version already exists
  # Copy the PhotoRDF if it exists, or add a standard copyright

  echo -e "small \c" >&2

  if [ -z "$title" ]; then cp $TMP $TMP3; else cp $RDFFILE $TMP3; fi
  if [[ ! -f "$smallname" || "$f" -nt "$smallname" ]]; then
    djpeg "$f" | pnmscale -xysize $smallwd $smallht | cjpeg |\
     wrjpgcom -cfile $TMP3 >"$smallname"
  fi

  typeset -i smallsize=`getsize "$smallname"`
  echo -e "(${smallsize} KB), \c" >&2

  # Generate thumbnail unless an up-to-date version already exists

  echo "thumbnail" >&2

  if [[ ! -f "$j" || "$f" -nt "$j" ]]; then
    djpeg "$smallname" | pnmscale -xysize $w1 $h1 | cjpeg >"$j"
  fi
  fileinfo=`djpeg "$j" | pnmfile`
  thumbwd=`echo $fileinfo | cut -d, -f2 | cut -d' ' -f2`
  thumbht=`echo $fileinfo | cut -d, -f2 | cut -d' ' -f4`

  # Find out if $smallname is indeed smaller than $f. Store the name
  # and size of the smaller of the two in $mainname and $mainsize, and
  # compute the name of an HTML file $mainpage
  #
  if [ $smallsize -lt $size ]; then
    mainname="$smallname"
    mainsize=$smallsize
  else
    mainname="$f"
    mainsize=$size
  fi

  # Write an HTML page for the small version, with links to prev, next
  # and thumbnails and to the large image

  (
    echo "<!doctype html public '-//W3C//DTD HTML 4.0//EN'"
    echo "  'http://www.w3.org/TR/REC-html40/strict.dtd'>"
    echo
    echo "<html lang=\"$language\">"

    # Header

    echo -e "<title>\c"
    if [ -z "$title" ]; then echo -e "$fc"; else echo -e "$title\c"; fi
    echo "</title>"
    [[ -z "$prev" ]] || echo "<link rel=prev href=\"$prev\">"
    [[ -z "$next" ]] || echo "<link rel=next href=\"$next\">"
    echo "<link rel=up href=\"$output\">"
    [[ -z "$title" ]] || echo "<link rel=meta href=\"$f;text%2Fxml\">"
    echo "<style type=\"text/css\">"
    echo "	body {background: #033; color: white}"
    echo "	h1, p {text-align: center}"
    echo "	h1 {color: #FF0}"
    echo "	table {margin: 1em auto}"
    echo "	th {text-align: right}"
    echo "	td {text-align: left}"
    echo "	tr {vertical-align: baseline}"
    echo "	img {border: thick solid white}"
    echo "	.rdf {background: #630; color: white}"
    echo "	.nav {width: 1em; border: thin solid; padding: 0.2em; float: left}"
    echo "	a {text-decoration: none}"
    echo "	a[rel=meta] {float: right}"
    echo "	a:link {color: inherit /*#00F*/}"
    echo "	.nav {color: inherit /*#999*/}"
    echo "	a:visited {color: inherit /*#80F*/}"
    echo "	a:active {background: #FFF}"
    echo "	a:hover {background: #FCC}"
    echo "	@media print { .nav {display: none} }"
    echo "</style>"
    echo

    # Body

    if [ -z "$prev" ]; then
      echo "<span class=nav>&#9664;</span>"
    else
      echo "<a class=nav rel=prev accesskey=P href=\"$prev\""
      echo "	title=\"${Previous[$lang]}\">&#9664;</a>"
    fi
    echo "<a class=nav rel=up accesskey=I href=\"$output\""
    echo "	title=\"${Index[$lang]}\">&#9679;</a>"
    if [ -z "$next" ]; then
      echo "<span class=nav>&#9654;</span>"
    else
      echo "<a class=nav rel=next accesskey=N href=\"$next\""
      echo "	title=\"${Next[$lang]}\">&#9654;</a>"
    fi
    if [ ! -z "$title" ]; then
      echo "<a class=nav rel=meta accesskey=R href=\"$f;text%2Fxml\""
      echo "	title=\"${Raw[$lang]}\">&#9670;</a>"
    fi
    echo
    echo -e "<h1>\c"
    if [ -z "$title" ]; then echo -e "$f\c"; else echo -e "$title\c"; fi
    echo "</h1>"
    echo
    echo "<p><a accesskey=M href=\"$f\"><img alt=\"${Image[$lang]}\""
    echo "	src=\"$mainname\"></a>"
    echo
    echo "<p class=\"desc\">$description"
    echo
    echo "<table>"
    [[ -z "$date" ]] || echo "<tr><th>${Date[$lang]}<td>$date"
    [[ -z "$place" ]] || echo "<tr><th>${Place[$lang]}<td>$place"
    [[ -z "$series" ]] || echo "<tr><th>${Series[$lang]}<td>$series"
    [[ -z "$number" ]] || echo "<tr><th>${Number[$lang]}<td>$number"
    [[ -z "$camera" ]] || echo "<tr><th>${Camera[$lang]}<td>$camera"
    [[ -z "$lens" ]] || echo "<tr><th>${Lens[$lang]}<td>$lens"
    [[ -z "$film" ]] || echo "<tr><th>${Film[$lang]}<td>$film"
    [[ -z "$develdate" ]] || echo "<tr><th>${DevelDate[$lang]}<td>$develdate"
    echo "<tr><th>${Size[$lang]}<td>$size&nbsp;KB"
    echo "</table>"

  ) > "$mainpage"

  # Output the HTML code for the thumbnail.
  # Link it to page with the small image

  echo
  echo "<div class=thumb>"
  echo " <div class=imgwrap>"
  echo "  <p><a href=\"$mainpage\"><img"
  echo "   src=\"$j\""
  echo "   alt=\"${Image[$lang]}\""
  [[ -z "$title" ]] || echo "   title=\"$title\""
  echo "   width=$thumbwd height=$thumbht></a>"
  echo " </div>"
  [[ -z "$title" ]] || echo " <h3>$title</h3>"
  echo " <p><a href=\"$f\"><span class=size>${size}&nbsp;KB</span></a> -"

  # Don't link to the small version if the small version is actually larger

  if [ $smallsize -lt $size ]; then
    echo " <a href=\"$smallname\"><span class=size>${smallsize}&nbsp;KB</span></a> -"
  else
    rm "$smallname"
  fi

  # ToDo: if the description (or any other field) is a URL, make a link

  echo " $description"

  # Add the number and the date (will only be visible when printed)

  [[ -z "$number" ]] || echo " <span class=num>($number)</span>"
  [[ -z "$date" ]] || echo " <span class=date>$date</span>"
  [[ -z "$place" ]] || echo " <span class=place>$place</span>"

  # If the PhotoRDF exists, make a URL that can retrieve it (Jigsaw only)

  [[ -z "$title" ]] || echo "<a class=rdf href=\"$f;text%2Fxml\">RDF</a>"

  echo "</div>"
done

output_footer

# Local Variables:
# mode: ksh
# End:
