#! /bin/sh

# This script converts PhotoML files to XMP format

# Copyright © 2010,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: 28 January 2011


pmlpath="`echo $0 | sed -e 's|/pmltoxmp||'`/.."
pmlvalidx="$pmlpath/tools/pmlvalid"

if [ ! "`which xsltproc 2>/dev/null`" ]; then
  echo "pmltoxmp: error executing xstlproc" >&2
  exit 1
fi

usage()
{
cat <<EOF >&2
usage: pmltoxmp [-h] [-n] ([-o path] [-s fstr] | [-f fid] [-g gid]) infile
       -h       Display usage information
       -n       Omit validity test
       -o path  Specify output path
       -s istr  Specify image id string format
       -f fid   Specify id of frame to convert
       -g gid   Specify id of group containing frame to convert
EOF
}

op=''
ff=''
fp=''
gp=''
novalid=0
while [ $# -ge 1 ]; do
  case $1 in
    -n)    novalid=1 ;;
    -o)    shift; op="$1" ;;
    -s)    shift; ff="$1" ;;
    -f)    shift; fp="$1" ;;
    -g)    shift; gp="$1" ;;
    -*)    usage; exit 1 ;;
    *)     infile=$1; break ;;
  esac
  shift
done   

if [ \( "$op" != '' -o "$ff" != '' \) -a \( "$fp" != '' -o "$gp" != '' \) ]; then
  usage; exit 1
fi
if [ "$infile" = '' ]; then
  usage; exit 1
fi

if [ ! -f "$infile" -o ! -r "$infile" ]; then
  echo pmltoxmp: could not open file $infile >&2
  exit 2
fi

dtdpub=`grep DOCTYPE $infile | grep -o -e '-\/\/.*EN'`;
dtdtyp=`echo $dtdpub | grep -o -e '//DTD[^/]*//' | cut -d ' ' -f 2`
if [ "$dtdtyp" != 'PhotoML' ]; then
  echo "pmltoxmp: input document type $dtdtyp not recognised" >&2
  exit 3
fi

if [ $novalid -eq 0 ]; then
  if ! $pmlvalidx "$infile" 2>/dev/null; then
    echo pmltoxmp: validity error in file $infile >&2
    exit 4
  fi
fi

if [ "$ff" != '' ]; then
  # Ensure that image id format string does not contain a path component
  ff=`echo $ff | sed -e 's|^.*/||'`".xmp"
  ffparam="--stringparam idstring-format $ff"
fi

fpparam=''
if [ "$fp" != '' ]; then
  fpparam="--stringparam fid-param $fp"
fi

gpparam=''
if [ "$gp" != '' ]; then
  gpparam="--stringparam gid-param $gp"
fi

if [ -r /etc/xml/catalog -a "$XML_CATALOG_FILES" = '' ]; then
  XML_CATALOG_FILES=/etc/xml/catalog
fi
XML_CATALOG_FILES="$pmlpath/dtd/catalog.xml $XML_CATALOG_FILES"
export XML_CATALOG_FILES
unset SGML_CATALOG_FILES

edxsl=$pmlpath/xsl/defaults/expand.xsl
txxsl=$pmlpath/xsl/misc/xmp.xsl

tmpin="/tmp/pmltoxmp.in.$$"
xsltproc $fsparam $ecparam $lmparam -o $tmpin $edxsl $infile
if [ "$fp" = '' -a "$gp" = '' ]; then
  tmpout="/tmp/pmltoxmp.out.$$"
  mkdir -p $tmpout
  opparam="--stringparam output-path $tmpout/"
  xsltproc $opparam $ffparam $txxsl $tmpin
  if [ "$op" = '' ]; then
    op='.'
  else
    op=`echo $op | sed -e 's|/$||'`
  fi
  nf=`ls $tmpout/*.xmp 2>/dev/null | wc -l`
  if [ $nf -gt 0 ]; then
    for f in $tmpout/*.xmp; do
      b=`basename $f`
      if [ -f "$b" ]; then
        echo Warning: skipping output "$op/$b" - file exists
      else
        mv "$f" "$op"
      fi
    done
  fi
else
  xsltproc $fpparam $gpparam $txxsl $tmpin
fi
status=$?

rm -f $tmpin
if [ -d "$tmpout" ]; then
  rm -rf $tmpout
fi

exit $status
