#!/usr/bin/make -f

include /usr/share/dpkg/default.mk

PYVERS := $(shell py3versions --supported -v)
FLAVOURS := $(PYVERS)

TESTSUITE_FAIL_CMD=exit 1
NON_FATAL=( echo "*** test-suite FAILED but continuing anyway ***"; true; )
ifneq ($(filter $(DEB_BUILD_ARCH), ),)
# tests currently fail on these architectures
    TESTSUITE_FAIL_CMD=$(NON_FATAL)
endif

export PYBUILD_BEFORE_TEST = {interpreter} {dir}/setup.py build_tests

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

%:
	dh $@ --buildsystem=pybuild

override_dh_gnome_clean:

# don't run the tests under fakeroot, otherwise they will try to connect to
# root's session D-BUS
override_dh_auto_test:
ifeq (,$(filter nocheck, $(DEB_BUILD_OPTIONS)))
ifeq ($(DEB_HOST_ARCH_OS), linux)
	set -e; \
	LC_ALL=C.UTF-8; \
	export HOME=$(CURDIR)/debian/tmp/home; \
	mkdir -p $$HOME/.local/share; \
	unset LD_PRELOAD; \
	xvfb-run \
	dh_auto_test || $(TESTSUITE_FAIL_CMD); \
	while [ -e /tmp/.X99-lock ]; do \
		echo "Waiting for xvfb to finish..."; \
		sleep 0.5; \
	done
	# run the pep8 test but don't fail on it
	python3 setup.py quality || $(NON_FATAL)
endif
endif

override_dh_python3:
	dh_python3 -O--buildsystem=pybuild

override_dh_auto_install:
	dh_auto_install
	mkdir debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)
	mv debian/tmp/usr/lib/pkgconfig debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)

override_dh_install:
	rm -fr debian/tmp/usr/lib/python*/dist-packages/__pycache__
	rm -fr debian/tmp/usr/lib/python*/dist-packages/*/__pycache__
	rm -fr debian/tmp/usr/lib/python*/dist-packages/*/*/__pycache__
	rm -f debian/tmp/usr/lib/python*/dist-packages/*.pyc
	rm -f debian/tmp/usr/lib/python*/dist-packages/*/*.pyc
	rm -f debian/tmp/usr/lib/python*/dist-packages/*/*/*.pyc
	dh_install

override_dh_strip:
	dh_strip --dbgsym-migration='python3-gi-dbg (<< 3.46.0-2~)'

# Do not run dh_makeshlibs, for some reasons the python bindings are build with
# a SONAME set and this causes dh_makeshlibs to create a shlibs file and to
# trigger ldconfig.
override_dh_makeshlibs:

override_dh_installchangelogs:
	dh_installchangelogs -XChangeLog
