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

# https://lists.debian.org/debian-devel/2022/02/msg00259.html
ifeq "$(shell which valgrind > /dev/null && echo true)" "true"
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 \
		-Dplugin_update_manager=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
	# Meson 0.60.0 installs to python3/dist-packages, older versions
	# installed to python3.x/site-packages. Stay compatible with both
	# for now
	if ! [ -d debian/tmp/usr/lib/python3/dist-packages/gi/overrides ]; then \
		install -d debian/tmp/usr/lib/python3/dist-packages/gi; \
		mv debian/tmp/usr/lib/python3*/site-packages/gi/overrides \
			debian/tmp/usr/lib/python3/dist-packages/gi; \
	fi

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_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/.*
