#!/bin/rc

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

OUTFILE=''
LIB=./libtx.$O

fn usage {
	echo Usage: txboot [-o outfile] sourcefile
	exit 1
}

more = y
while (test $#* '!=' 0 -a $more '=' y) {
	switch ($1) {
	case -o
		if (test $#* -lt 2) usage
		OUTFILE = $2
		shift 2
	case *
		more = n
	}
}

if (! ~ $#* 1) usage

if (~ $OUTFILE '') {
	OUTFILE = `{basename $1 .t}
	if (~ $OUTFILE $1) {
		echo TXBOOT: input program has no .t suffix
		exit 1
	}
}

../compiler/txtrn <$1 >$OUTFILE.UNOPT
../compiler/txopt <$OUTFILE.UNOPT >$OUTFILE.tcode
rm -f $OUTFILE.UNOPT

./txcgc.boot <$OUTFILE.tcode >$OUTFILE._c
rm $OUTFILE.tcode

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

$CC -B -Dplan9 $OUTFILE.c
rm $OUTFILE.c
$LD -o $OUTFILE $OUTFILE.$O $LIB
rm $OUTFILE.$O
strip $OUTFILE

