#!/bin/sh
# $Id: makelinkedimages,v 1.7 2007/10/14 01:53:40 grog Exp $
#
# Make a linked series of HTML pages, each of which points to a single
# image and links to the previous and next images.
#
# Specify the list of images as the paramters:
#
# $0 DIARY DEST *
#
#    DIARY is the name of the diary page
#    TOOLS is the name of the tools directory
#    DEST is the name of the index page
#    * are the files to process.
#
echo '++++' $0 $*
# Name of diary page
DIARY=$1
shift
# name of index page
TOOLS=$1
shift
# name of index page
DEST=$1
shift
PREVIMAGE=""
# Build each page
while [ $# -gt 0 ]; do
# base name of page
  if [ "$PREVIMAGE" != "" ]; then 
    PREVPAGE=`${TOOLS}/basefilename $PREVIMAGE`
    # name for printing, without -.
    PREVPAGENAME=`echo $PREVPAGE | sed 's:-: :g'`
  else
    PREVPAGE=""
    PREVPAGENAME=""
  fi
# Same for current image
  THISIMAGE=$1  
  THISPAGE=`${TOOLS}/basefilename $THISIMAGE`
# XXX
  echo  '****' processing $THISIMAGE
  THISPAGENAME=`echo $THISPAGE | sed 's:-: :g'`
# And next current image
  NEXTIMAGE=$2
  if [ "$NEXTIMAGE" != "" ]; then 
    NEXTPAGE=`${TOOLS}/basefilename $NEXTIMAGE`
    # name for printing, without -.
    NEXTPAGENAME=`echo $NEXTPAGE | sed 's:-: :g'`
  else
    NEXTPAGE=""
    NEXTPAGENAME=""
  fi
# This is upper case because identify outputs it like that.
  IMAGESIZE=`identify $THISIMAGE | sed 's:.*JPEG ::; s:.*GIF ::; s: .*::'`
  HORIZPIXELS=`echo $IMAGESIZE | sed 's:x.*::'`
  VERTPIXELS=`echo $IMAGESIZE | sed 's:.*x::'`
  cat <<EOF > $THISPAGE.html
<!-- Hey, emacs!  Edit this file in -*- html-fill -*- mode! -->
<!-- $Id: makelinkedimages,v 1.7 2007/10/14 01:53:40 grog Exp $ -->
<!-- Created on `date` by $0 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>$THISPAGENAME</title>
  </head>

  <body>
    <center>
      <h1>$THISPAGENAME</h1>
    </center>

    <center>
      <font size="-1">Last update: $Date: 2007/10/14 01:53:40 $</font>
    </center>

    <table summary="footings" width="100%">
      <tr>
EOF

  if [ "$PREVPAGENAME" != "" ]; then
      echo >> $THISPAGE.html "<td width="25%"><a href="$PREVPAGE.html">Previous page ($PREVPAGENAME)</a></td>"
  fi
# XXX what do we do here?
#      echo >> $THISPAGE.html "<td width="25%"><a href="/grog/recipes/yvonne/">Overview page</a></td>"
# Is this a JPEG?
  file $THISIMAGE | grep JPEG >/dev/null; 
  if [ $? -eq 0 ]; then
# we can at least hope for exif stuff
    exif $THISIMAGE > $THISPAGE.exif 
    LINES=`cat $THISPAGE.exif | wc -l`
    if [ $LINES -gt 6 ]; then
      HASEXIF=YES
      echo >> $THISPAGE.html "<td width="25%"><a href="$THISPAGE.exif">Exposure details</a></td>"  
# XXX debug
      echo $THISPAGE has EXIF data
    else
      HASEXIF=NO
      echo $THISPAGE 'is a JPEG, but it still has *NO* EXIF data'
      rm $THISPAGE.exif
    fi
  else
    HASEXIF=NO
    echo $THISPAGE 'is not a JPEG, so it has *NO* EXIF data'
  fi
  echo >> $THISPAGE.html "<td width="25%"><a href="$DEST">Index page</a></td>"  
  if [ "$NEXTPAGE" != "" ]; then
      echo >> $THISPAGE.html "<td width="25%"><a href="$NEXTPAGE.html">Next page ($NEXTPAGENAME)</a></td>"
  fi

  cat <<EOF >> $THISPAGE.html
      </tr>
    </table>
   <br/>
   <a href="$THISIMAGE"><img alt="$THISPAGENAME" src="$THISIMAGE" 
      width="$HORIZPIXELS" height="$VERTPIXELS" ></a>
   <br/>
   <hr/>
    <table summary="footings" width="100%">
      <tr>
EOF
# Save the exposure details
  if [ "$PREVPAGENAME" != "" ]; then
      echo >> $THISPAGE.html "<td width="25%"><a href="$PREVPAGE.html">Previous page ($PREVPAGENAME)</a></td>"
  fi
# XXX what do we do here?
#      echo >> $THISPAGE.html "<td width="25%"><a href="/grog/recipes/yvonne/">Overview page</a></td>"
  echo >> $THISPAGE.html "<td width="50%"></td>"  
  if [ "$NEXTPAGE" != "" ]; then
      echo >> $THISPAGE.html "<td width="25%"><a href="$NEXTPAGE.html">Next page ($NEXTPAGENAME)</a></td>"
  fi

  cat <<EOF >> $THISPAGE.html
      </tr>
    </table>

    <p>
    <a href="http://validator.w3.org/check/referer"><img
    src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31"
    width="88" /></a>

    <center>
      <font size="-1">\$Id: makelinkedimages,v 1.7 2007/10/14 01:53:40 grog Exp $</font>
    </center>

  </body>
</html>
EOF
  tidy -m -asxhtml $THISPAGE.html
  PREVIMAGE=$THISIMAGE
  shift
done
