#!/usr/bin/make -f
#export DH_VERBOSE = 1

export DH_VERBOSE = 1
export PYBUILD_NAME = datalad

# - test_datalad_credential_helper since wants interaction
#   https://github.com/datalad/datalad/issues/6623
export PYTHON_KEYRING_BACKEND=keyrings.alt.file.PlaintextKeyring
# test_test would be removed as 0.17.1
# test_subprocess_return_code_capture - https://github.com/datalad/datalad/issues/6109
export PYBUILD_TEST_ARGS = -s -v -k 'not test_test and not test_subprocess_return_code_capture' -c $(CURDIR)/tox.ini
export DATALAD_TESTS_NONETWORK = 1
export http_proxy=
export https_proxy=

# We need to copy .egg-info so entry points are discovered. And also need
# to generate then per-Python version since some module requirements differ
export PYBUILD_BEFORE_TEST=PYTHONPATH=$(CURDIR)/bin python{version} setup.py develop --install-dir bin/ && cp -rp datalad.egg-info {build_dir}/;


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

clean::
	dh_clean
	-rm -rf bin build .pybuild datalad.egg-info
	-find . -name '*.pyc' -delete

override_dh_auto_configure:
	# cheap fake of an installed datalad, so we get .egg-info with entry points
	$(MAKE) bin
	# and leave the rest for dh_python* runs
	mkdir -p build; \
	    export HOME=$(CURDIR)/build;  \
        git config --global user.name "TESTING"; \
        git config --global user.email "TESTING@example.com"

override_dh_auto_test:
#	# Since pybuild overrides HOME for its own purposes, we will rely on our
#	# setup_package placing it back into HOME
	PYTHON_KEYRING_BACKEND=keyrings.alt.file.PlaintextKeyring PATH=$(CURDIR)/bin:$$PATH \
	    dh_auto_test -- --test-pytest


override_dh_auto_install:
	dh_auto_install
	mv debian/python3-datalad/usr/bin/* debian/datalad/usr/bin
	rm -f debian/python*-datalad/usr/bin/*
	# Building manpages. Overcoming #532 which creates some config file
	mkdir -p build/man
	HOME=$(CURDIR)/build python3 setup.py build_manpage
	# Generating argcomplete helper for bash_completion.d
	{ register-python-argcomplete3 datalad || register-python-argcomplete datalad; } > debian/datalad/usr/share/bash-completion/completions/datalad

override_dh_installchangelogs:
	dh_installchangelogs CHANGELOG.md
