#!/bin/sh

CC=cc

keep=0
if [ "x$1" = "x-k" ]; then
	keep=1
	shift
fi

usage() {
	echo "Usage: $0 [-k] host target"
	echo "       available hosts/targets: "
	echo -n "       tcvm cpm dos fbsd-386 unix-386 unix-686 macos-686"
	echo    " unix-armv6 unix-armv7"
	echo "       -k  keep intermediate files"
	exit 1
}

if [ $# != 2 ]; then usage; fi

host="$1"
target="$2"

CFLAGS=""

# First try to figure out things automatically.
#
case `uname -m` in
	amd64|x86_64)	if [ "$host" != "unix-686" -a \
			     "$host" != "macos-686" ]
			then
				CFLAGS=$CFLAGS" -m32"
			fi ;;
esac

case `uname` in
	OpenBSD)	CFLAGS=$CFLAGS" -Wl,-z,notext"
esac

# If this fails, add flags here.
#
# CFLAGS=$CFLAGS" -m32"			# 386 backend on 64-bit x86 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 ##########

unsupported() {
	echo "$0: host=$1, target=$2 is not supported (too big)"
	exit 1
}

reset() {
	ln -fs txemtbin.t txemit.t
	ln -fs txtcvm.t t3x.t
	ln -fs cgtcvm.t cg.t
}

case "$host" in
	unix-386)	host="u386" ;;
	unix-686)	host="u686" ;;
	unix-armv6)	host="uav6" ;;
	unix-armv7)	host="uav7" ;;
	macos-686)	host="m686" ;;
	fbsd-386)	host="f386" ;;
esac

case "$target" in
	unix-386)	target="u386" ;;
	unix-686)	target="u686" ;;
	unix-armv6)	target="uav6" ;;
	unix-armv7)	target="uav7" ;;
	macos-686)	target="m686" ;;
	fbsd-386)	target="f386" ;;
	raspi-1)	target="rp-1" ;;
esac

case "$host$target" in
	tcvmtcvm) eh=bin; et=bin; h=tcvm;   t=tcvm;   s=.tc  ;;
	tcvmcpm)  eh=bin; et=bin; h=tcvm;   t=xtocpm; s=.tc  ;;
	tcvmdos)  eh=bin; et=bin; h=tcvm;   t=dos86c; s=.tc  ;;
	tcvmf386) eh=src; et=bin; h=tcvm;   t=fbd386; s=.tc  ;;
	tcvmu386) eh=src; et=bin; h=tcvm;   t=unx386; s=.tc  ;;
	tcvmuav6) eh=src; et=bin; h=tcvm;   t=unxav6; s=.tc  ;;
	tcvmuav7) eh=src; et=bin; h=tcvm;   t=unxav7; s=.tc  ;;
	cpmtcvm)  eh=bin; et=bin; h=cpmz80; t=cpmtvm; s=.com ;;
	cpmcpm)   eh=bin; et=bin; h=cpmz80; t=cpmz80; s=.com ;;
	cpmdos)   eh=bin; et=bin; h=cpmz80; t=cpmdos; s=.com ;;
	cpmf386)  unsupported $1 $2 ;;
	cpmu386)  unsupported $1 $2 ;;
	cpmuav6)  unsupported $1 $2 ;;
	cpmuav7)  unsupported $1 $2 ;;
	dostcvm)  eh=bin; et=bin; h=dos86c; t=tcvm;   s=.com ;;
	doscpm)   eh=bin; et=bin; h=dos86c; t=xtocpm; s=.com ;;
	dosdos)   eh=bin; et=bin; h=dos86c; t=dos86c; s=.com ;;
	dosf386)  unsupported $1 $2 ;;
	dosu386)  unsupported $1 $2 ;;
	dosuav6)  unsupported $1 $2 ;;
	dosuav7)  unsupported $1 $2 ;;
	f386tcvm) eh=src; et=bin; h=fbd386; t=tcvm;   s=""   ;;
	f386cpm)  eh=src; et=bin; h=fbd386; t=xtocpm; s=""   ;;
	f386dos)  eh=src; et=bin; h=fbd386; t=dos86c; s=""   ;;
	f386f386) eh=src; et=src; h=fbd386; t=fbd386; s=""   ;;
	f386rp-1) eh=src; et=b32; h=fbd386; t=raspi1; s=""   ;;
	u386tcvm) eh=src; et=bin; h=unx386; t=tcvm;   s=""   ;;
	u386cpm)  eh=src; et=bin; h=unx386; t=xtocpm; s=""   ;;
	u386dos)  eh=src; et=bin; h=unx386; t=dos86c; s=""   ;;
	u386u386) eh=src; et=src; h=unx386; t=unx386; s=""   ;;
	u386rp-1) eh=src; et=b32; h=unx386; t=raspi1; s=""   ;;
	u686tcvm) eh=src; et=bin; h=unx686; t=tcvm;   s=""   ;;
	u686cpm)  eh=src; et=bin; h=unx686; t=xtocpm; s=""   ;;
	u686dos)  eh=src; et=bin; h=unx686; t=dos86c; s=""   ;;
	u686u686) eh=src; et=src; h=unx686; t=unx686; s=""   ;;
	u686rp-1) eh=src; et=b32; h=unx686; t=raspi1; s=""   ;;
	uav6tcvm) eh=src; et=bin; h=unxav6; t=tcvm;   s=""   ;;
	uav6cpm)  eh=src; et=bin; h=unxav6; t=xtocpm; s=""   ;;
	uav6dos)  eh=src; et=bin; h=unxav6; t=dos86c; s=""   ;;
	uav6uav6) eh=src; et=src; h=unxav6; t=unxav6; s=""   ;;
	uav6rp-1) eh=src; et=b32; h=unxav6; t=raspi1; s=""   ;;
	uav7tcvm) eh=src; et=bin; h=unxav7; t=tcvm;   s=""   ;;
	uav7cpm)  eh=src; et=bin; h=unxav7; t=xtocpm; s=""   ;;
	uav7dos)  eh=src; et=bin; h=unxav7; t=dos86c; s=""   ;;
	uav7uav7) eh=src; et=src; h=unxav7; t=unxav7; s=""   ;;
	uav7rp-1) eh=src; et=b32; h=unxav7; t=raspi1; s=""   ;;
	m686tcvm) eh=src; et=bin; h=mac686; t=tcvm;   s=""   ;;
	m686cpm)  eh=src; et=bin; h=mac686; t=xtocpm; s=""   ;;
	m686dos)  eh=src; et=bin; h=mac686; t=dos86c; s=""   ;;
	m686m686) eh=src; et=src; h=mac686; t=mac686; s=""   ;;
	m686rp-1) eh=src; et=b32; h=mac686; t=raspi1; s=""   ;;
	*)        echo "$0: unsupported host/target combination"; exit 1 ;;
