#!/bin/sh
# $Id: build-tin,v 1.8 2016/08/16 13:16:22 tom Exp $
clean() {
	test -f conf-tin  || exit 1
	test -f Makefile && make distclean
	rm -f *.out
	rm -rf BUILD
}

show_build() {
	echo "** $*"
	find BUILD -type f -print | csort 
}

build() {
	clean

	test -d BUILD || mkdir BUILD
	test -d LOGS  || mkdir LOGS

	BIN=$1
	shift 1

	test -z "$CC" && BIN="$BIN"-cc
	test -n "$CC" && BIN="$BIN"-gcc

	BUILD=`pwd`/BUILD
	echo "Building BIN=$BIN, $*"
	./conf-tin $* \
		--exec-prefix=$BUILD \
		--enable-heapsort \
		--prefix=$BUILD $* && \
	make build && \
	make install && \
		show_build "after install" && \
		( cd src && make uninstall ) && \
		show_build "after uninstall" && \
		clean
}

savelog() {
	echo "..savelog $BUILD_LOG"
	sed -e s%`pwd`%SRCDIR%g -e s%$VER%-VER%g run.log >$BUILD_LOG
	rm -f run.log
}

if test -z "$BUILD_LOG" ; then
	clean

	test -d bin   && rm -rf bin

	BUILD_LOG=LOGS/`partition`-${CC-cc}-run.log
	BUILD_LOG=run.log run-log $0
	savelog

else
	# conf-tin sets CC to "cc" if empty; my Solaris machines have no cc
	# likewise, newer FreeBSD has clang but not gcc except in a port
	if test -n "$CC"
	then
		:
	elif type cc 2>/dev/null
	then
		CC=cc
	elif type gcc 2>/dev/null
	then
		CC=gcc
	elif type clang 2>/dev/null
	then
		CC=clang
	fi
	case `partition` in
	*solaris10)
		SHELL=/usr/xpg4/bin/sh; export SHELL
		CONFIG_SHELL=$SHELL; export CONFIG_SHELL
		build tin
		;;
	xen-*|par-*|*-9|*debian*)
		build tinw --with-screen=ncursesw
		;;
	*)
		build tin
		;;
	esac
fi
