#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

NJOBS := -j$(shell getconf _NPROCESSORS_ONLN)

%:
	dh $@ --with autoreconf

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_MULTIARCH  ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
buildroot = $(CURDIR)/debian/tmp

override_dh_auto_configure:
	find doc -type f -executable -print -exec chmod 0644 {} ';'
	dh_testdir
	# Add here commands to configure the package.
	CC_FOR_BUILD=cc ./configure \
		--enable-static --enable-shared \
		--host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/opt/alt/libxml2/usr \
		--datadir=/opt/alt/libxml2/usr/share \
		--libdir=/opt/alt/libxml2/usr/lib/$(DEB_HOST_MULTIARCH) \
		--without-python

override_dh_auto_build:
	$(MAKE) $(NJOBS) VERBOSE=1

override_dh_auto_install:
	make install DESTDIR=$(buildroot)
	find -name parser.h
	find ${buildroot} -type f -name '*.la' -print -delete
	rm -rf ${buildroot}${_datadir}/doc/
	gzip -9 -c doc/libxml2-api.xml > doc/libxml2-api.xml.gz
	# Generate the alt-libxml2-devel .so symlink list instead of shipping a
	# static debian/alt-libxml2-devel.links. A .links file is a literal path
	# list: it cannot expand $(DEB_HOST_MULTIARCH), so the hardcoded
	# x86_64-linux-gnu it used to carry made dh_link ship a dangling symlink on
	# arm64 - nothing could link against the library there, and
	# 0_integrity_check.bats "symlinks are correct and exists" failed on
	# debian12.arm64 and debian13.arm64. The sibling debian/*.install files
	# sidestep this with a lib/*/ wildcard, which dh_link does not accept.
	# Generating the file keeps dh_link itself on default behaviour, so this
	# works unchanged on debhelper 9 (debian/compat, and our oldest targets
	# ubuntu16.04 and ubuntu18.04) through 13.
	# The real name is read from the installed library rather than spelled out,
	# so a version bump cannot leave the link dangling the way the
	# 2.9.7 -> 2.10.2 bump once did.
	set -e; \
	libdir=opt/alt/libxml2/usr/lib/$(DEB_HOST_MULTIARCH); \
	soname=$$(cd $(buildroot)/$$libdir && ls libxml2.so.[0-9]*.[0-9]*.[0-9]* | head -1); \
	test -n "$$soname"; \
	echo "$$libdir/$$soname $$libdir/libxml2.so" > debian/alt-libxml2-devel.links

override_dh_auto_clean:
	rm -f debian/alt-libxml2-devel.links
