#!/usr/bin/make -f

# The Python version is the Debian version up to its first `-` (a revision)
# or `~` (the ~deb<R> suite and ~pr<P> preview suffixes the shared
# deb-version.py adds): 0.3.post140~deb12~pr39 -> 0.3.post140. hatchling
# rejects the suffixes, which aren't PEP 440; what is left is the version
# hatch-vcs gives the PyPI wheel of the same commit.
export SETUPTOOLS_SCM_PRETEND_VERSION = $(shell dpkg-parsechangelog -SVersion | sed 's/[-~].*//')
export PYBUILD_TEST_PYTEST = 1

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

# These four daemons cannot run until an admin has written their config
# (/etc/sensors2mqtt/env, snmp.toml), so --no-start keeps a fresh install from
# crash-looping a service that has nothing to talk to.
#
# --no-start ALONE is not enough, though: it also suppresses the restart on
# upgrade, and still emits the preinst `deb-systemd-invoke stop`. An upgrade
# therefore stopped the daemon and never started it again (issue #36).
#
# Pairing it with --restart-after-upgrade selects debhelper's
# postinst-systemd-restartnostart snippet, whose action is `try-restart` -- a
# no-op on a stopped unit, a restart on a running one -- and drops the preinst
# stop. That is exactly the split we want: silent on install, restart on
# upgrade. packaging/check-maintainer-scripts.py asserts this on the built debs.
override_dh_installsystemd:
	dh_installsystemd -p sensors2mqtt-snmp --no-start --restart-after-upgrade
	dh_installsystemd -p sensors2mqtt-snmp-control --no-start --restart-after-upgrade
	dh_installsystemd -p sensors2mqtt-local-control --no-start --restart-after-upgrade
	dh_installsystemd -p sensors2mqtt-ipmi-sensors --no-start --restart-after-upgrade
	dh_installsystemd -p sensors2mqtt-storage --no-start --restart-after-upgrade
	dh_installsystemd -p sensors2mqtt-storage-lvm --no-start --restart-after-upgrade
	dh_installsystemd --remaining-packages
