#!/bin/rc

PREP = 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

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

if (test $#* '!=' 0) {
	if (test x$1 '=' x-P) {
		PREP=y
		shift
	}
}

if (test $#* '!=' 1) {
	echo Usage: tc file
	exit 1
}

name = `{basename $1 .t}

if (~ $name $1) {
	echo tc: program has no .t suffix: $1
	exit 1
}

if (test -e $name.c) {
	echo tc: preserving file: $name.c
	exit 1
}

if (! test -f $1) {
	echo tc: cannot open: $1
	exit 1
}

if (~ $PREP y) {
	../compiler/txpp <$1 >$name.tpp
	if (test -f TXTRN.ERR) {
		rm TXTRN.ERR $name.tpp
		exit 1
	}
	../compiler/txx ../build/txtrn.tcode <$name.tpp >$name.tcode
	rm $name.tpp
}
if not {
	../compiler/txx ../build/txtrn.tcode <$1 >$name.tcode
}
if (test -f TXTRN.ERR) {
	rm TXTRN.ERR $name.tcode
	exit 1
}
../compiler/txx ../build/txcgc.tcode <$name.tcode >$name._c
if (test -f TXCG.ERR) {
	rm TXCG.ERR $name._c
	exit 1
}
rm $name.tcode

sed -n -e 's/^DECL (.*)/\1/p' <$name._c >$name.c
cat ../native/txcgc_wrap >>$name.c
grep -v '^DECL' <$name._c >>$name.c
rm $name._c

$CC -B -Dplan9 $name.c
X = $status
rm $name.c
if (! ~  $X '') exit 1

$LD -o $name $name.$O ../native/libtx.$O
rm $name.$O
strip $name
