#!/usr/bin/make -f

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

override_dh_auto_build:
	python3 -m pip show setuptools
	pip3 --version
	ls -l
	export VERSION="$$(dpkg-parsechangelog -S Version | sed -e 's/-[0-9~a-z].*$$//' -e 's/~alpha/a/g' -e 's/~beta/b/g' -e 's/~rc/rc/g' -e 's/~dev/.dev/g' -e 's/~/\+/g')"; \
	\
	echo "Patching pyproject.toml with static version: $$VERSION"; \
	sed -i '/^dynamic = .*"version".*/d' pyproject.toml; \
	sed -i '/^\[project\]/a version = "'$$VERSION'"' pyproject.toml; \
	sed -i '/"setuptools-git-versioning"/d' pyproject.toml; \
	sed -i 's/enabled = true/enabled = false/' pyproject.toml; \
	cat pyproject.toml; \
	\
	echo "Writing version to rayforge/version.txt: $$VERSION"; \
	echo "$$VERSION" > rayforge/version.txt; \
	\
	dh_auto_build

override_dh_auto_install:
	# STEP 1: Run the DEFAULT dh_auto_install command first.
	export PIP_NO_INDEX=1; \
	export PIP_FIND_LINKS=file://$(CURDIR)/vendor/sdist; \
	dh_auto_install; \
	\
	# STEP 2: Manually install dependencies into the same staging directory.
	echo "--- Manually installing vendored dependencies ---"; \
	HOME=$(CURDIR)/debian/.pip-cache pip3 install \
		--no-build-isolation \
		--no-dependencies \
		--no-index \
		--find-links=file://$(CURDIR)/vendor/sdist \
		--target=$(CURDIR)/debian/rayforge/usr/lib/python3/dist-packages \
		--upgrade \
		vtracer pyvips asyncudp trimesh raygeo

override_dh_auto_test:
	echo "Skipping tests during Debian build; they are run in the dedicated CI test job."

override_dh_clean:
	# Clean up the temporary pip cache
	rm -rf debian/.pip-cache
	# This is still necessary to prevent the vendor directory from being deleted.
	echo "Skipping default dh_clean to preserve vendored sdist."
