#!/bin/sh
#
# tp-run.sh - Script to run TxlProp
#  -- (C) 2005 Jonathan Yavner
#

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

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

# Parse the options
qv="-brief"
kt=""
css=""
options=""
while [ $# -gt 0 ]; do
    if [ "$1" = "-quiet" ]; then
	qv=$1
    elif [ "$1" = "-brief" ]; then
	qv=$1
    elif [ "$1" = "-verbose" ]; then
	qv=$1
    elif [ "$1" = "-keeptmp" ]; then
	trap '' EXIT
    elif [ "$1" = "-keepalltmp" ]; then
	kt="-keeptmp"
	trap '' EXIT
    elif [ "${1:0:5}" = "-css=" ]; then
	css=$1
    elif [ "${1:0:1}" = "-" ]; then
	options=`echo $options $1`
    else
	break
    fi
    shift
done

# Test validity
if [ $# -ne 1 ]; then
    echo "$0 [options] input.Txl >output.xml" >&2
    echo "" >&2
    echo "Run TxlProp on Input.Txl, producing output.xml.  Most options are" >&2
    echo "passed to TXL to describe how the target program should be" >&2
    echo "compiled. Special options (which should be first if present):" >&2
    echo "  -quiet      = Say nothing unless error" >&2
    echo "  -brief      = One line per call to TXL (default)" >&2
    echo "  -verbose    = Verbose output" >&2
    echo "  -keeptmp    = Keep output files for each phase" >&2
    echo "  -keepalltmp = Keep *all* temporary output files" >&2
    echo "  -css=FILE   = Use alternate stylesheet" >&2
    echo "" >&2
    exit 1
fi

# Execute!
if [ "$qv" = "-brief" ]; then echo "==parse==" >&2; fi
${mydir}parse/tpp-run.sh   $qv $kt $options $1           >tp_output1.xml
if [ "$qv" = "-brief" ]; then echo "==mark==" >&2; fi
${mydir}mark/tpm-run.sh    $qv $kt "$css" tp_output1.xml >tp_output2.xml
if [ "$qv" = "-brief" ]; then echo "==assert==" >&2; fi
${mydir}assert/tpa-run.sh  $qv $kt        tp_output2.xml >tp_output3.xml
if [ "$qv" = "-brief" ]; then echo "==tp-final==" >&2; fi
if [ "$qv" = "-verbose" ]; then Fqv="-v"; else Fqv="-q"; fi
txl $Fqv tp_output3.xml ${mydir}tp-final.Txl
