# Template file for 'libtool'
pkgname=libtool
version=2.4.7
revision=7
build_style=gnu-configure
hostmakedepends="texinfo perl automake help2man xz gnulib tar"
short_desc="Generic library support script"
maintainer="Enno Boland <gottox@voidlinux.org>"
license="GPL-2.0-or-later"
homepage="http://www.gnu.org/software/libtool"
distfiles="${GNU_SITE}/libtool/$pkgname-$version.tar.xz"
checksum=4f7f217f057ce655ff22559ad221a0fd8ef84ad1fc5fcb6990cecc333aa1635d

if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
	# Musl always searches LD_LIBRARY_PATH before RPATH and RUNPATH
	configure_args="lt_cv_shlibpath_overrides_runpath=yes"
fi

post_extract() {
	chmod -R +rw .
}

do_configure() {
	./bootstrap --force --no-git --skip-po \
		--gnulib-srcdir=/usr/share/gnulib/ --copy
	./configure ${configure_args} SED=/bin/sed GREP=/bin/grep
}

post_install() {
	local _canonical_host _host_os

	# this is necessary, because when cross building, libtool will pick up
	# the binary names of the crosstoolchain, which are prefixed with the
	# short triplet (i.e. arch-linux-env instead of arch-unknown-linux-env)
	# and those are not present in a real native environment; also the
	# sysroot var will be set and cflags will include extra cross-related
	# things that need to go; the target libtool script is meant to be used
	# in native environments, not in cross environments, so patch the script
	if [ "$CROSS_BUILD" ]; then
		# e.g. AR="armv7l-linux-gnueabihf-ar" becomes AR="ar"
		vsed -i -e "s,\([A-Z]\+\)=\"${XBPS_CROSS_TRIPLET}\-\(.*\)\",\1=\"\2\",g" \
		 ${PKGDESTDIR}/usr/bin/libtool

		# clear out any sysroot present
		vsed -i -e "s,^lt_sysroot=.*,lt_sysroot=," \
		 ${PKGDESTDIR}/usr/bin/libtool

		# clear out sysroot include path
		vsed -i -e "s,\-I${XBPS_CROSS_BASE}/usr/include,,g" \
		 ${PKGDESTDIR}/usr/bin/libtool

		# Strip cross sysroot from paths
		vsed -i -e "s,${XBPS_CROSS_BASE},,g" \
		 ${PKGDESTDIR}/usr/bin/libtool

		if [ "$XBPS_TARGET_WORDSIZE" = "64" ]; then
			vsed -i \
			 -e "s,/${XBPS_CROSS_TRIPLET}/lib/../lib64,/lib64,g" \
			 ${PKGDESTDIR}/usr/bin/libtool
		else
			vsed -i \
			 -e "s,/${XBPS_CROSS_TRIPLET}/lib,/lib,g" \
			 ${PKGDESTDIR}/usr/bin/libtool
		fi

		# canonicalize host_alias, replace build(_alias,_os)
		_canonical_host=$(grep "^host=" ${PKGDESTDIR}/usr/bin/libtool | sed 's/host=//')
		_host_os=$(grep "^host_os=" ${PKGDESTDIR}/usr/bin/libtool | sed 's/host_os=//')
		vsed -i \
		 -e "s,^host_alias=.*,host_alias=${_canonical_host}," \
		 -e "s,^build_alias=.*,build_alias=${_canonical_host}," \
		 -e "s,^build=.*,build=${_canonical_host}," \
		 ${PKGDESTDIR}/usr/bin/libtool

		# The host_os and build_os can be the same (i.e. linux-musl or linux-gnu)
		if ! grep "build_os=${_host_os}" ${PKGDESTDIR}/usr/bin/libtool >/dev/null 2>&1; then
			vsed -i \
			 -e "s,^build_os=.*,build_os=${_host_os}," \
			 ${PKGDESTDIR}/usr/bin/libtool
		fi
	fi
}

libltdl-devel_package() {
	depends="libltdl-${version}_${revision}"
	short_desc="GNU Libtool Dynamic Module Loader development files"
	pkg_install() {
		vmove usr/include
		vmove "usr/lib/*.a"
		vmove "usr/lib/*.so"
		vmove usr/share/aclocal/ltdl.m4
		vmove usr/share/libtool/libltdl
	}
}
libltdl_package() {
	short_desc="Runtime libraries for GNU Libtool Dynamic Module Loader"
	pkg_install() {
		vmove "usr/lib/libltdl.so.*"
	}
}
