#!/usr/bin/make -f

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

# Debian Policy 9.1.1 reserves /opt for software not shipped by the
# distribution, so the tape media directory is moved under /var/lib.
MHVTL_HOME_PATH = /var/lib/mhvtl

DKMS_SRC = debian/tmp/usr/src/mhvtl-$(DEB_VERSION_UPSTREAM)

# libvtlscsi.so carries no SONAME and has no stable ABI, so it is a private
# library under Policy 10.2 rather than a public one. Setting LIBDIR to the
# multiarch directory makes upstream install it under <libdir>/mhvtl
# (config.mk's MHVTL_LIBDIR) and record that as the binaries' RUNPATH, so no
# rpath needs to be injected here.
MULTIARCH_LIBDIR = /usr/lib/$(DEB_HOST_MULTIARCH)

# usr/Makefile assigns CFLAGS with = and so discards the environment, but it
# interpolates RPM_OPT_FLAGS into both CFLAGS and the shared library link.
# That is the only hook upstream offers for distribution flags, so the
# hardening flags are routed through it. The link rules use $(CFLAGS) rather
# than $(LDFLAGS), hence the linker flags travel with it.
RPM_OPT_FLAGS = \
	$(shell dpkg-buildflags --get CPPFLAGS) \
	$(shell dpkg-buildflags --get CFLAGS) \
	$(shell dpkg-buildflags --get LDFLAGS)

# Unit and generator directories come from systemd.pc: /lib/systemd on
# Debian 12, whose dh_installsystemd only looks there, /usr/lib/systemd on
# later releases. debian/mhvtl.install reads the same values.
SYSTEMD_UNITDIR := $(shell pkg-config --variable=systemdsystemunitdir systemd)
SYSTEMD_GENERATORDIR := $(shell pkg-config --variable=systemdsystemgeneratordir systemd)
ifeq ($(SYSTEMD_UNITDIR),)
$(error systemd.pc not found: install systemd-dev (systemd before 253-2 ships it itself))
endif
export MHVTL_UNITDIR = $(patsubst /%,%,$(SYSTEMD_UNITDIR))
export MHVTL_GENERATORDIR = $(patsubst /%,%,$(SYSTEMD_GENERATORDIR))

# The upstream makefiles read every path from config.mk. Command line
# assignments override its ?= defaults and propagate to the sub-makes.
MHVTL_MAKE_VARS = \
	PREFIX=/usr \
	MANDIR=/share/man \
	LIBDIR=$(MULTIARCH_LIBDIR) \
	MHVTL_HOME_PATH=$(MHVTL_HOME_PATH) \
	MHVTL_CONFIG_PATH=/etc/mhvtl \
	SYSTEMD_SERVICE_DIR=$(SYSTEMD_UNITDIR) \
	SYSTEMD_GENERATOR_DIR=$(SYSTEMD_GENERATORDIR) \
	RPM_OPT_FLAGS='$(strip $(RPM_OPT_FLAGS))'

%:
	dh $@ --with dkms

override_dh_auto_build:
	dh_auto_build -- $(MHVTL_MAKE_VARS)

# Upstream ships no test suite.
override_dh_auto_test:

# The top level install target also installs the kernel module into
# /lib/modules and reloads systemd, neither of which may happen during a
# package build, so the relevant sub-makes are invoked directly.
override_dh_auto_install:
	install -d debian/tmp/usr/bin debian/tmp$(MHVTL_HOME_PATH)
	for dir in usr etc scripts man; do \
		$(MAKE) -C $$dir install DESTDIR=$(CURDIR)/debian/tmp \
			$(MHVTL_MAKE_VARS) || exit 1; \
	done
	# DKMS source tree, flattened the same way as kernel/mhvtl_kernel.tgz:
	# mhvtl.c includes "vtl_common.h", which lives in include/common.
	install -d $(DKMS_SRC)
	cp -a kernel/Makefile kernel/config.sh kernel/*.c kernel/*.h $(DKMS_SRC)/
	cp -a include/common/*.h $(DKMS_SRC)/

# The upstream source directory is itself called usr/ and builds into usr/bin,
# which dh_install would prefer over the staging tree. Pin it to debian/tmp.
override_dh_install:
	dh_install --sourcedir=debian/tmp

override_dh_dkms:
	dh_dkms -p mhvtl-dkms -V $(DEB_VERSION_UPSTREAM) kernel/dkms.conf

# The daemon units are templates instantiated by the generator from
# device.conf, so only the target is enabled. Nothing is started on install:
# that needs a configured library and media created by make_vtl_media.
# The LSB init script is generated and installed by the upstream Makefile
# (etc/init.d/mhvtl). --only-scripts registers it with update-rc.d / invoke-rc.d
# without trying to source a debian/mhvtl.init. It is shipped for non-systemd
# init systems (Devuan et al.); on a systemd host it is an inert no-op (the
# script bails out under /run/systemd/system) and the native generator-driven
# vtllibrary@/vtltape@ units own mhVTL. --no-start matches the units' policy of
# not launching daemons at install time.
override_dh_installinit:
	dh_installinit --only-scripts --no-start

override_dh_installsystemd:
	dh_installsystemd -p mhvtl --no-enable --no-start \
		mhvtl-load-modules.service mhvtl-bay-monitor.service \
		vtltape@.service vtllibrary@.service
	dh_installsystemd -p mhvtl --no-start mhvtl.target
