#!/bin/sh
# 
# xmlresume script
# JPackage Project <http://www.jpackage.org/>
# $Id$

# Source functions library
. /usr/share/java-utils/java-functions

# Source system prefs
if [ -f /etc/xmlresume.conf ] ; then 
  . /etc/xmlresume.conf
fi

# Source user prefs
if [ -f $HOME/.xmlresumerc ] ; then 
  . $HOME/.xmlresumerc
fi

if [ -z "$XMLRESUME_RESUME" ] ; then
  XMLRESUME_RESUME=`pwd`/resume
fi

if [ -z "$XMLRESUME_COUNTRY" ] ; then 
  XMLRESUME_COUNTRY=us
fi

if [ -z "$XMLRESUME_PAPERSIZE" ] ; then
  if [ "x$XMLRESUME_COUNTRY" = xus ] ; then
    XMLRESUME_PAPERSIZE=letter
  else
    XMLRESUME_PAPERSIZE=a4
  fi
fi

if [ ! -f ${XMLRESUME_RESUME}.xml ] ; then
  echo Cannot find file \`$XMLRESUME_RESUME\' >&2
  exit 1
fi

if [ ! -f /usr/share/xmlresume/xsl/output/${XMLRESUME_COUNTRY}-html.xsl \
  -o ! -f /usr/share/xmlresume/xsl/output/${XMLRESUME_COUNTRY}-text.xsl ] ; then
  echo Country \`$XMLRESUME_COUNTRY\' not supported >&2
  exit 1
fi

if [ ! -f /usr/share/xmlresume/xsl/output/${XMLRESUME_COUNTRY}-${XMLRESUME_PAPERSIZE}.xsl ] ; then
  echo Paper size \`$XMLRESUME_PAPERSIZE\' not supported for country \`$XMLRESUME_COUNTRY\' >&2
  exit 1
fi

if [ -z "$XMLRESUME_QUIET" ] ; then
  echo Running \`$0\' with the following settings:
  echo
  echo '    * 'XMLRESUME_RESUME=$XMLRESUME_RESUME
  echo '    * 'XMLRESUME_COUNTRY=$XMLRESUME_COUNTRY
  echo '    * 'XMLRESUME_PAPERSIZE=$XMLRESUME_PAPERSIZE
  echo '    * 'XMLRESUME_FILTER=$XMLRESUME_FILTER
  echo
fi

export CLASSPATH=$(build-classpath xerces-j2 xalan-j2 fop avalon-logkit avalon-framework batik xmlresume-filter)

set_javacmd

$JAVACMD org.apache.xalan.xslt.Process -in ${XMLRESUME_RESUME}.xml -xsl http://xmlresume.sourceforge.net/xsl/output/${XMLRESUME_COUNTRY}-html.xsl -out ${XMLRESUME_RESUME}.html
$JAVACMD org.apache.xalan.xslt.Process -in ${XMLRESUME_RESUME}.xml -xsl http://xmlresume.sourceforge.net/xsl/output/${XMLRESUME_COUNTRY}-text.xsl -out ${XMLRESUME_RESUME}.txt
$JAVACMD org.apache.xalan.xslt.Process -in ${XMLRESUME_RESUME}.xml -xsl http://xmlresume.sourceforge.net/xsl/output/${XMLRESUME_COUNTRY}-${XMLRESUME_PAPERSIZE}.xsl -out ${XMLRESUME_RESUME}.fo
$JAVACMD org.apache.fop.apps.Fop -fo ${XMLRESUME_RESUME}.fo -pdf ${XMLRESUME_RESUME}.pdf

if [ -n "$XMLRESUME_FILTER" ] ; then
  $JAVACMD net.sourceforge.xmlresume.filter.Filter -in ${XMLRESUME_RESUME}.xml -out ${XMLRESUME_RESUME}-filtered.xml $XMLRESUME_FILTER
fi
