#!/bin/sh -e
# convert between TEI and other formats. intended to be use as symlink to other scripts
# whose names will dictate the format and direction of conversion.
# Sebastian Rahtz, July 2011
# copyright: TEI Consortium
# license: GPL
# $Id$

script=`basename $0`
scriptdir=`dirname "$0"`
APPHOME=`(cd $scriptdir; cd ..; pwd)`
defaultAPPHOME=`(cd $scriptdir; cd .. ; pwd)`
profiledir=$APPHOME/profiles
defaultprofiledir=$APPHOME/profiles
oxygenlib=/usr/share/oxygen/lib
die()
{
    echo "ERROR: $@."
    D=`date "+%Y-%m-%d %H:%M:%S"`
    echo "This was a fatal error. $D"
    exit 1
}

usageMsg()
{
echo "TEI conversion: from $from to $to"
echo ""
echo "  Usage: $0 [options] inputfile [outputfile]"
echo
echo "  Options, binary switches:"
echo "  --verbose            # be verbose"
echo "  --debug              # be verbose, do not delete intermediate files"
echo "  --apphome=$APPHOME   # where to find app directory"
echo "  --profiledir=$profiledir    # where to find profile directory"
echo "  --profile=$profile    # which transformation profile to use"
echo "  --lang=$lang    # which language to use "
echo "  --oxygenlib=$oxygenlib    # where is oxygenlib"
echo "  --splitLevel=$splitLevel        # splitting level "
case $format in
    rng|rnc|dtd|xsd)
        if [ $from != odd ]; then echo "  --odd                # perform processing of ODD (if appropriate)"; fi
        echo "  --localsource=DIR    # where is local copy of source of TEI Guidelines"
        echo "  --summaryDoc         # only make summary, when doing ODD processing"
        echo "  --schema=NAME        # process the schemaSpec called NAME"
        ;;
esac
case "$script" in
    teitoepub*) # could be teitoepub or teitoepub3
        echo "  --fileperpage        # create output files one per page "
        echo "  --nocompress         # disable compression of epub output file"
        ;;
esac

echo 
echo "  Options, shown with defaults:"
echo "  --saxonjar=$SAXONJAR  # location of Saxon jar file"
echo "  --trangjar=$TRANGJAR  # location of trang jar file"
case "$script" in
    teitoepub*) # could be teitoepub or teitoepub3
echo "  --mediaoverlay       # create media overlays from timeline data "
echo "  --coverimage=        # name of JPEG file for ebook cover"
echo "  --css=               # supply name of CSS file "
echo "  --publisher=         # name of publisher for ebook "
echo "  --subject=           # subject/category for ebook "
echo "  --uid=               # unique URN for ebook "
echo "  --viewportwidth=     # viewport width for epub fixed-size pages "
echo "  --viewportheight=    # viewport height for epub fixed-size pages "
    ;;
esac

echo
echo "Stylesheet family version: $version"
}

#-------------------------------------------------
fileperpage=
debug=
lang=en
profile=default
odd=false
verbose=false
summaryDoc=""
antflag="-q -Djava.awt.headless=true"
splitLevel=
TEISOURCE=
TEISOURCE_DEFAULT="${APPHOME}/source/p5subset.xml"

# epub only
overlayfile=
publisher=
coverimage=
cover=
cssFile=
conversion=
subject=
uid=
viewportwidth=
viewportheight=
medioverlay=
nocompress=
schema=
# ---
version=`cat "$APPHOME/VERSION"`
SAXONJAR=$APPHOME/lib/saxon10he.jar
TRANGJAR=$APPHOME/lib/trang.jar
from=`echo $script | sed 's/to.*//'`
to=`echo $script | sed 's/.*to//'`

if [ ! -f "$SAXONJAR" ] ; then die "Saxon jar file $SAXONJAR does not exist"; fi

while test $# -gt 0; do
  case $1 in
    --coverimage=*) cover=`echo $1 | sed 's/.*=//'`;;
    --css=*)        cssFile=-DcssFile=`echo $1 | sed 's/.*=//'`;;
    --fileperpage)  splitLevel="-DsplitLevel=-1"; fileperpage="-DfilePerPage=true";;
    --odd)          odd=true;;
    --lang=*)    lang=`echo $1 | sed 's/.*=//'`;;
    --localsource=*)   TEISOURCE="`echo $1 | sed 's/.*=//'`";;
    --schema=*)   schema="`echo $1 | sed 's/.*=//'`";;
    --mediaoverlay) mediaoverlay="-Dmediaoverlay=true";;
       --profile=*)    profile=`echo $1 | sed 's/.*=//'`;;
     --oxygenlib=*)  oxygenlib=`echo $1 | sed 's/.*=//'`;;
    --profiledir=*) profiledir=`echo $1 | sed 's/.*=//'`;;
       --apphome=*)  APPHOME=`echo $1 | sed 's/.*=//'`;;
     --publisher=*)  publisher=`echo $1 | sed 's/.*=//'`;;
      --saxonjar=*)   SAXONJAR=`echo $1 | sed 's/.*=//'`;;
      --trangjar=*)   TRANGJAR=`echo $1 | sed 's/.*=//'`;;
    --splitLevel=*) splitLevel="-DsplitLevel=`echo $1 | sed 's/.*=//'`";;
       --subject=*)    subject="`echo $1 | sed 's/.*=//'`";;
           --uid=*)        uid=`echo $1 | sed 's/.*=//'`;;
    --debug)        debug="-Ddebug=true"; verbose=true; antflag=-v;;
    --verbose)      verbose=true;;
    --summaryDoc)      summaryDoc="-DsummaryDoc=true";;
    --viewportwidth=*)   viewportwidth="-DviewPortWidth=`echo $1 | sed 's/.*=//'`";;
    --viewportheight=*)   viewportheight="-DviewPortHeight=`echo $1 | sed 's/.*=//'`";;
    --nocompress)       nocompress="-DnoCompress=true";;
    --help)         usageMsg; exit 1;;
     *) if test "$1" = "${1#--}" ; then 
           break
        else
           echo "WARNING: Unrecognized option '$1' ignored"
        fi ;;
  esac
  shift
