#!/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

export CFLAGS = $(shell dpkg-buildflags --get CFLAGS)
export CPPFLAGS = $(shell dpkg-buildflags --get CPPFLAGS)
export LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)

%:
	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 -e && for pyvers in $(PYVERS); do \
		python$$pyvers setup.py build; \
		python$$pyvers-dbg setup.py build; \
	done
	set -e && for pyvers in $(PY3VERS); do \
		python$$pyvers setup.py build; \
		python$$pyvers-dbg setup.py build; \
	done

override_dh_auto_install:
	set -e && for pyvers in $(PYVERS); 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
	set -e && for pyvers in $(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
	dh_install

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/*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 -e && 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
	set -e && for py in $(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:
# ifeq (,$(filter nostrip,$(DEB_BUILD_OPTIONS)))
	dh_strip -X"python3" --dbg-package=python-crypto-dbg
	dh_strip -X"python2" --dbg-package=python3-crypto-dbg
#	cd debian/*-dbg/usr/lib/debug/usr/lib && mv pyshared pymodules
# endif
