# Template file for 'ncurses'
pkgname=ncurses
version=6.4
revision=2
bootstrap=yes
configure_args="--enable-big-core"
short_desc="System V Release 4.0 curses emulation library"
maintainer="Enno Boland <gottox@voidlinux.org>"
license="MIT"
homepage="https://invisible-island.net/ncurses/"
changelog="https://invisible-island.net/ncurses/announce.html"
distfiles="https://invisible-island.net/archives/ncurses/ncurses-${version}.tar.gz"
checksum=6931283d9ac87c5073f30b6290c4c75f21632bb4fc3603ac8100812bed248159

depends="ncurses-base-${version}_${revision}"

do_configure() {
	export CFLAGS="$CFLAGS -fPIC"
	export BUILD_CFLAGS="$BUILD_CFLAGS -fPIC"

	mkdir -p ncurses-build ncursesw-build

	cd ${wrksrc}/ncursesw-build
	# widec build
	../configure ${configure_args} \
		--enable-widec --with-shared --without-debug \
		--with-manpage-symlinks --with-manpage-format=normal \
		--without-ada --enable-ext-colors --with-xterm-kbs=del \
		--without-tests --enable-pc-files \
		--with-pkg-config-libdir=/usr/lib/pkgconfig \
		ac_cv_path_ac_pt_PKG_CONFIG=/usr/bin/pkg-config \
		PKG_CONFIG_LIBDIR=/usr/lib/pkgconfig \
		BUILD_CFLAGS="$BUILD_CFLAGS"

	cd ${wrksrc}/ncurses-build
	# non-widec build
	../configure ${configure_args} --with-shared \
		--without-debug --without-ada --without-tests --with-xterm-kbs=del \
		--enable-pc-files --with-pkg-config-libdir=/usr/lib/pkgconfig \
		ac_cv_path_ac_pt_PKG_CONFIG=/usr/bin/pkg-config \
		PKG_CONFIG_LIBDIR=/usr/lib/pkgconfig \
		BUILD_CFLAGS="$BUILD_CFLAGS"
}

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

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

do_install() {
	vlicense COPYING

	cd ${wrksrc}/ncursesw-build
	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}
	ln -sf libncurses.so.${version} \
		${DESTDIR}/usr/lib/libncurses.so.${version:0:1}

	# Create libtinfo symlinks.
	ln -sfr ${DESTDIR}/usr/lib/libncursesw.so \
		${DESTDIR}/usr/lib/libtinfo.so
	ln -sfr ${DESTDIR}/usr/lib/libncursesw.so.${version} \
		${DESTDIR}/usr/lib/libtinfo.so.${version}
	ln -sfr ${DESTDIR}/usr/lib/libtinfo.so.${version} \
		${DESTDIR}/usr/lib/libtinfo.so.${version:0:1}
	ln -sfr ${DESTDIR}/usr/lib/pkgconfig/ncursesw.pc \
		${DESTDIR}/usr/lib/pkgconfig/tinfo.pc

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

	if [ "$CROSS_BUILD" ]; then
		sed -i -e "s,-L$XBPS_CROSS_BASE,-L,g" ${DESTDIR}/usr/bin/ncursesw6-config
	fi
}

post_install() {
	# resolve conflict with *-terminfo packages
	rm ${DESTDIR}/usr/share/terminfo/a/alacritty{,-direct}
	rm ${DESTDIR}/usr/share/terminfo/d/dvtm{,-256color}
	rm ${DESTDIR}/usr/share/terminfo/s/st{,-256color}
	rm ${DESTDIR}/usr/share/terminfo/f/foot{,-direct,+base}
}

ncurses-libs_package() {
	short_desc+=" -- shared libraries"
	pkg_install() {
		vmove "usr/lib/libform*.so.*"
		vmove "usr/lib/libmenu*.so.*"
		vmove "usr/lib/libncurses*.so.*"
		vmove "usr/lib/libpanel*.so.*"
	}
}
ncurses-devel_package() {
	depends="ncurses-libs-${version}_${revision}"
	short_desc+=" - development files"
	pkg_install() {
		vmove "usr/bin/ncurses*-config"
		vmove usr/include
		vmove usr/lib/pkgconfig/ncursesw.pc
		vmove usr/lib/pkgconfig/formw.pc
		vmove usr/lib/pkgconfig/menuw.pc
		vmove usr/lib/pkgconfig/ncurses++w.pc
		vmove usr/lib/pkgconfig/panelw.pc
		vmove "usr/lib/*.a"
		vmove "usr/lib/libcurses*.so"
		vmove "usr/lib/libform*.so"
		vmove "usr/lib/libmenu*.so"
		vmove "usr/lib/libncurses*.so"
		vmove "usr/lib/libpanel*.so"
		vmove usr/share/man/man3
		vmove usr/share/man/man1/ncursesw6-config.1
	}
}
ncurses-base_package() {
	short_desc+=" - base terminfo files"
	pkg_install() {
		cat ${FILESDIR}/base-files | while read line; do
			vmove ${line}
		done
	}
}
ncurses-term_package() {
	depends="ncurses-base-${version}_${revision}"
	short_desc+=" - full terminal descriptions"
	pkg_install() {
		vmove usr/share/tabset
		vmove usr/share/terminfo
	}
}

ncurses-libtinfo-libs_package() {
	depends="ncurses-libs-${version}_${revision}"
	short_desc+=" - libtinfo.so symlink"
	pkg_install() {
		vmove "usr/lib/libtinfo*.so.*"
	}
}

ncurses-libtinfo-devel_package() {
	lib32mode=full
	depends="ncurses-devel-${version}_${revision}"
	depends+=" ncurses-libtinfo-libs-${version}_${revision}"
	short_desc+=" - libtinfo.so symlink - development files"
	pkg_install() {
		vmove usr/lib/libtinfo.so
		vmove "usr/lib/pkgconfig/tinfo.pc"
	}
}
