#!/bin/sh
#
# Author: Marco Ladermann
# Date: Sat May 10 13:59:35 CEST 2003 @541 /Internet Time/
# Purpose: Starts the transformation process of a UI file to Java
# Changed:
#
# This software is free software. It is released under the terms of the
# GNU Lesser General Public Licence (LGPL)
# see http://www.gnu.org/copyleft/lesser.html
#
# These stylesheets are distributed in the hope that they will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#

getAbsolutePath() {
    ls=$(ls -l $1)
    pushd `dirname ${ls##* }` >/dev/null
    pwd -P
    popd >/dev/null
}

usage() {
    juic=$(basename $0)
    cat <<EOT
Usage: $juic [options] uifile
Where options can be:
    -proc       (xsltproc | xalan | saxon)  : Processor to use, defaults to "xsltproc"
    -package    "the.package.for.the.class" : Generates a package declaration
    -abstract   (true | false)              : Slots are declared abstract, defaults to "true"
    -images     dir                         : Path to images, defaults to "images"
    -os         (unix | msdos | mac)        : Selects newline characters
EOT
    exit 1
}

# Assume the directory structure
# ${UIXSL}/
#          bin/
#          lib/
#          xsl/
#              common/
#              java

a=$(getAbsolutePath $0)
UIXSLDIR=$(dirname "$a")
UIXSL=${UIXSLDIR}/juic.xsl

ABSTRACT=
IMAGES=
OS=
PACKAGE=
PROC=

JAVA_OPTS="-Xmx64m -Xms64m"

while [ -n "$1" ]
do
    case "$1" in
        -abstract) ABSTRACT=$2; shift; shift;;
        -images) IMAGES=$2; shift; shift;;
        -os) OS=$2; shift; shift;;
        -package) PACKAGE=$2; shift; shift;;
        -proc) PROC=$2; shift; shift;;
        -*) echo Unknown parameter $1; shift;;
        *) break;;
    esac
done

if [ -z $1 ]
then
    usage
fi

if [ -z "$PROC" ]
then
    x=
    if [ -n "`which xsltproc`" ]
    then
        PROC=xsltproc
    else
        PROC=saxon
    fi
fi


while [ -n "$1" ]
do
    OPT=
    case "$PROC" in
        xsltproc)
            test -n "$PACKAGE"  && OPT="--stringparam package $PACKAGE"
            test -n "$IMAGES"   && OPT="$OPT --stringparam images $IMAGES"
            test -n "$ABSTRACT" && OPT="$OPT --param genabstract $ABSTRACT"
            test -n "$OS"       && OPT="$OPT --stringparam os=$OS"
            echo xsltproc --nonet --novalid $OPT $UIXSL $1
            xsltproc --nonet --novalid $OPT $UIXSL $1
        ;;
        saxon)
            test -n "$PACKAGE"  && OPT="package=$PACKAGE"
            test -n "$IMAGES"   && OPT="$OPT images=$IMAGES"
            test -n "$ABSTRACT" && OPT="$OPT genabstract=$ABSTRACT"
            test -n "$OS"       && OPT="$OPT os=$OS"
            echo java $JAVA_OPTS -cp $UIXSLDIR/lib/saxon/saxon.jar com.icl.saxon.StyleSheet $1 $UIXSL $OPT
            java $JAVA_OPTS -cp $UIXSLDIR/lib/saxon/saxon.jar com.icl.saxon.StyleSheet $1 $UIXSL $OPT
        ;;
        xalan)
            OPT="-in $1 -xsl $UIXSL"
            test -n "$PACKAGE"  && OPT="$OPT -param package $PACKAGE"
            test -n "$IMAGES"   && OPT="$OPT -param images $IMAGES"
            test -n "$ABSTRACT" && OPT="$OPT -param genbstract $ABSTRACT"
            test -n "$OS"       && OPT="$OPT -param os=$OS"
            CP=
            for jar in $UIXSLDIR/lib/xalan/*.jar; do CP="$CP:$jar"; done
            echo java $JAVA_OPTS -Djava.endorsed.dirs=$UIXSLDIR/lib/xalan -cp $CP org.apache.xalan.xslt.Process $OPT
            java $JAVA_OPTS -Djava.endorsed.dirs=$UIXSLDIR/lib/xalan -cp $CP org.apache.xalan.xslt.Process $OPT
        ;;
    esac
    shift
done

