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

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

# Abort on first error
set -e
trap 'rm -f tpp_output[12].Txl tpp_output[345].xml tpp_data[12].tmp tpp_parse[12].tmp' EXIT

# Parse options
qv="-q"
options=""
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: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's 'parse' phase.  Most options are passed to TXL to" >&2
    echo "describe how the target program should be compiled.  Special" >&2
    echo "options (which should be first if present):" >&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 "" >&2
    exit 1
fi

# Execute!  We can't use -v when executing tpp-toxml, due to TXL bug when
# both -xml and -v are used in a grammar with -newline and [NL].

if [ "$qv" = "-q" ]; then echo "  ...tpp-directives..." >&2; fi
txl $qv $1 ${mydir}tpp-directives.Txl - $options              >tpp_output1.Txl
if [ "$qv" = "-q" ]; then echo "  ...tpp-lex..." >&2; fi
txl -s 90 $qv tpp_output1.Txl ${mydir}tpp-lex.Txl             >tpp_output2.Txl
if [ "$qv" = "-q" ]; then echo "  ...tpp-toxml..." >&2; fi
txl ${qv%-v} -i `pwd` tpp_output2.Txl ${mydir}tpp-toxml.Txl   >tpp_output3.xml
if [ "$qv" = "-q" ]; then echo "  ...tpp-xmlfix..." >&2; fi
txl -s 256 $qv tpp_output3.xml ${mydir}tpp-xmlfix.Txl         >tpp_output4.xml
if [ "$qv" = "-q" ]; then echo "  ...tpp-xmlclean..." >&2; fi
txl -s 90 $qv tpp_output4.xml ${mydir}tpp-xmlclean.Txl        >tpp_output5.xml
if [ "$qv" = "-q" ]; then echo "  ...tpp-merge..." >&2; fi
txl $qv tpp_output5.xml ${mydir}tpp-merge.Txl
