#! /bin/sh

# This script constructs an HTML index of PhotoML files

# Copyright © 2003-2011 Brendt Wohlberg <photoml@wohlberg.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License at
# http://www.gnu.org/licenses/gpl-2.0.txt.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

# Most recent modification: 11 February 2011


# Set up paths to pmltrans and pmlexpand utilities 
pmlpath="`echo $0 | sed -e 's|/pmlindex||'`/.."
pmltransx="$pmlpath/tools/pmltrans"
pmlexpandx="$pmlpath/tools/pmlexpand"

# Ensure that xsltproc, pmltrans, and pmlexpand are available
if [ ! "`which xsltproc 2>/dev/null`" ]; then
  echo "pmlindex: error executing xstlproc" >&2
  exit 1
fi
if [ ! -x "$pmltransx" ]; then
  echo "pmlindex: error executing pmltrans" >&2
  exit 1
fi
if [ ! -x "$pmlexpandx" ]; then
  echo "pmlindex: error executing pmlexpand" >&2
  exit 1
fi

# Function for displaying usage information
usage()
{
cat <<EOF >&2
usage: pmlindex [-h] [-y] [-s size] (([-e] [-m]) | [-d]) [-p dst-path]
                infile [infile] ...
       -h            Display usage information
       -y            Group table entries by year
       -s size       Specify primary font size in points
       -e            Include exposure detail column when appropriate
       -m            Include location map when coordinates specified
       -d            Use detailed rather than summary output
       -p dst-path   Path to directory into which output should be written
EOF
}

# Parse command line arguments
yflag=''
ygp=''
sflag=''
fsp=''
eflag=''
mflag=''
dflag=''
dstpath='.';
while [ $# -ge 1 ]; do
  case $1 in
    -y)    yflag='-y'; ygp="--stringparam year-group 1" ;;
    -s)    shift; sflag="-s $1"; fsp="--stringparam font-size $1" ;;
    -e)    eflag='-e' ;;
    -m)    mflag='-m' ;;
    -d)    dflag='-d' ;;
    -p)    shift; dstpath=$1 ;;
    -*)    usage; exit 1 ;;
    *)     break ;;
  esac
  shift
done   

# Display usage information if appropriate
if [ "$1" = '' ]; then
  usage; exit 1
fi

# Make destination directory if necessary
mkdir -p $dstpath

# Set up paths to support XSL
xsl0="$pmlpath/xsl/html/preindex.xsl"
xsl1="$pmlpath/xsl/html/index.xsl"
# Set up temporary file paths
tmpidx="/tmp/pmlindex.idx.$$"
tmpexp="/tmp/pmlindex.exp.$$"
# Open root element of XML index source file
echo "<photoindex>" > $tmpidx

# Iterate over all files to be indexed
for src in $@; do
  # Generate error if a file could not be read
  if [ ! -r "$src" -o ! -f "$src" ]; then
    echo "pmlindex: could not read file $src" 1>&2
    exit 2
  fi

  # Set up path variables
  srcbase=`basename $src .xml`
  dst="$dstpath/$srcbase.html"

  # Apply pmltrans (with defaults expansion) if the HTML
  # representation of the current PhotoML file does not exist, 
  # or is out of date with respect to its source
  if [ ! -e "$dst" -o "$src" -nt "$dst"  ]; then
    $pmltransx -x $eflag $mflag $dflag $sflag $src > $dst
    if [ ! $? -eq 0 ]; then
      echo "pmlindex: error running pmltrans" 1>&2
      rm -f $tmpidx $tmpexp
      exit 3
    fi 
  fi

  if grep -q '<!-- IndexCache:' $dst; then
    # If index data cached as a comment in the current HTML file,
    # extract and use in the index source file
    indexdata=`grep '<!-- IndexCache:' $dst | sed -e 's/<!-- IndexCache: //'\
              -e 's/-->.*//' -e 's/\\-/-/g' -e 's/\&gt;/>/g' -e 's/\&lt;/</g'`
    echo "<file xml=\"$src\" html=\"$srcbase.html\">$indexdata</file>"\
	  >> $tmpidx
  else
    # If index data not cached as a comment in the current HTML file,
    # expand the defaults in the current PhotoML file and determine
    # them. Insert details of the current PhotoML file in the index
    # source file, and append the index cache as a comment to the
    # current HTML file.
    $pmlexpandx $src $tmpexp
    if [ ! $? -eq 0 ]; then
      echo "pmlindex: error running pmlexpand" 1>&2
      rm -f $tmpidx $tmpexp
      exit 4
    fi
    idxsrc=`xsltproc --novalid $xsl0 $tmpexp 2>/dev/null`
    rm -f $tmpexp
    echo "<file xml=\"$src\" html=\"$srcbase.html\">$idxsrc</file>" >> $tmpidx
    indexdata=`echo $idxsrc |  tr -d '\n' | sed -e 's/>[ \t]*</></g'\
                    -e 's/</\&lt;/g' -e 's/>/\&gt;/g' -e 's/-/\\-/g'`
    echo "<!-- IndexCache: $indexdata -->" >> $dst
  fi
done

# Close root element of XML index source file
echo "</photoindex>" >> $tmpidx
# Process XML index source file to generate HTML index
xsltproc $ygp $fsp $ecp $xsl1 $tmpidx > "$dstpath/index.html"
# Clean up
rm -f $tmpidx

exit 0
