#!/usr/bin/make -f
#
# Palmer Mail -- Debian packaging rules. Plan item 7.4a, Round 2.
#
# THIS IS A TRANSLATION OF packaging/build-deb.sh's RESOLUTE PATH, NOT A SECOND
# DESIGN. That script produces two artefacts; only one of them can ever be an
# archive package, so only one path travels here.
#
# WHY THE NOBLE PATH IS ABSENT AND CANNOT BE ADDED. It runs pip at build time to
# vendor PySide6, icalendar and msal. Debian buildds have no network. That makes
# it IMPOSSIBLE, not merely disfavoured, and nothing in this file should ever be
# extended to try.
#
# WHY dh AND NOT pybuild. Palmer Mail is an application with PRIVATE modules, not
# a library. There is no importable python3-palmer-mail to publish, and Debian's
# Python Policy requires private modules to be absent from sys.path. pybuild
# exists to install into dist-packages, which is exactly what must not happen
# here -- so the application is installed to /usr/lib/palmer-mail and reached by
# the launcher's PYTHONPATH, the same layout the shipped artefacts already use.

export DH_VERBOSE = 1

APPDIR  = debian/palmer-mail/usr/lib/palmer-mail
DESTDIR = debian/palmer-mail

%:
	dh $@ --with python3

# Nothing to configure or compile: the package is pure Python. Declared rather
# than left to dh's guess, so a stray setup.py can never start a build.
override_dh_auto_configure:

override_dh_auto_build:
	# The icon is DRAWN, not committed as a binary blob, so it is reviewable in
	# a diff and reproducible from source -- see packaging/make_placeholder_icon.py.
	# It needs an offscreen Qt, which the script sets for itself.
	mkdir -p debian/icons
	python3 packaging/make_placeholder_icon.py debian/icons

# THE SUITE IS NOT RUN AT BUILD TIME, AND THAT IS A GAP RATHER THAN A CHOICE.
#
# pytest-qt and pytest-randomly are not packaged in Debian, and most of this
# suite needs the first of them. A build-time subset has been MEASURED -- 52
# files, 1,595 checks, 18 s -- but wiring it needs a Build-Depends set and a
# selection mechanism that do not exist yet. Declaring the override empty says
# so out loud; letting dh find no tests would say nothing.
override_dh_auto_test:
	# THE GUI TESTS CANNOT RUN HERE, AND THAT IS NOT A WORKAROUND. They need the
	# qtbot fixture from pytest-qt, which is not packaged in Debian; without it
	# 95 files error and 3 abort for want of a QApplication. The remaining 75
	# files -- 2,199 tests -- pass with the plugin absent, which is exactly the
	# condition on a buildd. They are listed in debian/no-gui-tests.list, and the
	# list was MEASURED per file rather than inferred from imports.
	#
	# Offscreen because a buildd has no display.
	# PYTHONPATH=src because the package is not installed at this point and a
	# buildd has no editable install -- without it conftest.py cannot import
	# palmer_mail and pytest exits 4 before running a single test.
	QT_QPA_PLATFORM=offscreen PYTHONDONTWRITEBYTECODE=1 PYTHONPATH=$(CURDIR)/src \
	    python3 -m pytest -q --no-header -p no:randomly \
	        $$(grep -v '^\#' debian/no-gui-tests.list)

override_dh_auto_install:
	install -d $(APPDIR)
	cp -a src/palmer_mail $(APPDIR)/
	# __pycache__ must not ship: dh_python3 bytecompiles at install time, into
	# the right place, for the interpreter the user actually has.
	find $(APPDIR) -name '__pycache__' -type d -prune -exec rm -rf {} +
	# The installed copy names its own provenance without git, which a buildd
	# does not have. build-deb.sh writes a commit id here; a source package has
	# no commit, so it records the SOURCE PACKAGE VERSION instead -- which is
	# the identifier that is true for an archive build and is resolvable in the
	# archive, where a commit id would not be.
	printf '"""Written by debian/rules. Not tracked in git."""\n\nBUILD_COMMIT = "%s"\n' \
	    "$$(dpkg-parsechangelog -S Version)" > $(APPDIR)/palmer_mail/_build_info.py
	install -D -m 0755 packaging/palmer-mail.launcher.sh $(DESTDIR)/usr/bin/palmer-mail
	install -D -m 0644 packaging/palmer-mail.desktop \
	    $(DESTDIR)/usr/share/applications/palmer-mail.desktop
	install -D -m 0644 debian/icons/palmer-mail-256.png \
	    $(DESTDIR)/usr/share/icons/hicolor/256x256/apps/palmer-mail.png
	install -D -m 0644 debian/icons/palmer-mail-64.png \
	    $(DESTDIR)/usr/share/icons/hicolor/64x64/apps/palmer-mail.png

# The private module directory is not on sys.path, so dh_python3 must be told
# where it is or it will find nothing to bytecompile and say nothing about it.
override_dh_python3:
	dh_python3 /usr/lib/palmer-mail

override_dh_auto_clean:
	rm -rf debian/icons