done

if [ $# -gt 2 ]; then
    echo "Error: only one parameter (input file) or two parameters (input file and output file) allowed: $*"
    exit 1
fi

if [ $to = tei ]; then
    direction=from
    format=$from
    outsuffix=.xml
else
    direction=to
    format=$to
    outsuffix=.$to
fi
if [ $from = odd ] ; then odd=true; fi

infile=${1}
indir=$(cd $(dirname ${1}); pwd) # This returns a full path where just $(dirname) might return just ‘.’
infilename=$(basename ${1})
if [ $# -eq 1 ]; then           # only 1 argument, output file is input with new extension
    # strip off expected suffixes from input filename
    infilebase=${infilename}
    infilebase=${infilebase%.odd}
    infilebase=${infilebase%.tei}
    infilebase=${infilebase%.xml}
    infilebase=${infilebase%.csv}
    infilebase=${infilebase%.dbk}
    infilebase=${infilebase%.docx}
    infilebase=${infilebase%.html}
    infilebase=${infilebase%.xhtml}
    infilebase=${infilebase%.htm}
    infilebase=${infilebase%.odt}
    infilebase=${infilebase%.md}
    infilebase=${infilebase%.wp}
    infilebase=${infilebase%.xlsx}
    # output goes in same dir as input
    outdir=${indir}
    # output filename is like input but with common prefix replaced with new one
    outfilename=${infilebase}$outsuffix
else                            # 2 arguments (if there were more then 2 we would have already aborted)
    outfile=${2}
    # output directory is the directory specified
    outdir=`dirname "${outfile}"`
    # Following line tries to create the output directory if it does not exist yet
    outdir=`(mkdir -p ${outdir}; cd ${outdir}; pwd)`
    # output filename is as specified
    outfilename=`basename ${outfile}`
fi

if [ ${indir}/${infilename} = ${outdir}/${outfilename} ] ; then die "input and output files are the same"; fi

if [ ! -f "${indir}/${infilename}" ] ; then die "Input file $infile does not exist"; fi

if test "$defaultAPPHOME" != "$APPHOME" ; then
    if test "$profiledir" = "$defaultprofiledir" ; then
        profiledir=$APPHOME/profiles
    fi
fi

# **************************************** #
# *** work out defaultSource parameter *** # 
# ***  to pass to the XSL Stylesheets  *** #
# **************************************** #  
# case 1: no localsource set, we'll use the hard-coded values 
# for the source as set in the XSL Stylesheets
if test "x$TEISOURCE" = "x"
then
  defaultSource="\"$TEISOURCE_DEFAULT\""
  echo "WARNING: No localsource set. Will get a copy from $TEISOURCE_DEFAULT if necessary."

# case 2: localsource is set and starts with 'http',
# thus pointing at a remote resource
elif [ "$TEISOURCE" != "${TEISOURCE#http}" ]
then
  echo "using remote resource $TEISOURCE as default source" 
  defaultSource="\"$TEISOURCE\""

# case 3: localsource is set and the local file exists
elif test -e $TEISOURCE
then
  REALD=`dirname "$TEISOURCE"`
  SDIR=`(cd $REALD; pwd)`
  REALSOURCE=$SDIR/`basename $TEISOURCE`
  echo "using local file $REALSOURCE as default source"
  defaultSource="\"$REALSOURCE\""
# last case: exit with error
else 
  die "unable to get localsource from $TEISOURCE"
fi

if ! test -f "$APPHOME/$format/build-$direction.xml"; then
    die "No support for $from to $to: ant task $APPHOME/$format/build-$direction.xml does not exist"
fi

case $format in
      rnc) ;;
      xsd) ;;
      *) if ! test -f "$profiledir/$profile/$format/$direction.xsl"
          then
          die "No support for profile $profile: $profiledir/$profile/$format/$direction.xsl does not exist"
         fi
          ;;
esac

echo Convert ${indir}/${infilename} to ${outdir}/${outfilename} \($from to $to\) using profile $profile $debug $fileperpage $splitLevel $viewportwidth $viewportheight 
ant $antflag -f "$APPHOME/$format/build-$direction.xml" \
        -lib "${SAXONJAR}" $debug \
    $fileperpage $cssFile $splitLevel $viewportwidth $viewportheight $summaryDoc $mediaoverlay $nocompress \
        -Dodd=$odd \
        -Dsaxon.jar="${SAXONJAR}" \
        -Dtrang.jar="${TRANGJAR}" \
        -Dverbose=$verbose \
        -DinputFile="${indir}/${infilename}" \
        -DoutputFile="${outdir}/${outfilename}" \
        -Dprofiledir="$profiledir" \
        -Dprofile="$profile" \
        -Dpublisher=$publisher \
        -Duid=$uid \
        -DselectedSchema=$schema \
        -Dlang=$lang \
        -Dsubject=$subject \
        -Dcoverimage=$cover \
        -Doxygenlib=$oxygenlib \
        -DdefaultSource=$defaultSource
