#!/bin/rc

TX = $home/t3x
TXB = $TX/bin
TXL = $TX/lib
LIB = $TXL/libtx.a
OPT = n
PREP = n
NATIVE = n

switch ($objtype) {
case 68020
	O = 2
case 960
	O = 6
case 386
	O = 8
case sparc
	O = k
case mips
	O = v
case 3210
	O = x
}
CC = $O^c
LD = $O^l

fn license {
echo ''
echo 'T3X -- A Compiler for the Procedural Language T, version 3X'
echo 'Copyright (C) 1996-2000 Nils M Holm.  All rights reserved.'
echo
echo 'Redistribution and use in source and binary forms, with or without'
echo 'modification, are permitted provided that the following conditions'
echo 'are met:'
echo '1. Redistributions of source code must retain the above copyright'
echo '   notice, this list of conditions and the following disclaimer.'
echo '2. Redistributions in binary form must reproduce the above copyright'
echo '   notice, this list of conditions and the following disclaimer in the'
echo '   documentation and/or other materials provided with the distribution.'
echo ''
echo 'THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS\'\' AND'
echo 'ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE'
echo 'IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE'
echo 'ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE'
echo 'FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL'
echo 'DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS'
echo 'OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)'
echo 'HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT'
echo 'LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY'
echo 'OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF'
echo 'SUCH DAMAGE.'
echo ''
	exit 0
}

fn usage {
	echo 'Usage: tx [-h] [-L] [-OPn] file'
	echo '       tx -h explains the options'
	exit 1
}

fn explain {
	echo ''
	echo 'Usage: tx [-h] [-L] [-OPn] file'
	echo '       -L - print the conditions of use'
	echo '       -O - enable the Tcode optimizer'
	echo '       -P - enable the preprocessor'
	echo '       -n - generate native code (default=Tcode)'
	echo ''
	echo 'Use "txx file" to run a Tcode program'
	echo ''
	exit 0
}

fn abort {
	if (! ~ x$OFILE x) {
		rm -f $OFILE.txpp $OFILE.unopt $OFILE.tcode $OFILE._c \
			$OFILE.c $OFILE.$O
	}
	rm -f TXPP.ERR TXTRN.ERR TXOPT.ERR TXCG.ERR
	exit 1
}

more = y
while (test $#* '!=' 0 -a $more '=' y) {
	switch(x$1) {
		case x-h
			explain
		case x-O
			shift
			OPT=y
		case x-P
			shift
			PREP=y
		case x-L
			license
		case x-n
			shift
			NATIVE=y
		case x-*
			usage
		case *
			more = n
	}
}

if (! ~ $#* 1) usage

if (! test -f $1) {
	echo tx: $1: no such file
	exit 1
}

OFILE = `{basename $1 .t}

if (~ $OFILE $1) {
	echo tx: $1: file has no .t suffix
	exit 1
}

rm -f TXPP.ERR TXTRN.ERR TXOPT.ERR TXCG.ERR

if (~ $PREP y) {
	txpp <$1 >$OFILE.txpp
	if (test -f TXPP.ERR) abort
	txtrn <$OFILE.txpp >$OFILE
	rm $OFILE.txpp
}
if not {
	txtrn <$1 >$OFILE
}
if (test -f TXTRN.ERR) abort

if (~ $OPT y) {
	mv $OFILE $OFILE.unopt
	txopt <$OFILE.unopt >$OFILE
	rm $OFILE.unopt
	if (test -f TXOPT.ERR) abort
}

if (~ $NATIVE y) {
	mv $OFILE $OFILE.tcode
	txcgc <$OFILE.tcode >$OFILE._c
	rm $OFILE.tcode
	if (test -f TXCG.ERR) abort
	sed -n -e 's/^DECL (.*)/\1/p' <$OFILE._c >$OFILE.c
	cat $TXL/txcgc_wrap >>$OFILE.c
	grep -v '^DECL' <$OFILE._c >>$OFILE.c
	rm $OFILE._c
	$CC -B -Dplan9 $OFILE.c
	if (! ~ $status '') abort
	rm $OFILE.c
	$LD -o $OFILE $OFILE.$O $LIB
	if (! ~ $status '') abort
	rm $OFILE.$O
	strip $OFILE
}
