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

include /usr/share/dpkg/pkg-info.mk

export PYBUILD_NAME=debugpy

%:
	dh $@ --buildsystem=pybuild

# Ensure that we have updated the _version.py patch to the current
# upstream PyPI version.

UPSTREAM_VERSION = $(subst +ds,,$(DEB_VERSION_UPSTREAM))

execute_after_dh_testdir:
	grep -q $(UPSTREAM_VERSION) src/debugpy/_version.py

# We link to pydevd as debugpy assumes that it is a vendored module;
# it would not be that straightforward to modify debugpy to use the system
# version of pydevd.
# Unfortunately pydevd is horrible: it installs multiple different modules.
# Rather than hardcode this, we calculate the list of modules dynamically.
PYDEVD_TOP_LEVEL = $(shell dpkg -L python3-pydevd | grep dist-packages/ | \
	cut -d/ -f6 | sort -u | grep -v egg-info)

PYTHON_DIR = /usr/lib/python3/dist-packages
PYDEVD_VENDORED_DIR = src/debugpy/_vendored/pydevd

src/debugpy/_vendored/pydevd/pydevd.py:
	# copy pydevd into source tree
	mkdir -p $(PYDEVD_VENDORED_DIR) && \
	    cd $(PYDEVD_VENDORED_DIR) && \
	    for pydevd_top in $(PYDEVD_TOP_LEVEL); do \
	        cp -a $(PYTHON_DIR)/$$pydevd_top . ; \
	    done

execute_before_dh_auto_configure: src/debugpy/_vendored/pydevd/pydevd.py

execute_after_dh_auto_clean:
	rm -rf $(PYDEVD_VENDORED_DIR)

override_dh_python3:
	dh_python3 --no-ext-rename
	echo "debugpy:Built-Using=pydevd (= $$(dpkg-query -f='$${Version}' -W python3-pydevd))" >> debian/python3-debugpy.substvars

ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	# * The http_proxy setting is to disable the proxies that pybuild
	# introduces; the tests might set up and use a local http server.
	# (See https://bugs.debian.org/1022188)
	export http_proxy=""; \
	export PYBUILD_SYSTEM=custom; \
	PYBUILD_TEST_ARGS="\
		cd {build_dir}; \
		$(CURDIR)/debian/run_tests $(CURDIR)/debian/get_test_exclusions {interpreter} -m pytest" dh_auto_test
endif

execute_after_dh_installdocs:
	# dh_installdocs cannot handle a filename with spaces (bug #1016596)
	cp -p doc/Subprocess\ debugging.md debian/python3-debugpy/usr/share/doc/python3-debugpy