#!/bin/sh 

# convert TEI file $1 into epub
# Sebastian Rahtz, January 2010
# copyright: TEI Consortium
# license: GPL
# $Id: teitoepub 7133 2010-01-24 21:35:01Z rahtz $


die()
{
    echo; echo
    echo "ERROR: $@."
    D=`date "+%Y-%m-%d %H:%M:%S.%N"`
    echo "This was a fatal error. $D"
    exit 1
}

usageMsg()
{
echo "teitoepub -- reads in a TEI XML file and creates an ePub book"
echo "  Usage: teitoepub[options] file"
echo "  options, shown with defaults:"

echo " --profile=default      profile family"
echo " --odd                        perform ODD pre-processing"
echo " --uid=XXX                supply unique URN for ebook"
echo " --debug                    emit some diagnostics" 
}

profile=default
odd=false
uid=
debug=false
while test $# -gt 0; do
  case $1 in
    --profile=*)  profile=`echo $1 | sed 's/.*=//'`;;
    --uid=*)  uid=`echo $1 | sed 's/.*=//'`;;
    --odd)       odd=true;;
    --debug)     debug=true;;
    --help)          usageMsg;;
    *) if test "$1" = "${1#--}" ; then 
	   break
	else
	   echo "WARNING: Unrecognized option '$1' ignored"
	   echo "For usage syntax issue $0 --help"
	fi ;;
  esac
  shift
done
HERE=`pwd`
if test -f $1 
then 
     echo Read file $1
else
  echo File $1 does not exist
  die
fi
D=`dirname $1`
F=`basename $1`
B=`basename $1 .xml`
THERE=`(cd $D; pwd)`
X=`dirname $0`
APPHOME=`(cd $X; pwd)`
TEMPDIR=/tmp/epub$$
TEMPLATES=$APPHOME/profiles/$profile/epub
XSL=$APPHOME
mkdir -p $TEMPDIR/OEBPS/media
case $THERE/$F in 
       *.zip)  (cd $TEMPDIR; unzip -q $THERE/$F ); F=`basename $F .zip`.xml;; 
       *) cp $THERE/$F $TEMPDIR/$F  ;; 
esac
cd $TEMPDIR
saxon $F $XSL/docx/utils/graphics/listgraphics.xsl ORIG=$THERE DIR=$TEMPDIR/OEBPS/media > fetch
sh $TEMPDIR/fetch
rm $TEMPDIR/fetch
if $odd
then
    saxon -o $TEMPDIR/tmp1.xml $F $XSL/odds2/odd2odd.xsl localsource=/usr/share/xml/tei/odd/p5subset.xml TEIC=true
    saxon -o $TEMPDIR/tmp2.xml $TEMPDIR/tmp1.xml $XSL/odds2/odd2lite.xsl displayMode=rnc  summaryDoc=true
else
    ln -s $F tmp2.xml
fi
saxon  tmp2.xml $APPHOME/profiles/$profile/epub/to.xsl  uid=$uid odd=$odd
if $debug 
then
    echo leaving tmp[123].xml alone
else
    rm -f tmp1.xml tmp2.xml
fi
rm -f $THERE/$B.epub
zip -q0X  $THERE/$B.epub mimetype
zip -qXr9D  $THERE/$B.epub   META-INF OEBPS
cd $HERE
if $debug 
then
    echo Working files in $TEMPDIR
else
    rm -rf $TEMPDIR
fi
echo Wrote $B.epub in $THERE
