# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../ncurses/ncurses.conf
# Copyright (C) 2006 - 2010 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE Project
# Copyright (C) 1998 - 2003 Clifford Wolf
#
# More information can be found in the files COPYING and README.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

prefix=
set_confopt

if ! atstage native; then
	var_append extraconfopt " " "--without-cxx --without-cxx-binding"
fi

# saving our local patches and empty the patchfiles variable
localpatches=$patchfiles
patchfiles=

# append official patches
for x in `match_source_file -p 'patch'` ; do
	var_append patchfiles " " "$x"
done

# append our local patches
var_append patchfiles " " "$localpatches"

# on dietlibc we won't use NLS
[ "$SDECFG_LIBC" != dietlibc ] || SDECFG_DISABLE_NLS=1

# on systems having NLS support we build ncurses twice
if [ "$SDECFG_DISABLE_NLS" != 1 ]; then
	buildloops=2
fi

# sparc64 may need extra love
if [ "$arch" == "sparc64" -a "$SDECFG_SPARC64_32BIT" = 1 ]; then
	# double the loops
	buildloops=$( expr $buildloops \* 2 )
fi

configscript="../configure"

ncurses_preconf() {
	if [ $buildloop -eq 1 ]; then
		# first loop is easy, always simply narrowc
		mkdir -p narrowc
		cd narrowc
	else
		cd ..
		# then the things get a bit uglier
		if [ "$SDECFG_DISABLE_NLS" != 1 ]; then
			# NLS support
			if [ $buildloop -eq 2 ]; then
				# loop 2: normal wide
				mkdir -p widec
				cd widec
				var_append confopt " " "--enable-widec"
				var_append makeinstopt " " "includedir=$includedir/ncursesw"
			elif [ $buildloop -eq 3 ]; then
				# loop 3: altered arch, narrow.
				var_remove GCC_WRAPPER_INSERT ' ' '-m64'
				alter_arch

				mkdir -p narrowc_32
				cd narrowc_32
				var_remove makeinstopt " " "includedir=$includedir/ncursesw"
			else
				# loop 4: alreday altered arch, wide.
				mkdir -p widec_32
				cd widec_32
				var_append confopt " " "--enable-widec"
				var_append makeinstopt " " "includedir=$includedir/ncursesw"
			fi
		else
			# no NLS support
			# loop 2: altered arch, narrow.
			var_remove GCC_WRAPPER_INSERT ' ' '-m64'
			alter_arch

			mkdir -p narrowc_32
			cd narrowc_32
		fi
	fi
}

hook_add preconf 3 "ncurses_preconf"

[ "$SDECFG_STATIC" != 1 ] && var_append extraconfopt " " "--with-shared"

var_append extraconfopt " " "--with-normal"

# Fine-Tuning:
#
# make tic use symbolic links not hard links
var_append extraconfopt " " "--enable-symlinks"

# Extensions:
#
# compile with SIGWINCH handler
var_append extraconfopt " " "--enable-sigwinch"

# Development Code:
#
# compile with hard-tabs code
var_append extraconfopt " " "--enable-hard-tabs"

# Experimental Code:
#
# compile with $COLORFGBG code
var_append extraconfopt " " "--enable-colorfgbg"

# always create compatibility symlinks for curses
make_curses_symlinks() {
	echo "Creating compatibility symlinks for curses."

	if [ "$SDECFG_STATIC" != 1 ]; then
		ln -svf libncurses.so $root$libdir/libcurses.so
	fi

	ln -svf libncurses.a $root$libdir/libcurses.a
}
hook_add postmake 5 "make_curses_symlinks"

remove_include_termcap() {
	echo "Removing 'termcap.h' from include/Makefile for 'make install'."
	sed -i 's,termcap.h,,g' include/Makefile
}

make_termcap_symlinks() {
	echo "Creating compatibility symlinks for termcap."

	if [ "$SDECFG_STATIC" != 1 ]; then
		ln -svf libncurses.so $root$libdir/libtermcap.so
	fi

	ln -svf libncurses.a $root$libdir/libtermcap.a
}

if pkginstalled termcap ; then
	echo_status "Found termcap package ->" \
			"disable ncurses internal termcap library."
	var_append extraconfopt " " "--disable-termcap"
	hook_add inmake 5 "remove_include_termcap"
else
	hook_add postmake 6 "make_termcap_symlinks"
fi