#!/usr/bin/make -f

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

%:
	dh $@

override_dh_autoreconf:
	dh_autoreconf --as-needed

CONFFLAGS = \
	-Dprofiler=false \
	-Dremote_desktop=false

ifeq ($(DEB_HOST_ARCH_OS),linux)
CONFFLAGS += \
	-Degl_device=true\
	-Dwayland=true \
	-Dwayland_eglstream=true \
	-Dnative_backend=true
else
CONFFLAGS += \
	-Dudev=false \
	-Dcore_tests=false \
	-Dlibwacom=false \
	-Dwayland=false \
	-Dnative_backend=false \
	-Ddefault_driver=gl
endif

ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH),armel armhf))
CONFFLAGS += \
	-Ddefault_driver=gles2
else
CONFFLAGS += \
	-Ddefault_driver=gl
endif

override_dh_auto_configure:
	dh_auto_configure -- \
		$(CONFFLAGS)

# Use meson test directly as it allows us to use the timeout multiplier
BUILDDIR=$(CURDIR)/obj-$(DEB_HOST_GNU_TYPE)
TEST_COMMAND_BASE=env \
	-u XDG_CACHE_HOME \
	-u XDG_CONFIG_DIRS \
	-u XDG_CONFIG_HOME \
	-u XDG_DATA_DIRS \
	-u XDG_DATA_HOME \
	HOME=$(BUILDDIR)/HOME \
	XDG_RUNTIME_DIR=$(BUILDDIR)/XRD \
	GSETTINGS_SCHEMA_DIR=$(BUILDDIR)/data \
	dbus-run-session -- xvfb-run -s '+iglx -noreset' -a \
	meson test -C $(BUILDDIR) --no-rebuild --verbose --timeout-multiplier 6 \
	           --no-stdsplit --print-errorlogs
TEST_COMMAND=$(TEST_COMMAND_BASE) --no-suite flaky
TEST_COMMAND_FLAKY=$(TEST_COMMAND_BASE) --suite flaky

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  TEST_COMMAND += --num-processes $(NUMJOBS)
endif

# Ignore test failures on hurd, kfreebsd and s390x. Although the test results
# indicate there may be a serious issue, the port maintainers may
# prefer we do the build anyway. I plan to file a bug for this later.
# Don't run the tests on mips since they either time out or fail too much,
# blocking migration to testing
override_dh_auto_test:
ifeq (,$(filter nocheck, $(DEB_BUILD_OPTIONS)))
	glib-compile-schemas $(BUILDDIR)/data
	mkdir -m 700 $(BUILDDIR)/HOME
	mkdir -m 700 $(BUILDDIR)/XRD
ifneq (,$(filter hurd-i386 kfreebsd-i386 kfreebsd-amd64 riscv64 s390x,$(DEB_HOST_ARCH)))
	-$(TEST_COMMAND)
	-$(TEST_COMMAND_FLAKY)
else ifeq (,$(filter mips mips64el mipsel,$(DEB_HOST_ARCH)))
	$(TEST_COMMAND)
	-$(TEST_COMMAND_FLAKY)
endif
endif

override_dh_missing:
	dh_missing --fail-missing

override_dh_girepository:
	dh_girepository /usr/lib/$(DEB_HOST_MULTIARCH)/muffin

override_dh_makeshlibs:
	dh_makeshlibs -V

override_dh_shlibdeps:
	dh_shlibdeps -Llibmuffin-0 -l/usr/lib/$(DEB_HOST_MULTIARCH)/muffin

override_dh_strip:
	dh_strip --dbg-package=muffin-dbg

override_dh_dwz:
