#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_LDFLAGS_MAINT_APPEND = -Wl,-O1 -Wl,-z,defs -Wl,--as-needed

include /usr/share/dpkg/architecture.mk

ifneq (,$(filter libgdk-pixbuf2.0-doc,$(shell dh_listpackages)))
  BUILD_DOCS:=-Dgtk_doc=true
else
  BUILD_DOCS:=-Dgtk_doc=false
endif

%:
	dh $@ --with gir,gnome

override_dh_auto_configure:
	# replace GIF in testcase which was broken in the LZW code size, not
	# the values of the pixels uncovered after fix for CVE-2021-44648
	cp -a debian/invalid-colors.gif tests/test-images/gif-test-suite/invalid-colors.gif
	dh_auto_configure -- \
		$(BUILD_DOCS) \
		-Dinstalled_tests=true \
		-Dintrospection=enabled \
		$(NULL)

# temporary directory for the minimal MIME cache generation
mimedir := debian/build-mime
mimexml := /usr/share/mime/packages/freedesktop.org.xml

infiles := \
	libgdk-pixbuf-2.0-0.triggers \
	libgdk-pixbuf-2.0-0.postinst \
	libgdk-pixbuf-2.0-0.postrm

override_dh_auto_test:
ifeq ($(filter nocheck,$(DEB_BUILD_OPTIONS)),)
	# on slow arches the "slow" tests take longer than the timeout allowed
	# by upstream; allow even more time (-t is a multipiler)
ifneq (,$(filter $(DEB_HOST_ARCH),kfreebsd-amd64))
	meson test --print-errorlogs -C $(CURDIR)/obj-$(DEB_HOST_GNU_TYPE) -t 5 --no-suite=flaky || true
else
	meson test --print-errorlogs -C $(CURDIR)/obj-$(DEB_HOST_GNU_TYPE) -t 5 --no-suite=flaky
endif
endif

override_dh_install:
	dh_install
	# process .in files
	set -e; for file in $(infiles); do \
		sed -e"s/#MULTIARCH#/$(DEB_HOST_MULTIARCH)/g" \
			debian/$${file}.in > debian/$$file; \
	done
	# generate a minimal MIME cache for the udeb
	mkdir -p $(mimedir)/packages
	[ -f $(mimexml) ]
	xsltproc -nonet debian/keep-png-only.xsl $(mimexml) \
		> $(mimedir)/packages/png.freedesktop.org.xml
	update-mime-database $(mimedir)
	install -m 0644 -D $(mimedir)/mime.cache debian/libgdk-pixbuf-2.0-0-udeb/usr/share/mime/mime.cache
	# generate a loaders cache for the udeb
	mkdir -p debian/libgdk-pixbuf-2.0-0-udeb/usr/lib/$(DEB_HOST_MULTIARCH)/gdk-pixbuf-2.0/2.10.0
	LD_LIBRARY_PATH=debian/libgdk-pixbuf-2.0-0-udeb/usr/lib:$(LD_LIBRARY_PATH) \
		debian/libgdk-pixbuf-2.0-0/usr/lib/$(DEB_HOST_MULTIARCH)/gdk-pixbuf-2.0/gdk-pixbuf-query-loaders \
		debian/libgdk-pixbuf-2.0-0-udeb/usr/lib/$(DEB_HOST_MULTIARCH)/gdk-pixbuf-2.0/2.10.0/loaders/*.so \
		| sed 's;$(CURDIR)/debian/libgdk-pixbuf-2.0-0-udeb;;g' \
		> debian/libgdk-pixbuf-2.0-0-udeb/usr/lib/$(DEB_HOST_MULTIARCH)/gdk-pixbuf-2.0/2.10.0/loaders.cache
	chmod 0644 debian/libgdk-pixbuf-2.0-0-udeb/usr/lib/$(DEB_HOST_MULTIARCH)/gdk-pixbuf-2.0/2.10.0/loaders.cache

override_dh_makeshlibs-arch:
	dh_makeshlibs -plibgdk-pixbuf-2.0-0 --add-udeb=libgdk-pixbuf-2.0-0-udeb -X "/usr/lib/$(DEB_HOST_MULTIARCH)/gdk-pixbuf-2.0" -V -- -c4
	dh_makeshlibs --remaining-packages -V -- -c4

override_dh_missing:
	dh_missing --fail-missing

override_dh_clean:
	dh_clean
	set -e; for file in $(infiles); do \
		rm -f debian/$$file; \
	done
	rm -rf $(mimedir)

override_dh_builddeb:
	# on Ubuntu PNGs are optimised using 'optipng', but the (installed)
	# testsuite relies on them being unmangled
	NO_PNG_PKG_MANGLE=1 dh_builddeb -pgdk-pixbuf-tests
	dh_builddeb --remaining-packages

override_dh_strip_nondeterminism:
	dh_strip_nondeterminism -Xinstalled-tests