#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

PYTHON2 := $(shell pyversions -sv)
PYTHON3 := $(shell py3versions -sv)

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

%:
	dh $@ --with python2,python3 --buildsystem=pybuild

override_dh_auto_clean:
	rm -rf build/ .pybuild/
	rm -f greenlet.egg-info/PKG-INFO
	find . \( -type f -or -type l \) \( -name '*.py[co]' -or -name '*.so' -or -name '*.log' \) -delete
	cd doc && make clean

build-python%:
	python$* setup.py build

override_dh_auto_build: $(PYTHON3:%=build-python%)
	dh_auto_build
	cd doc && PYTHONPATH=.. make html

install-python%:
	python$* setup.py install --root=$(CURDIR)/debian/tmp --install-layout=deb

override_dh_auto_install: $(PYTHON3:%=install-python%)
	dh_auto_install
	mv $(CURDIR)/debian/tmp/usr/include/python3.7/ \
		$(CURDIR)/debian/tmp/usr/include/python3.7m/

override_dh_auto_test: $(PYTHON2:%=test-%-stamp) $(PYTHON3:%=test-%-stamp)
test-%-stamp:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	rm -rf build/test-$*
	mkdir build/test-$*
	python$* setup.py install_lib -d build/test-$*
  ifneq (,$(filter $(DEB_HOST_ARCH), ppc64el))
	-PYTHONPATH=build/test-$* python$* run-tests.py -n
  else
	PYTHONPATH=build/test-$* python$* run-tests.py -n
  endif
endif
	touch test-python-$*-stamp

override_dh_strip:
ifeq (,$(filter nostrip,$(DEB_BUILD_OPTIONS)))
	dh_strip -p python-greenlet --dbg-package=python-greenlet-dbg
	dh_strip -p python3-greenlet --dbg-package=python3-greenlet-dbg
endif

override_dh_compress:
	dh_compress -X.html

.PHONY: override_dh_auto_clean override_dh_auto_build override_dh_auto_test \
	override_dh_strip override_dh_compress
