#!/usr/bin/make -f

# Get the supported Python versions
PYVERS = $(shell pyversions -r -v)
# Get the default Python version
PYVERSION = $(shell pyversions -d -v)

# 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)

%:
	dh $@ --with=python2

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

override_dh_installdocs:
	# Build documentation
	PYTHONPATH=$(call pythonpath,${PYVERSION}) epydoc --config Doc/epydoc-config
	dh_installdocs

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

override_dh_auto_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
		set -e -x; \
		for py in $(PYVERS); 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:
# ifeq (,$(filter nostrip,$(DEB_BUILD_OPTIONS)))
	dh_strip --dbg-package=python-crypto-dbg
#	cd debian/*-dbg/usr/lib/debug/usr/lib && mv pyshared pymodules
# endif
