#!/bin/sh
# $Id: build-tin,v 1.16 2023/12/17 20:11:16 tom Exp $
# vile:ts=4 sw=4
THIS=tin

: ${MAKE:=make}
export MAKE

clean-$THIS || exit 1

. no-rcs

clean() {
	( clean-$THIS; )
}

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

build() {
	rcs_save

	mkdir -p BUILD
	mkdir -p 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-$THIS \
		--exec-prefix=$BUILD \
		--enable-heapsort \
		--prefix=$BUILD "$@" && \
	test -f src/Makefile && \
	$MAKE build && \
	$MAKE install && \
		show_build "after install" && \
		( cd src && $MAKE uninstall ) && \
		show_build "after uninstall" &&
		echo "Build-SUCCESS $@" ||
		echo "Build-FAILURE $@"
	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 no 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
	*solaris*)
		# this assumes that xpg3-fixup.pl was run in aclocal.m4 and configure
		build $THIS --without-nntps "$@"
		;;
	xen-*|par-*|*-9|*debian*)
		build tinw --with-screen=ncursesw "$@"
		;;
	*)
		build $THIS "$@"
		;;
	esac
fi
