#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_LDFLAGS_MAINT_APPEND = -Wl,-O1 -Wl,-z,defs
export DPKG_GENSYMBOLS_CHECK_LEVEL = 4

binaries := $(shell dh_listpackages)

ifeq (,$(findstring nodoc, $(DEB_BUILD_OPTIONS)))
ifneq ($(filter libgladeui-doc,$(binaries)),)
	ENABLE_DOCS = -Dgtk_doc=true
endif
endif
ifneq (,$(filter $(DEB_HOST_ARCH), ia64 kfreebsd-amd64 kfreebsd-i386 sh4))
	ENABLE_WEBKIT = -Dwebkit2gtk=disabled
endif
ifeq (,$(filter $(DEB_HOST_ARCH), amd64 arm64 armel armhf i386 mips64el mipsel ppc64el s390x powerpc ppc64 riscv64))
	ENABLE_GJS = -Dgjs=disabled
endif

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- -Dauto_features=enabled $(ENABLE_DOCS) $(ENABLE_WEBKIT) $(ENABLE_GJS)

execute_before_dh_makeshlibs:
ifneq (,$(filter $(DEB_HOST_ARCH), ia64 kfreebsd-amd64 kfreebsd-i386 sh4))
	grep -E -v 'webkit|gjs' debian/libgladeui-2-13.symbols > debian/libgladeui-2-13.symbols.$(DEB_HOST_ARCH)
else ifeq (,$(filter $(DEB_HOST_ARCH), amd64 arm64 armel armhf i386 mips64el mipsel ppc64el s390x powerpc ppc64 riscv64))
	grep -E -v 'gjs' debian/libgladeui-2-13.symbols > debian/libgladeui-2-13.symbols.$(DEB_HOST_ARCH)
endif

TEST_COMMAND = dh_auto_test -- --wrap="dbus-run-session -- xvfb-run -a" \
	--timeout-multiplier 5

override_dh_auto_test:
# Tests are failing without gjs support, see https://gitlab.gnome.org/GNOME/glade/-/issues/495
ifeq (,$(filter $(DEB_HOST_ARCH), amd64 arm64 armel armhf i386 mips64el mipsel ppc64el s390x powerpc ppc64 riscv64))
	$(TEST_COMMAND) || true
else
	$(TEST_COMMAND)
endif

# There's unclear precedent in the Debian archive for which package
# should install the ITS files. These files probably aren't needed yet anyway.
override_dh_missing:
	dh_missing -X/usr/share/gettext/

execute_before_dh_clean:
	rm -f debian/libgladeui-2-13.symbols.$(DEB_HOST_ARCH)
