#!/usr/bin/make -f

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

# Explicitly disable this plugin which defaults to enabled
# unless we're on linux architectures.
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
ifeq (linux,$(DEB_HOST_ARCH_OS))
SYSPROF_PLUGIN = -Dplugin_sysprof=true
else
SYSPROF_PLUGIN = -Dplugin_sysprof=false
endif

ifneq (,$(filter amd64 arm64 armhf i386 mips mips64 mips64el mipsel powerpc ppc64 ppc64el s390x x32,$(DEB_HOST_ARCH)))
VALGRIND_PLUGIN = -Dplugin_valgrind=true
else
VALGRIND_PLUGIN = -Dplugin_valgrind=false
endif

# Enable Flatpak support on supported architectures
ifneq (,$(filter $(DEB_HOST_ARCH), amd64 arm64 armel armhf i386 mips mipsel mips64el ppc64el s390x hppa powerpc powerpcspe ppc64))
	FLATPAK_PLUGIN = -Dplugin_flatpak=true
else
	FLATPAK_PLUGIN = -Dplugin_flatpak=false
endif

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- \
		-Dnetwork_tests=false \
		$(FLATPAK_PLUGIN) \
		$(SYSPROF_PLUGIN) \
		$(VALGRIND_PLUGIN)

# force debian/tmp installation for now until we ship
# more than one binary package, then this can be dropped...
override_dh_auto_install:
	dh_auto_install --destdir=$(CURDIR)/debian/tmp

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

override_dh_auto_test:
	mkdir -p -m0700 $(CHECK_RUNTIME_DIR)
	HOME=$(CHECK_RUNTIME_DIR) dbus-run-session -- xvfb-run dh_auto_test

override_dh_install:
	rm -rf debian/tmp/usr/lib/python*/*-packages/gi/overrides/__pycache__/
	rm -rf debian/tmp/usr/include/gnome-builder-*
	dh_install -Xpkgconfig -Xgir-1.0

override_dh_missing:
	# see debian/not-installed and bgo#758266 + #804769
	dh_missing --fail-missing -Xpkgconfig

override_dh_makeshlibs:
	dh_makeshlibs -X/usr/lib/$(DEB_HOST_MULTIARCH)/gnome-builder

override_dh_shlibdeps:
	dh_shlibdeps -ldebian/gnome-builder/usr/lib/$(DEB_HOST_MULTIARCH)/gnome-builder/

override_dh_girepository:
	dh_girepository -ldebian/tmp/usr/share/gnome-builder/gir-1.0 \
	  /usr/lib/$(DEB_HOST_MULTIARCH)/gnome-builder/ \
	  /usr/lib/$(DEB_HOST_MULTIARCH)/gnome-builder/girepository-1.0/

override_dh_python3:
# https://bugs.debian.org/838342
	dh_python3 -X.*/plugins/meson_templates/resources/.*