esac

dh=targets
dt=targets

if [ ! -e tcvm ]; then
	echo "Tcode/0 VM (./tcvm) not found"
	exit 1
fi

# Is there a bootstrapping code generator for the given target?
# If so, do a 2-stage bootstrap
#
stage2=no
if [ -f targets/cg${t}b.t ]; then
	stage2=$t
	t=${t}b
	echo "Using two-stage build"
fi

if [ $h = tcvm ]; then
	echo "building tx-$2.tc"
	ln -fs txemt$eh.t txemit.t
	ln -fs txtcvm.t t3x.t
	ln -fs $dt/cg$t.t cg.t
	./tcvm bin/txtrn0.tc txtrn tx-$2
else
	echo "building tx-$2.tc (as a side effect)"
	ln -fs txemt$eh.t txemit.t
	ln -fs txtcvm.t t3x.t
	ln -fs $dh/cg$h.t cg.t
	./tcvm bin/txtrn0.tc txtrn tx-$2
	if [ $h = fbd386 -o \
	     $h = unx386 -o \
	     $h = unx686 -o \
	     $h = unxav6 -o \
	     $h = unxav7 -o \
	     $h = mac686 ]
	then
		echo "building tx-$2.s (intermediate)"
	else
		echo "building tx-$2$s"
	fi
	ln -fs txemt$et.t txemit.t
	ln -fs $dt/cg$t.t cg.t
	ln -fs $dh/tx$h.t t3x.t
	./tcvm tx-$2.tc txtrn tx-$2
	if [ $keep = 0 ]; then rm -f tx-$2.tc; fi
fi

if [ $h = fbd386 ]; then
	echo "building tx-$2$s"
	as -32 -o tx-$2.o tx-$2.s
	ld -o tx-$2$s tx-$2.o
	strip tx-$2$s
	if [ $keep = 0 ]; then rm -f tx-$2.s tx-$2.o; fi
fi

if [	$h = unx386 -o \
	$h = unxav6 -o \
	$h = unxav7 ]
then
	echo "building tx-$2$s"
	$CC $CFLAGS -DBPW=4 -s -o tx-$2$s tx-$2.s targets/unixlib.c
	if [ $keep = 0 ]; then rm -f tx-$2.s; fi
fi

if [	$h = unx686 -o \
	$h = mac686 ]
then
	echo "building tx-$2$s"
	$CC $CFLAGS -DBPW=8 -s -o tx-$2$s tx-$2.s targets/unixlib.c
	if [ $keep = 0 ]; then rm -f tx-$2.s; fi
fi

if [ $stage2 != no ]; then
	echo "building stage-2 tx-$2$s"
	ln -fs $dt/cg${stage2}.t cg.t
	./tx-$2$s txtrn tx-${2}-2
	$CC $CFLAGS -DBPW=8 -s -o tx-${2}-2$s tx-${2}-2.s targets/unixlib.c
	mv -f tx-${2}-2 tx-$2
	if [ $keep = 0 ]; then rm -f tx-$2.s; fi
	if [ $keep = 0 ]; then rm -f tx-${2}-2.s; fi
fi

reset
