#!/bin/sh
# $Id: build-ncurses,v 1.20 2025/05/24 19:26:52 tom Exp $
# vile:ts=4 sw=4

: ${MAKE:=make}
export MAKE

clean() {
	test -d ncurses || exit 1
	test -f Makefile && $MAKE distclean
	test -d BUILD && rm -rf BUILD
	rm -f *.out
}

build() {
	BUILD=`pwd`/BUILD

	test -d bin   || mkdir bin
	test -d BUILD || mkdir BUILD
	test -d BUILD/PkgConfig || mkdir BUILD/PkgConfig
	test -d LOGS  || mkdir LOGS

	TERMINFO=$BUILD/terminfo
	test -d $TERMINFO || mkdir $TERMINFO
	export TERMINFO

	CFG=$1
	shift 1

	OPTS="--with-pkg-config-libdir=$BUILD/PkgConfig"

	# some nawks cannot handle long strings
	case `partition` in
	aix-*|OSF1-V5_1|HP-UX-B_11_00)
		OPTS="--disable-big-strings"
		;;
	esac

	case "${CC:=cc}" in
	*clang*)
		OPTS="$OPTS --without-ada"
		;;
	esac

	case `partition` in
	*openbsd*|*netbsd*|*freebsd*|m*)
		OPTS="$OPTS --enable-string-hacks"
		OPTS="$OPTS --with-hashed-db"
		;;
	esac

	case `partition` in
	*solaris*)
		;;
	*)
		OPTS="$OPTS --with-versioned-syms"
		;;
	esac

	. openbsd-egcc
	. openbsd-lld-noleaks

	case `partition` in
	*openbsd*)
		OPTS="$OPTS --without-ada"
		;;
	esac

	echo "Building $CFG $*"
	cfg-$CFG $OPTS \
		--without-libtool \
		$no_LEAKS \
		--disable-macros  \
		--enable-ext-colors \
		--enable-ext-mouse \
		--enable-ext-putwin \
		--enable-opaque-curses \
		--enable-opaque-form \
		--enable-opaque-menu \
		--enable-opaque-panel \
		--enable-pc-files \
		--enable-rpath \
		--enable-sp-funcs \
		--enable-wgetch-events \
		--exec-prefix=$BUILD \
		--prefix=$BUILD \
		--with-chtype=uint32_t \
		--with-develop \
		--with-shared \
		--with-trace \
		--without-debug \
		--without-normal \
		"$@" && \
	( $MAKE && \
		check-ncu-pconfig && \
		$MAKE install && \
		$MAKE uninstall && \
		save-ncurses && \
		echo "PASSED-Building $CFG $*" ) ||
		echo "FAILED-Building $CFG $*"
	ldd test/ncurses
	clean
}

if test -z "$BUILD_LOG" ; then
	clean

	BUILD_LOG=run.log run-log $0 "$@"
	if test -z "$BUILD_TYPE"
	then
		case "x$CC" in
		x*pgcc)		BUILD_TYPE=pgcc;;
		x*icc)		BUILD_TYPE=icc;;
		x*clang)	BUILD_TYPE=clang;;
		x*c89)		BUILD_TYPE=c89;;
		x*c99)		BUILD_TYPE=c99;;
		x*gcc-c23)	BUILD_TYPE=gcc-c23;;
		x*gcc-c11)	BUILD_TYPE=gcc-c11;;
		x*gcc-stricter)	BUILD_TYPE=gcc-stricter;;
		x*gcc-normal)	BUILD_TYPE=gcc-normal;;
		x*gcc*)		BUILD_TYPE=gcc;;
		*)		BUILD_TYPE=cc;;
		esac
	fi
	sed -e s%`pwd`%SRCDIR%g run.log >>LOGS/`partition`-${BUILD_TYPE}-run.log
	rm -f run.log
else
	build ncurses "$@"
	build ncurses --enable-widec "$@"
	build thread "$@"
fi
