#!/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 -Wl,--as-needed

binaries := $(shell dh_listpackages)

ifeq (,$(findstring nodoc, $(DEB_BUILD_OPTIONS)))
ifneq ($(filter libgladeui-doc,$(binaries)),)
	ENABLE_DOCS = -Dgtk_doc=true
endif
endif
ifneq ($(DEB_HOST_ARCH_OS), linux)
	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)

override_dh_makeshlibs:
ifeq (,$(filter $(DEB_HOST_ARCH), amd64 arm64 armel armhf i386 mips64el mipsel ppc64el s390x powerpc ppc64 riscv64))
ifeq ($(DEB_HOST_ARCH_OS), linux)
	grep -E -v 'gjs' debian/libgladeui-2-13.symbols > debian/libgladeui-2-13.symbols.$(DEB_HOST_ARCH)
else
	grep -E -v 'webkit|gjs' debian/libgladeui-2-13.symbols > debian/libgladeui-2-13.symbols.$(DEB_HOST_ARCH)
endif
endif
	dh_makeshlibs -- -c4

CHECK_RUNTIME_DIR= $(CURDIR)/debian/tmp/run

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	mkdir -p -m0700 $(CHECK_RUNTIME_DIR)
# 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))
	HOME=$(CHECK_RUNTIME_DIR) XDG_RUNTIME_DIR=$(CHECK_RUNTIME_DIR) \
		dbus-run-session -- xvfb-run dh_auto_test || true
else
	HOME=$(CHECK_RUNTIME_DIR) XDG_RUNTIME_DIR=$(CHECK_RUNTIME_DIR) \
		dbus-run-session -- xvfb-run dh_auto_test
endif
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 --fail-missing -X/usr/share/gettext/

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