#!/bin/sh
# $Id: build-clang-ncurses,v 1.11 2023/08/26 20:48:44 tom Exp $
# vile:ts=4 sw=4
# clang won't handle gnat/Ada95, but should handle the other features...

: ${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"
	OPTS="$OPTS --without-ada"

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

	case `partition` in
	muskrat|magpie)
		export CXXFLAGS="$CXXFLAGS --std=c++14"
		OPTS="$OPTS --enable-string-hacks"
		;;
	*openbsd*|*netbsd*|*freebsd*)
		OPTS="$OPTS --enable-string-hacks"
		OPTS="$OPTS --with-hashed-db"
		;;
	esac

	. openbsd-lld-noleaks

	echo "Building $CFG $*"
	cfg-$CFG "$@" $OPTS \
		$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 \
		--with-versioned-syms \
		--without-debug \
		--without-normal \
		"$@" && \
	( $MAKE && \
		check-ncu-pconfig && \
		$MAKE install && \
		$MAKE uninstall && \
		save-ncurses && \
		echo "PASSED-Building $CFG $*" ) ||
		echo "FAILED-Building $CFG $*"
	clean
}

if test -z "$BUILD_LOG" ; then
	clean

	BUILD_LOG=run.log with-clang run-log $0
	sed -e s%`pwd`%SRCDIR%g run.log >>LOGS/`partition`-clang.log
	rm -f run.log
else
	build ncurses "$@"
	build ncurses --enable-widec "$@"
	build thread "$@"
fi
