#!/usr/bin/make -f

PACKAGE=libapache2-mod-wsgi

APXS2=/usr/bin/apxs2
PY3VERS=$(shell py3versions -vs)
PY3DEFAULT=$(shell py3versions -dv)
PY3MIN=$(shell for VERSION in $(PY3VERS); do echo $$VERSION; done | sort --version-sort | head -1)
PY3MAX=$(shell for VERSION in $(PY3VERS); do echo $$VERSION; done | sort --version-sort | tail -1 | LANG=C awk '{print $$NF+0.01}')
pkgdir = $(CURDIR)/debian/$(PACKAGE)-py3

%:
	dh $@ --with autoreconf,apache2

override_dh_auto_configure:
	set -e ; for pyver in $(PY3VERS); do \
		mkdir -p build-$$pyver ;\
		find . -mindepth 1 -maxdepth 1 -name 'debian' -o -name 'build-*' -o -exec cp -r {} build-$$pyver \; ;\
		dh_auto_configure -Bbuild-$$pyver -- --with-apxs=$(APXS2) --with-python=/usr/bin/python$$pyver ;\
	done

override_dh_auto_build:
	set -e ; for pyver in $(PY3VERS); do \
		dh_auto_build -Bbuild-$$pyver ;\
		touch @ ;\
	done

override_dh_auto_install:
	set -e ; for pyver in $(PY3VERS); do \
		dh_auto_install -Bbuild-$$pyver -- DESTDIR=${pkgdir} ;\
		mv ${pkgdir}/usr/lib/apache2/modules/mod_wsgi.so ${pkgdir}/usr/lib/apache2/modules/mod_wsgi.so-$$pyver ;\
	done

override_dh_auto_clean:
	rm -rf build-*

override_dh_install:
	dh_install
	chmod 755 debian/$(PACKAGE)-py3/usr/share/python3/runtime.d/libapache2-mod-wsgi-py3.rtupdate

override_dh_link:
	dh_link -p $(PACKAGE)-py3 /usr/lib/apache2/modules/mod_wsgi.so-$(PY3DEFAULT) usr/lib/apache2/modules/mod_wsgi.so
	dh_link --remaining-packages

override_dh_gencontrol:
	# Generate dependencies manually as there are no public modules
	echo 'python3:Depends=python3 (>= $(PY3MIN)), python3 (<< $(PY3MAX))' \
	     >> $(CURDIR)/debian/$(PACKAGE)-py3.substvars
	dh_gencontrol

override_dh_auto_test:
	# deactivate tests
