# ncurses - Terminal UI library, needed by procps-ng (top/watch)
name=ncurses
version=6.5
release=1
desc="Terminal UI library, needed by procps-ng (top/watch)"
url="https://invisible-island.net/ncurses/"
license="MIT"
depend="glibc"
makedepend=""
replaces="netbsd-curses"
source="https://invisible-island.net/archives/ncurses/ncurses-6.5.tar.gz"
sha256="SKIP"

build() {
	cd "ncurses-6.5"
	# --with-shared/--enable-widec/--with-termlib: the plain ./configure
	# default only produces a static, narrow-char libcurses.a - every real
	# consumer (readline, procps-ng's top/watch) links against the shared,
	# wide-char sonames (libtinfo.so.6, libncursesw.so.6) this produces
	# instead.
	./configure \
		--prefix=/usr \
		--sysconfdir=/etc \
		--libdir=/usr/lib \
		--with-shared \
		--without-static \
		--without-tests \
		--with-termlib \
		--enable-widec \
		--enable-pc-files \
		--with-pkg-config-libdir=/usr/lib/pkgconfig \
		--disable-stripping
	# -j1: ncurses' own Makefiles have a real, well-known parallel-build
	# race (panel/menu/form need curses.priv.h, generated by the main
	# ncurses/ subdir's build, but nothing serializes that dependency
	# correctly across gmake's per-directory recursion) - hit directly as
	# "No rule to make target '../../ncurses/curses.priv.h'" at -j28.
	gmake -j1
}

package() {
	cd "ncurses-6.5"
	gmake DESTDIR="$pkgdir" install
	# ncurses builds tic/infocmp etc as *w (wide) binaries and leaves the
	# plain names as-is (unbuilt) when --enable-widec is used without also
	# building a parallel narrow-char tree - symlink the widths in so
	# anything that just calls "tic" or "ncurses.h" still finds them.
	for f in "$pkgdir"/usr/bin/*w; do
		[ -e "$f" ] || continue
		ln -sf "$(basename "$f")" "$pkgdir/usr/bin/$(basename "${f%w}")"
	done
	ln -sf ncursesw6 "$pkgdir/usr/include/ncurses" 2>/dev/null || :
	# --enable-widec suffixes every library name with 'w' (libtinfow,
	# libncursesw, ...) instead of replacing the plain ones - real-world
	# consumers (procps-ng's top/watch, readline) link against the plain
	# unsuffixed sonames (libtinfo.so.6), which this build never produces on
	# its own. The wide build is API/ABI-compatible as a superset, so a
	# symlink is enough - same fix every mainstream distro's ncurses package
	# applies for this exact reason.
	cd "$pkgdir/usr/lib"
	for lib in ncurses tinfo form panel menu; do
		for f in "$lib"w.so*; do
			[ -e "$f" ] || continue
			ln -sf "$f" "$(printf '%s' "$f" | sed "s/${lib}w/${lib}/")"
		done
	done
}
