#!/usr/bin/make -f

include /usr/share/dpkg/default.mk
include /usr/share/python/python.mk
# breaks debclean, need to do this manually
DISABLE_UPDATE_UPLOADERS=1
include /usr/share/gnome-pkg-tools/1/rules/uploaders.mk
-include /usr/share/gnome-pkg-tools/1/rules/gnome-get-source.mk

PYVERS := $(shell pyversions --requested -v debian/control) $(shell py3versions --supported -v)
FLAVOURS := $(PYVERS) $(patsubst %,%-dbg,$(PYVERS))

TESTSUITE_FAIL_CMD=exit 1
ifneq ($(findstring $(DEB_BUILD_ARCH), s390x mipsel kfreebsd-amd64 kfreebsd-i386 hurd-i386),)
# tests currently fail on these architectures
    TESTSUITE_FAIL_CMD=true
endif

configure_flags += \
		--disable-silent-rules \
		$(NULL)

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

%:
	dh $@ --with python2,python3,autoreconf

override_dh_auto_configure:
	set -e && for x in $(FLAVOURS); do \
	    dh_auto_configure --builddirectory=build-$$x -- PYTHON=/usr/bin/python$$x $(configure_flags); \
	done

override_dh_auto_build:
	set -e && for x in $(FLAVOURS); do \
	    dh_auto_build --builddirectory=build-$$x; \
	done

ifneq ($(findstring $(DEB_BUILD_ARCH), kfreebsd-amd64 kfreebsd-i386),)
# don't run the tests under fakeroot, otherwise they will try to connect to
# root's session D-BUS
override_dh_auto_test:
	set -e; export HOME=$(CURDIR)/debian/tmp/home; mkdir -p $$HOME/.local/share; unset LD_PRELOAD; \
	for x in $(FLAVOURS); do \
	        xvfb-run dh_auto_test --builddirectory=build-$$x || $(TESTSUITE_FAIL_CMD); \
	        while [ -e /tmp/.X99-lock ]; do echo "Waiting for xvfb to finish..."; sleep 0.5; done; \
	done;
endif

# we remove stuff that we do not need, and rename the *.so modules to what
# Python expects for that flavor (http://www.python.org/dev/peps/pep-3149/)
# group the install directory by major Python version and dbg/non-dbg
override_dh_auto_install:
	set -e && for x in $(FLAVOURS); do \
	    INST=debian/install-`echo $$x | sed 's/\.[0-9]*//'`; \
	    dh_auto_install --builddirectory=build-$$x --destdir=$$INST; \
	    find $$INST \( -name '*.pyc' -o -name '*.pyo' -o -name '*.la' \) -delete; \
	done

override_dh_auto_clean:
	rm -rf build-*
	dh_auto_clean

override_dh_install:
	dh_install -ppython-gi -ppython-gi-cairo --sourcedir=debian/install-2
	dh_install -ppython-gi-dbg --sourcedir=debian/install-2-dbg
	dh_install -ppython3-gi -ppython3-gi-cairo -ppython-gi-dev --sourcedir=debian/install-3
	dh_install -ppython3-gi-dbg --sourcedir=debian/install-3-dbg
	# for transitional packages; this is not expected to install any files
	dh_install --remaining-packages --sourcedir=/nonexisting
	# cairo.so is installed into a separate package; modelling this with
	# *.install files is a pain, so just remove it manually
	rm debian/python*-gi/usr/lib/python*/*-packages/gi/*cairo*

override_dh_strip:
	dh_strip -ppython-gi --dbg-package=python-gi-dbg
	dh_strip -ppython-gi-cairo --dbg-package=python-gi-dbg
	dh_strip -ppython3-gi --dbg-package=python3-gi-dbg
	dh_strip --remaining-packages

override_dh_makeshlibs:
	dh_makeshlibs -ppython-gi  -V'python-gi (>= 3.4)'
	dh_makeshlibs -ppython3-gi -V'python3-gi (>= 3.4)'
	dh_makeshlibs --remaining-packages

# without this, dh_python2 insists on 2.6 modules
override_dh_python2:
	dh_python2 --no-guessing-versions

override_dh_clean:
	# from uploaders.mk; this uses cdbs-style clean:: which breaks dh clean
	{ cat $(CONTROL_HEADER) ; sed "s/@GNOME_TEAM@/$(UPLOADERS)/" debian/control.in ; } > debian/control
	rm -rf debian/install-*
	find \( -name '*.pyc' -o -name '*.pyo' -o -name '*.la' \) -delete; \
	dh_clean
