#!/usr/bin/make -f

# Get the supported Python versions
PYVERS = $(shell pyversions -vr)
# Get the default Python version
PYVERSION = $(shell pyversions -vd)
# Get the supported Python3 versions
PY3VERS = $(shell py3versions -vr)

# Callable functions to determine the correct PYTHONPATH
pythonpath = $$(ls -d $(CURDIR)/build/lib.*-$(1))
pythonpath_dbg = $$(ls -d $(CURDIR)/build/lib_d.*-$(1) 2>/dev/null || ls -d $(CURDIR)/build/lib.*$(1)-pydebug)

# build with -O3 (as upstream does) if noopt is not set
ifeq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
export DEB_CFLAGS_MAINT_APPEND=-O3
endif

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

override_dh_auto_clean:
	rm -rf build
	dh_auto_clean

override_dh_clean:
	# Keep LEGAL/copy/LICENSE.orig
	dh_clean -XLEGAL/copy/LICENSE.orig

override_dh_auto_build:
	set -ex && for pyvers in $(PYVERS) $(PY3VERS); do \
		python$$pyvers setup.py build; \
		python$$pyvers-dbg setup.py build; \
	done
	PYTHONPATH=$(call pythonpath,${PYVERSION}) epydoc --config Doc/epydoc-config

override_dh_auto_install:
	set -ex && for pyvers in $(PYVERS) $(PY3VERS); do \
		python$$pyvers setup.py install --install-layout=deb --root $(CURDIR)/debian/tmp; \
		python$$pyvers-dbg setup.py install --install-layout=deb --root $(CURDIR)/debian/tmp; \
	done

override_dh_install:
	# Install everything excluding the *_d.so/*dmu.so debug extensions to python(3)-crypto
	dh_install -X"_d.so" -X"python3" "debian/tmp/*" -p python-crypto
	dh_install -X"dmu.so" -X"python2" "debian/tmp/*" -p python3-crypto
	# Install the debug extensions to python(3)-crypto-dbg
	find debian/tmp/ -name "*_d.so" -exec dh_install '{}' -p python-crypto-dbg \;
	find debian/tmp/ -name "*dmu.so" -exec dh_install '{}' -p python3-crypto-dbg \;
	# Continue with regular dh_install
	dh_install

override_dh_auto_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	set -ex && for py in $(PYVERS) $(PY3VERS); do \
		PYTHONPATH=$(call pythonpath,$$py) python$$py setup.py test ;\
		PYTHONPATH=$(call pythonpath_dbg,$$py) python$$py-dbg setup.py test ;\
	done
endif

override_dh_strip:
	dh_strip -X"python3" --dbg-package=python-crypto-dbg
	dh_strip -X"python2" --dbg-package=python3-crypto-dbg
