#!/usr/bin/make -f

# See debhelper(7) (uncomment to enable).
# Output every command that modifies files on the build system.
#export DH_VERBOSE = 1
.PHONY: setup-tox-runner

setup-tox-runner:
	python3 -m venv .tox-runner
	.tox-runner/bin/python -m pip install 'tox==4.65.0'

override_dh_auto_test:
# ciuser is used because tox and PostgreSQL cannot be run as root.
# localtmp is recreated from scratch so the step is idempotent on reused
# CI workspaces (a leftover localtmp used to make `mkdir` fail the build).
ifeq ($(CI),true)
	echo "This is running in automated ci infrastructure"
	adduser --disabled-password --gecos "" ciuser
	rm -rf $(CURDIR)/localtmp
	mkdir -p $(CURDIR)/localtmp
	chown -R ciuser:ciuser $(CURDIR)
	su ciuser -c "env TMPDIR=$(CURDIR)/localtmp tox"
else
	# Create an isolated tox environment for the test suite.
	$(CURDIR)/.tox-runner/bin/tox
endif

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

override_dh_auto_build: setup-tox-runner
	dh_auto_build
	$(CURDIR)/.tox-runner/bin/tox -e docs

override_dh_auto_clean: setup-tox-runner
	rm -rf $(CURDIR)/.tox-runner
	dh_auto_clean
