#!/usr/bin/make -f

# The following tests are problematic and skipped:
# - Meson's python module always returns the default interpreter, which makes
#   Cython related tests fail on non-default python versions
# - numpy.distutils is still shipped because of Python 3.11, which makes the
#   test_all_modules_are_expected test fail with Python 3.12+ (where it is
#   supposed to be dropped).
export PYBUILD_TEST_CUSTOM = 1
export PYBUILD_TEST_ARGS = {interpreter} {dir}/runtests.py --no-build --verbose --pythonpath {build_dir} \
	-- -k 'not cython and not test_all_modules_are_expected'

# Helper variables
PY3VERS = $(shell py3versions -vr)
PY3DEF = $(shell py3versions -dv)
STAGING_DIR = $(CURDIR)/debian/python3-numpy

%:
	dh $@ --buildsystem=pybuild

execute_after_dh_python3:
	# tweak the entry_points console_scripts list to include all supported versions
	if [ -f $(STAGING_DIR)/usr/lib/python3/dist-packages/numpy-*.egg-info/entry_points.txt ] ; then \
		ENTRYPOINT=$(shell ls $(STAGING_DIR)/usr/lib/python3/dist-packages/numpy-*.egg-info/entry_points.txt) ; \
		for v in $(PY3VERS); do \
			python3 debian/edit_console_scripts.py $$ENTRYPOINT "f2py$$v" "numpy.f2py.f2py2e:main" ; \
		done \
	fi

execute_before_dh_install:
	# add versioned f2pyX.Y scripts
	set -e; for v in $(PY3VERS); do \
		sed "1s,#!.*python[^ ]*\(.*\),#!/usr/bin/python$$v," $(STAGING_DIR)/usr/bin/f2py > $(STAGING_DIR)/usr/bin/f2py$$v; \
	done
	cp -a $(STAGING_DIR)/usr/bin/f2py$(PY3DEF) $(STAGING_DIR)/usr/bin/f2py3
	sed -i "1s,#!.*python[^ ]*\(.*\),#!/usr/bin/python3," $(STAGING_DIR)/usr/bin/f2py3

execute_after_dh_install:
	# create symlinks for .h files
	set -e; for i in $(PY3VERS); do \
	ABITAG=`python$$i -c "import sys; print(sys.abiflags)"`; \
	[ -d $(STAGING_DIR)/usr/include/python$$i$$ABITAG ] || \
		mkdir -p $(STAGING_DIR)/usr/include/python$$i$$ABITAG; \
		dh_link -ppython3-numpy usr/lib/python3/dist-packages/numpy/core/include/numpy usr/include/python$$i$$ABITAG/numpy; \
	done

execute_before_dh_gencontrol:
	python3 debian/versions3.helper >> debian/python3-numpy.substvars

