#! /usr/bin/make -f
%:
	dh $@ --with python3,sphinxdoc

DEB_BUILD_GNU_TYPE := $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
CROSS :=
else
CROSS := CC=$(DEB_HOST_GNU_TYPE)-gcc
endif

PY3REQUESTED := $(shell py3versions -r)
PY3DEFAULT := $(shell py3versions -d)
# Run setup.py with the default python3 last so that the scripts use
# #!/usr/bin/python3 and not #!/usr/bin/python3.X.
PY3 := $(filter-out $(PY3DEFAULT),$(PY3REQUESTED)) python3

override_dh_auto_build:
	dh_auto_build
	$(MAKE) -C preload $(CROSS)
	set -e; for python in $(PY3); do \
		$$python setup.py build; \
	done
	$(MAKE) -C doc html

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	set -e; for python in $(PY3); do \
		$$python setup.py test; \
	done
endif

override_dh_auto_clean:
	dh_auto_clean
	$(MAKE) -C preload clean
	$(MAKE) -C doc clean
	rm -rf build *.egg-info .tox
	find -name __pycache__ -print0 | xargs -0r rm -rf

override_dh_auto_install:
	# setuptools likes to leave some debris around, which confuses things.
	find build -name __pycache__ -print0 | xargs -0r rm -rf
	find build -name \*.egg-info -print0 | xargs -0r rm -rf
	dh_auto_install
	$(MAKE) -C preload install DESTDIR=$(CURDIR)/debian/tmp
	set -e; for python in $(PY3); do \
		$$python setup.py install --force --root=$(CURDIR)/debian/tmp \
			--no-compile -O0 --install-layout=deb; \
	done

# Sphinx documentation is architecture-independent.
override_dh_sphinxdoc-arch:
