#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

# The build must not write to $HOME or reach the network: mhvtl-control uses
# only the Go standard library, so the module cache is never needed, and the
# build cache lives in the build tree.
export GOCACHE = $(CURDIR)/debian/.gocache
export GOTOOLCHAIN = local
export GOFLAGS = -mod=readonly
export GOPROXY = off

# The newest versioned Go toolchain, else "go" from PATH. Debian 12's own
# golang-go is 1.19, older than go.mod's 1.22; there the build uses
# golang-1.2x-go from bookworm-backports, which has no /usr/bin/go unless
# the backports golang-go is installed too.
GO := $(or $(shell ls -d /usr/lib/go-1.*/bin/go 2>/dev/null | sort -V | tail -n 1),go)

# Symbols are kept (LDFLAGS empty) so dh_strip can split them into -dbgsym.
# PIE, as Debian's hardening expects; it links no libraries, only needs ld.so.
MAKE_VARS = GO=$(GO) VERSION=$(DEB_VERSION_UPSTREAM) LDFLAGS= \
	GOBUILDFLAGS="-trimpath -buildvcs=false -buildmode=pie"

# Install the unit where this release's systemd looks for it: bookworm's
# dh_installsystemd only finds units in /lib/systemd/system, later releases
# use /usr/lib/systemd/system.
SYSTEMD_UNITDIR = $(shell pkg-config --variable=systemdsystemunitdir systemd)

%:
	dh $@

override_dh_auto_build:
	$(MAKE) build $(MAKE_VARS)

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(GO) test ./...
endif

override_dh_auto_install:
	@test -n "$(SYSTEMD_UNITDIR)" || { echo "pkg-config can't find systemd.pc (systemdsystemunitdir)" >&2; exit 1; }
	$(MAKE) install $(MAKE_VARS) UNITDIR=$(SYSTEMD_UNITDIR) DESTDIR=$(CURDIR)/debian/mhvtl-control

# dh_fixperms resets these to 0644/0755. The options file may hold the API
# token; /var/lib/mhvtl-control-apply belongs to the config-apply helper.
override_dh_fixperms:
	dh_fixperms
	chmod 0750 debian/mhvtl-control/etc/mhvtl-control
	chmod 0600 debian/mhvtl-control/etc/mhvtl-control/mhvtl-control.env
	chmod 0700 debian/mhvtl-control/var/lib/mhvtl-control-apply

# dwz does not understand the DWARF the Go linker writes.
override_dh_dwz:

# Not enabled on install: the operator decides whether and where to expose
# the dashboard, and whether to turn configuration writes on by enabling
# mhvtl-control-apply.socket and mhvtl-control-apply-recover.service.
# deb-systemd-invoke doesn't start a disabled unit, so a fresh install starts
# nothing, and an upgrade restarts a unit only where it is enabled. Restarting
# the recovery oneshot is safe: it acts only on an apply that died with an
# earlier boot, and only while no mhVTL instance runs. The helper's template
# is started by its socket alone, and dh_installsystemd leaves templates out.
override_dh_installsystemd:
	dh_installsystemd --no-enable

override_dh_auto_clean:
	rm -rf bin debian/.gocache

# The Go standard library is linked in statically, so record the toolchain's
# source package as Static-Built-Using, which deb-control(5) defines for
# static builds such as Go's (dpkg >= 1.21.3):
# https://manpages.debian.org/trixie/dpkg-dev/deb-control.5.en.html
override_dh_gencontrol:
	dh_gencontrol -- "-Vgolang:Static-Built-Using=$$(dpkg-query -W -f='$${source:Package} (= $${source:Version})' $$(dpkg-query -S "$$(readlink -f "$$(command -v $(GO))")" | cut -d: -f1))"
