# Template build file for 'ncurses'.
pkgname=ncurses
version=5.9
revision=6
short_desc="A System V Release 4.0 curses emulation library"
maintainer="Juan RP <xtraeme@gmail.com>"
homepage="http://www.gnu.org/software/ncurses/"
license="MIT"
distfiles="http://ftp.gnu.org/pub/gnu/ncurses/$pkgname-$version.tar.gz"
checksum=9046298fb440324c9d4135ecea7879ffed8546dd1b58e59430ea07a4633f563b
long_desc="
 The ncurses (new curses) library is a freeware emulation of System V Release
 4.0 curses. It uses terminfo format, supports pads and color and multiple
 highlights and forms characters and function-key mapping, and has all the
 other SYSV-curses enhancements over BSD curses. The distribution includes
 the library and support utilities, including a terminfo compiler tic, a 
 ecompiler infocmp, clear, tput, tset, and a termcap conversion tool captoinfo.
 Full manual pages are provided for the library and tools."

do_configure() {
	mkdir ncurses-build ncursesw-build
	cd ${wrksrc}/ncursesw-build

	# Override config.sub with ours.
	cp -f ${XBPS_CROSSPFDIR}/config.sub ${wrksrc}

	# widec build
	CFLAGS="$CFLAGS -fPIC" \
		../configure ${CONFIGURE_SHARED_ARGS} \
		--enable-widec --with-shared --without-debug \
		--with-manpage-symlinks --without-ada --enable-ext-colors \
		--without-tests

	cd ${wrksrc}/ncurses-build
	# non-widec build
	CFLAGS="$CFLAGS -fPIC" \
		../configure ${CONFIGURE_SHARED_ARGS} --with-shared \
		--without-debug --without-ada --without-tests
}

do_build() {
	cd ${wrksrc}/ncursesw-build
	make ${makejobs}

	cd ${wrksrc}/ncurses-build
	make ${makejobs}
}

do_install() {
	cd ${wrksrc}/ncursesw-build
	if [ "$CROSS_BUILD" ]; then
		sed -e "s#install : install.data install.libs#install: install.libs#g" -i misc/Makefile
	fi
	make DESTDIR=${DESTDIR} install

	# Fool packages looking to link to non-wide-character ncurses libraries
	for lib in curses ncurses form panel menu; do
		rm -f ${DESTDIR}/usr/lib/lib${lib}.so
		echo "INPUT(-l${lib}w)" >${DESTDIR}/usr/lib/lib${lib}.so
		chmod 755 ${DESTDIR}/usr/lib/lib${lib}.so
		ln -sf lib${lib}w.a ${DESTDIR}/usr/lib/lib${lib}.a
	done
	ln -sf libncurses++w.a ${DESTDIR}/usr/lib/libncurses++.a

	# Some packages look for -lcurses during build
	rm -f ${DESTDIR}/usr/lib/libcursesw.so
	echo "INPUT(-lncursesw)" >${DESTDIR}/usr/lib/libcursesw.so
	chmod 755 ${DESTDIR}/usr/lib/libcursesw.so
	ln -sf libncurses.so ${DESTDIR}/usr/lib/libcurses.so
	ln -sf libncursesw.a ${DESTDIR}/usr/lib/libcursesw.a
	ln -sf libncurses.a ${DESTDIR}/usr/lib/libcurses.a

	# non-widec compatibility library
	cd ${wrksrc}/ncurses-build
	install -Dm755 lib/libncurses.so.${version} \
		${DESTDIR}/usr/lib/libncurses.so.${version}

	# Create compat symlinks.
	for f in curses ncurses form panel menu; do
		ln -sfr ${DESTDIR}/usr/lib/lib${f}w.so.6 \
			${DESTDIR}/usr/lib/lib${f}w.so.5
	done

	# Remove broken symlink.
	rm -f ${DESTDIR}/usr/lib/terminfo

        ln -sfr ${DESTDIR}/usr/bin/ncursesw6-config \
                ${DESTDIR}/usr/bin/ncursesw5-config
        ln -sfr ${DESTDIR}/usr/bin/ncursesw6-config \
                ${DESTDIR}/usr/bin/ncurses5-config
}

ncurses_package() {
	depends="ncurses-base>=${version}"
	pkg_install() {
		vmove usr/bin
		vmove usr/share/man/man1
	}
}

ncurses-libs_package() {
	short_desc+=" -- shared libraries"
	replaces="ncursesw>=0"

	pkg_install() {
		vmove "usr/lib/*.so*"
	}
}

ncurses-devel_package() {
	depends="ncurses-libs>=${version}"
	short_desc+=" -- development files"
	replaces="ncursesw-devel>=0"

	pkg_install() {
		vmove usr/include
		vmove "usr/lib/*.a"
		vmove usr/share/man/man3
		vmove usr/share/man/man1/ncursesw6-config.1
	}
}

if [ -z "$CROSS_BUILD" ]; then

ncurses-base_package() {
	short_desc+=" -- base terminfo files"
	noarch=yes

	pkg_install() {
		cat ${FILESDIR}/base-files | while read line; do
			vmove ${line}
		done
	}
}

ncurses-term_package() {
	depends="ncurses-base>=${version}"
	short_desc+=" -- full terminal descriptions"
	noarch=yes

	pkg_install() {
		vmove usr/share/tabset
		vmove usr/share/terminfo
	}
}

fi # !CROSS_BUILD