#!/bin/sh

target='unix-386'		# default target
cc='cc'				# C compiler
				# used to compile the lib and do the linking

T3XDIR=$HOME/t3x/0		# where the compiler resides

# First try to figure out things automatically.
#
CFLAGS=""
m32=""

case `uname -m` in
	amd64|x86_64)	if [ -f "$T3XDIR/tx-unix-686" ]; then
				target="unix-686"
			else
				target="unix-386"
				m32="-m32"
			fi ;;
	386|i386)	target="unix-386" ;;
	arm) 		if [ -f "$T3XDIR/tx-unix-armv7" ]; then
				target="unix-armv7"
			else
				target="unix-armv6"
			fi ;;
esac

case `uname` in
	OpenBSD)	CFLAGS=$CFLAGS" -fPIC" ;;
esac

# If this fails, add flags and/or target here.
#
# target=''

# CFLAGS=$CFLAGS" -m32"			# on x86 64-bit systems
# CFLAGS=$CFLAGS" -static"		# in case all else fails
# CFLAGS=$CFLAGS" -fPIC"		# get rid of stupid linker errors
# CFLAGS=$CFLAGS" -Wl,-z,notext"	# get rid of stupid linker errors

########## nothing to configure past this line ##########

tcvm=`which tcvm`

bytecode=no
bootstrap=no
externs=""

targets=""
for x in tcvm unix-386 unix-686 unix-armv6 unix-armv7 fbsd-386 dos cpm raspi-1
do
	if [ -f $T3XDIR/tx-$x ]; then
		targets="$targets $x"
	fi
done

if [ "$targets" = "" ]; then
	target="tcvm"
	bytecode="yes"
	for x in tcvm dos cpm; do
		if [ -f $T3XDIR/tx-$x.tc ]; then
			targets="$targets $x"
		fi
	done
fi

usage() {
	echo \
	"Usage: `basename $0` [-bcsv] [-t target] [-e extern] infile [outfile]"
	echo "       targets:$targets"
	echo "       -b - use bootstrapping compiler"
	echo "       -c - link console library (native Unix only)"
	echo "       -s - link system library (native Unix only)"
	echo "       -v - print version and exit"
	exit 1
}

if [ "$tcvm" = "" ]; then
	echo "$0: error: TCVM not installed"
	exit 1
fi

verbose=no

while true; do
	case $1 in
	-b)	bootstrap=yes; target=bootstrap; shift ;;
	-c)	externs="$externs $T3XDIR/console.c -lcurses"; shift ;;
	-s)	externs="$externs $T3XDIR/system.c"; shift ;;
	-e)	externs="$externs $2"; shift 2 ;;
	-t)	target=$2; shift 2 ;;
	-v)	verbose=yes; shift ;;
	-*)	usage ;;
	*)	break ;;
	esac
done

if [ $verbose = yes ]; then
	echo "T3X/0 $target"
	exit
fi

# In case both 386 and 686 backends are installed
#
if [ $target = unix-386 ]; then
	m32="-m32"
fi

if [ "$1" = "" ]; then usage; fi

if [ $bootstrap = yes ]; then
	: # Ok
elif [ $bytecode = no -a ! -e $T3XDIR/tx-$target ]; then
	echo "$0: no such target: $target"
	exit 1
elif [ $bytecode = yes -a ! -e $T3XDIR/tx-$target.tc ]; then
	echo "$0: no such target: $target"
	exit 1
fi

out=${2:-$1}

if [ $bootstrap = yes ]; then
	$tcvm $T3XDIR/txtrn.tc $1 $out
elif [ $bytecode = yes ]; then
	$tcvm $T3XDIR/tx-$target.tc $1 $out
else
	$T3XDIR/tx-$target $1 $out
	if [ $? != 0 ]; then exit 1; fi
	if [ "$target" = "fbsd-386" ]; then
		as -32 -o $out.o $out.s
		ld -o $out $out.o $externs
		strip $out
		rm -f $out.s $out.o
	fi
	if [	"$target" = "unix-386" -o \
		"$target" = "unix-armv6" -o \
		"$target" = "unix-armv7" \
	]; then
		$cc $CFLAGS -DBPW=4 $m32 -s -o $out $out.s $T3XDIR/unixlib.c \
			$externs
		rm -f $out.s
	elif [	"$target" = "unix-686" ]; then
		$cc $CFLAGS -DBPW=8 -s -o $out $out.s $T3XDIR/unixlib.c \
			$externs
		rm -f $out.s
	fi
fi
