#!/usr/bin/make -f

#export DH_VERBOSE=1
#export PYBUILD_VERBOSE=1

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
else
    NUMJOBS = auto
endif

PY3VERSIONS=$(shell py3versions -vr)
ifneq (,$(filter 3.2 3.3,$(PY3VERSIONS)))
    PICKLE_PROTOCOL=3
else
    PICKLE_PROTOCOL=4
endif

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

override_dh_auto_clean:
	dh_auto_clean
	rm -f debian/README

override_dh_auto_build-arch:
	dh_auto_build -- --build-args="--arch-only"

override_dh_auto_build-indep:
	python3 tools/create_docs.py --skip-install README=debian/README
	dh_auto_build -- --build-args=" \
	                        --indep-only \
	                        --parallel=$(NUMJOBS) \
	                        --pickle-protocol=$(PICKLE_PROTOCOL)"

override_dh_auto_install-arch:
	dh_auto_install -- --install-args=" \
	                        --arch-only \
	                        --install-lib=/usr/lib/pybik"

override_dh_auto_install-indep:
	dh_auto_install -- --install-args=" \
	                        --indep-only \
	                        --install-lib=/usr/lib/pybik \
	                        --install-scripts=/usr/lib/pybik \
	                        --data-dir=/usr/share"

override_dh_auto_test:
	# test whether pickled data can be loaded with all supported Python versions
	PYBUILD_SYSTEM=custom \
	PYBUILD_TEST_ARGS="{interpreter} -c 'import glob,pickle; \
	           [(print(\"test with {version}:\", f), pickle.load(open(f, \"rb\"))) \
	           for f in glob.glob(\"build/models/*\")]'" \
	dh_auto_test

override_dh_python3:
	dh_python3 -p pybik
	dh_python3 -p pybik-bin usr/lib/pybik/pybiklib/

override_dh_builddeb:
	dh_builddeb -- -Zxz

