#!/bin/sh
# FreeTXL 10.3 Application Compiler

# Where's FreeTXL?
TXLLIB=/usr/local/lib/txl

# Check we have arguments
if [ "$1" = "" ]
then
    echo "Usage:  txlc [txloptions] txlfile" 1>&2
    exit 99
fi

# Decode TXL program name and options
TXLPROG=""
TXLOPTIONS=""
while [ "$1" != "" ]
do
    case "$1" in
	*.Txl)
		if [ "$TXLPROG" = "" ]
		then
		    TXLPROG="$1"
		else
		    echo "Usage:  txlc [txloptions] txlfile" 1>&2
		    exit 99
		fi
		;;
	-help)
		echo "Usage:  txlc [txloptions] txlfile" 1>&2
		echo "('txl -help' for TXL options)" 1>&2
		exit 99
		;;
	-*) 
		TXLOPTIONS="$TXLOPTIONS $1"
		;;
	*)
		echo "Usage:  txlc [txloptions] txlfile" 1>&2
		exit 99
		;;
    esac
    shift
done

# Find our source file
TXLNAME=`basename $TXLPROG .Txl`
if [ ! -r $TXLNAME.Txl -a ! -r Txl/$TXLNAME.Txl ]
then 
    echo "Unable to open $TXLNAME.Txl" 1>&2
    exit 99
fi

# Compile to TXLVM byte code using TXL 
/bin/rm -f $TXLNAME.CTxl Txl/$TXLNAME.CTxl
txl -q -c $TXLNAME.Txl $*

# Check that we got a result
if [ ! -r $TXLNAME.CTxl ]
then
    if [ -r Txl/$TXLNAME.CTxl ]
    then
	/bin/mv Txl/$TXLNAME.CTxl $TXLNAME.CTxl
    else
	echo "TXL compile failed" 1>&2
	exit 99
    fi
fi

# Convert TXLVM byte code to initialized C byte array
$TXLLIB/txlcvt.x $TXLNAME.CTxl

# Compile and link with TXLVM
cc -O -o $TXLNAME.x $TXLLIB/txlmain.o $TXLLIB/txlvm.o  ${TXLNAME}_TXL.c

# Clean up
/bin/rm -f $TXLNAME.CTxl ${TXLNAME}_TXL.*


syntax highlighted by Code2HTML, v. 0.9.1