#!/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

%:
	dh $@ --with gir,gnome

override_dh_auto_configure:
	dh_auto_configure -- \
		-Ddocs=true \
		-Dinstalled_tests=false

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

infiles := \
	libgdk-pixbuf2.0-0.triggers \
	libgdk-pixbuf2.0-0.postinst \
	libgdk-pixbuf2.0-0.postrm

override_dh_auto_test:
ifeq ($(filter nocheck,$(DEB_BUILD_OPTIONS)),testsuitedisabled)
	# on slow arches the "slow" tests take longer than the timeout allowed
	# by upstream; allow even more time (-t is a multipiler)
	meson test --print-errorlogs -C $(CURDIR)/obj-$(DEB_HOST_GNU_TYPE) -t 5
endif

override_dh_install:
	find $(CURDIR)/debian/tmp -name *.la -print -delete
	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-pixbuf2.0-0-udeb/usr/share/mime/mime.cache
	# generate a loaders cache for the udeb
	mkdir -p debian/libgdk-pixbuf2.0-0-udeb/usr/lib/$(DEB_HOST_MULTIARCH)/gdk-pixbuf-2.0/2.10.0
	LD_LIBRARY_PATH=debian/libgdk-pixbuf2.0-0-udeb/usr/lib:$(LD_LIBRARY_PATH) \
		debian/libgdk-pixbuf2.0-0/usr/lib/$(DEB_HOST_MULTIARCH)/gdk-pixbuf-2.0/gdk-pixbuf-query-loaders \
		debian/libgdk-pixbuf2.0-0-udeb/usr/lib/$(DEB_HOST_MULTIARCH)/gdk-pixbuf-2.0/2.10.0/loaders/*.so \
		| sed 's;$(CURDIR)/debian/libgdk-pixbuf2.0-0-udeb;;g' \
		> debian/libgdk-pixbuf2.0-0-udeb/usr/lib/$(DEB_HOST_MULTIARCH)/gdk-pixbuf-2.0/2.10.0/loaders.cache
	chmod 0644 debian/libgdk-pixbuf2.0-0-udeb/usr/lib/$(DEB_HOST_MULTIARCH)/gdk-pixbuf-2.0/2.10.0/loaders.cache

override_dh_makeshlibs:
	dh_makeshlibs --add-udeb=libgdk-pixbuf2.0-0-udeb -X "/usr/lib/$(DEB_HOST_MULTIARCH)/gdk-pixbuf-2.0" -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)
