#!/usr/bin/make -f
# Copyright © 2002,2003 Colin Walters <walters@verbum.org>
# Copyright © 2003 Daniel Stone <daniels@debian.org>
# Copyright © 2006 Sjoerd Simons <sjoerd@debian.org>
# Copyright © 2012 Collabora Ltd.

export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow
export DEB_PYTHON_INSTALL_LAYOUT = deb
export LANGUAGE=C.UTF-8
export LC_ALL=C.UTF-8
unexport LANG
unexport LC_MESSAGES

include /usr/share/dpkg/default.mk

built_binaries := $(shell dh_listpackages)
default_python := $(shell py3versions --default)
python_versions := $(shell py3versions --requested --version debian/control)

%:
	dh $@ $(DH_ADDONS) --buildsystem=pybuild

common_build_args = \
	-Csetup-args=-Dinstalled_tests=true \
	-Csetup-args=-Dinstalled_tests_subdir={interpreter} \
	$(NULL)

ifneq ($(filter %-doc,$(built_binaries)),)
enable_doc = enabled
else
enable_doc = disabled
endif

override_dh_auto_build:
	env \
	PYBUILD_BUILD_ARGS="\
		$(common_build_args) \
		-Csetup-args=-Ddoc=disabled \
	" \
	PYBUILD_BUILD_ARGS_${default_python}="\
		$(common_build_args) \
		-Csetup-args=-Ddoc=$(enable_doc) \
	" \
	dh_auto_build

export PYBUILD_TEST_ARGS = \
	if ! env \
		LC_ALL=C.UTF-8 \
		MESON_TESTTHREADS=1 \
		PYTHON={interpreter} \
		VERBOSE=1 \
	meson test \
		-C {home_dir}/meson-build \
		--verbose \
	; then \
		tail -v -n +0 meson-logs/testlog.txt; \
		exit 1; \
	fi

override_dh_auto_test-arch:
	dh_auto_test -- --test-custom

override_dh_auto_test-indep:
	: # We run tests in each per-architecture build

# Use Meson directly to install things that mesonpy would
# install into a weird location
export PYBUILD_AFTER_INSTALL = \
	meson install \
		-C {home_dir}/meson-build \
		--destdir=$(CURDIR)/debian/tmp \
		--no-rebuild \
		--tags=devel,doc,tests \
		$(NULL)

execute_after_dh_auto_install:
	rm -frv debian/tmp/usr/doc
	rm -frv debian/tmp/usr/include/python3*
	rm -frv debian/tmp/usr/installed-tests
	rm -frv debian/tmp/usr/lib/python3*/dist-packages/.dbus_python.mesonpy.libs

override_dh_installchangelogs:
	dh_installchangelogs -XChangeLog

override_dh_installexamples:
	dh_installexamples --doc-main-package=python-dbus-doc -ppython-dbus-doc
	dh_installexamples --remaining-packages

override_dh_installdocs:
	dh_installdocs --doc-main-package=python-dbus-doc -ppython-dbus-doc
	dh_installdocs --doc-main-package=python3-dbus -ppython3-dbus
	dh_installdocs --remaining-packages

override_dh_sphinxdoc-indep:
	dh_sphinxdoc /usr/share/doc/python-dbus-doc/html

override_dh_auto_clean:
	dh_auto_clean
	rm -Rf build-*
	find . -name '*.py[co]' -print0 | xargs -0 rm -f

override_dh_missing:
	dh_missing --fail-missing

override_dh_python3:
	dh_python3 --shebang=/usr/bin/python3
