#!/bin/sh
#
# tpm-run.sh - Script to run TxlProp's "mark" phase
#  -- (C) 2005 Jonathan Yavner

# Directory where our files are
mydir=${0%tpm-run.sh}

# Abort on first error
set -e
trap 'rm -f tpm_output[12].xml' EXIT

# Parse the options
qv="-q"
css=""
while [ $# -gt 0 ]; do
    if [ "$1" = "-quiet" ]; then
	qv="-q -q"
    elif [ "$1" = "-brief" ]; then
	qv="-q"
    elif [ "$1" = "-verbose" ]; then
	qv="-v"
    elif [ "$1" = "-keeptmp" ]; then
	trap '' EXIT
    elif [ "${1:0:5}" = "-css=" ]; then
	css=${1:5}
    elif [ "$1" = "" ]; then
 	echo -n #Skip empty argument, such as quoted "$css" when $css=""
    else
	break
    fi
    shift
done

# Test validity
if [ $# -ne 1 ]; then
    echo "$0 [-quiet] [-brief] [-verbose] [-keeptmp] input.xml >output.xml" >&2
    echo "" >&2
    echo "Run TxlProp's 'mark' phase.  Options:" >&2
    echo "  -quiet     = Say nothing" >&2
    echo "  -brief     = One line per call to TXL (default)" >&2
    echo "  -verbose   = Verbose output" >&2
    echo "  -keeptmp   = Keep temporary files" >&2
    echo "  -css='...' = Use alternate stylesheet" >&2
    echo "" >&2
    exit 1
fi

# Execute!
if [ "$qv" = "-q" ]; then echo "  ...tpm-grammar..." >&2; fi
txl $qv $1              ${mydir}tpm-grammar.Txl - "$css" >tpm_output1.xml
if [ "$qv" = "-q" ]; then echo "  ...tpm-callers..." >&2; fi
txl -s 90 $qv tpm_output1.xml ${mydir}tpm-callers.Txl    >tpm_output2.xml
if [ "$qv" = "-q" ]; then echo "  ...tpm-crisis..." >&2; fi
txl $qv tpm_output2.xml ${mydir}tpm-crisis.Txl
