# Template file for 'gettext'
pkgname=gettext
version=0.22.5
revision=1
build_style=gnu-configure
configure_args="--disable-java --disable-native-java --disable-csharp
 --disable-libasprintf --enable-threads=posix --disable-rpath --without-emacs
 --disable-acl --with-included-glib --with-included-libcroco --without-git
 --with-included-libxml --with-included-libunistring --disable-openmp
 --disable-curses --without-examples --enable-nls
 gl_cv_func_gettimeofday_clobber=no"
# gettext will try to use xz bzip2 gzip to compress
# /usr/share/gettext/archive.dir.tar.<ext>
# Either:
# - Have xz in hostmakedepends; or
# - Let gettext-devel depends on bzip2
hostmakedepends="xz"
short_desc="Internationalized Message Handling Library and tools"
maintainer="Enno Boland <gottox@voidlinux.org>"
license="GPL-3.0-or-later"
homepage="https://www.gnu.org/software/gettext/"
changelog="https://git.savannah.gnu.org/cgit/gettext.git/plain/NEWS"
distfiles="${GNU_SITE}/${pkgname}/${pkgname}-${version}.tar.gz"
checksum=ec1705b1e969b83a9f073144ec806151db88127f5e40fe5a94cb6c8fa48996a0

subpackages="gettext-devel-examples gettext-devel gettext-devel-tools"
unset _intl_lib
if [ "$XBPS_TARGET_LIBC" = musl ]; then
	# force libintl
	configure_args+=" --with-included-gettext"
	subpackages+=" gettext-libs"
	_intl_lib=intl
else
	# on glibc, old gettext-libs conflicts with gettext
	replaces="gettext-libs>=0"
	conflicts="gettext-libs>=0"
fi

post_patch() {
	if [ "$XBPS_TARGET_LIBC" = musl ]; then
		grep -rl 'test fails on Linux with musl' gettext-tools/tests |
		xargs sed -i '/test fails on Linux with musl/s/^/Exit 77 /'
		sed -i 's/defined MUSL_LIBC/0/' \
			gettext-tools/gnulib-tests/test-canonicalize.c
		# return 7, not sure what's the problem
		# which means have openat(2) but ERANGE or ENOENT on getcwd(3)
		# not sure what is the problem here
		sed -i 's/test_long_name.*;/0;/' \
			gettext-tools/gnulib-tests/test-getcwd.c
	fi
}

post_install() {
	# don't overwrite musl's header
	if [ "$XBPS_TARGET_LIBC" = musl ]; then
		vmkdir usr/include/gettext
		mv ${DESTDIR}/usr/include/libintl.h ${DESTDIR}/usr/include/gettext/libintl.h
	fi
}

gettext-devel-examples_package() {
	short_desc+=" - examples for development"
	pkg_install() {
		vmove usr/share/doc/gettext
	}
}

gettext-devel_package() {
	depends="${sourcepkg}>=${version}_${revision}
	 gettext-devel-tools>=${version}_${revision}"
	if [ -n "${_intl_lib}" ]; then
		depends+=" gettext-libs>=${version}_${revision}"
	else
		replaces="gettext-libs>=0"
	fi
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/include
		vmove "usr/lib/*.a"
		# libgettext{lib,src}-$version.so need to be in the main package
		for _lib in ${_intl_lib} gettextlib gettextpo gettextsrc textstyle
		do
			vmove "usr/lib/lib${_lib}.so"
		done
		vmove usr/share/man/man3
	}
}

gettext-devel-tools_package() {
	depends="${sourcepkg}>=${version}_${revision} tar xz"
	conflicts="gettext-tiny>=0 gettext-tiny-devel>=0"
	short_desc+=" - development tools"
	pkg_install() {
		vmove usr/share/aclocal
		vmove usr/bin/autopoint
		vmove usr/bin/gettextize
		vmove usr/share/man/man1/autopoint.1
		vmove usr/share/man/man1/gettextize.1
		vmove usr/share/gettext
	}
}

gettext-libs_package() {
	short_desc+=" - shared libraries"
	pkg_install() {
		vmove "usr/lib/libintl.so.*"
	}
}
