#!/usr/bin/make -f
#
# aaru5 — repackage of the upstream prebuilt linux_amd64 NativeAOT binary.
# Nothing is compiled here; the "build" step GENERATES the manpage.

# Debian upstream version (e.g. 5.4.2), derived from the changelog by dropping
# the -revision (and any ~series suffix a per-series build appended). Used only
# as the generator's fallback, for the case where the binary will not run in the
# build root and cannot report its own version.
UPSTREAM_VERSION := $(shell dpkg-parsechangelog -SVersion | sed -e 's/-[^-]*$$//')

%:
	dh $@

# The manpage's COMMAND REFERENCE is generated by walking the shipped binary's
# own --help tree (33 sub-commands), so it is the binary's text and cannot drift
# from the version installed. This replaces a hand-written command list that
# would go stale silently. The .TH version stamp is honest here for the same
# reason: content and version are produced together. (Hand-written pages get a
# FIXED marker instead — see ubuntu/redumper.)
#
# The generator seeds a throwaway HOME with an Aaru.xml carrying a saturated
# GdprCompliance level: on a fresh HOME the binary otherwise opens its
# interactive GDPR consent dialog and, with no TTY on the builder, aborts with
# SIGABRT. It also probes whether the binary runs at all and falls back to the
# curated page with a note in place of the reference rather than failing.
#
# SOURCE_DATE_EPOCH (set by dpkg-buildpackage from the changelog) is honoured by
# the generator for the .TH date, so rebuilds stay byte-identical.
# On arm64, swap in the linux_arm64 build before generating the manpage. The
# .orig tarball (Debtransform-Tar) is the linux_amd64 tarball, so ./aaru and
# ./libe_sqlite3.so are the x86-64 build; the arm64 tarball travels as a
# Debtransform-Files extra (obs-build covers it with --include-binaries) and is
# extracted over the tree here, replacing BOTH the binary and its arch-specific
# sidecar. The generator then runs the arm64 binary natively on the arm64
# builder (the hard guard below still fails loudly on a silent fallback).
# arm64 ships UNTESTED.
override_dh_auto_build:
ifeq ($(DEB_HOST_ARCH),arm64)
	tar -xJf aaru-*_linux_arm64.tar.xz
endif
	sh debian/aaru5-manpage.sh ./aaru debian/aaru5.1.in $(UPSTREAM_VERSION) \
	    > debian/aaru5.1
# The generator has a rescue path (curated page, no command reference) for build
# roots where the binary will not start. Make sure it can never ship unnoticed:
# on OBS it fired SILENTLY and the build stayed green with a manpage that had no
# reference at all. Nobody inspects a green build.
	grep -q '^\.SS aaru5 device' debian/aaru5.1 || { \
	    echo "aaru5.1 has no generated command reference -- the generator fell back."; \
	    echo "The build root could not run the binary; see its WARNINGs above."; \
	    exit 1; }

override_dh_auto_test:

# Do not touch the upstream release binary / its libe_sqlite3.so sidecar: they
# are shipped as-is by upstream; stripping / dwz would only risk corrupting the
# NativeAOT ELF and would make the packaged files differ from the release.
override_dh_strip:

override_dh_dwz:

# The udev rule is a packaging artifact (not in the upstream tarball), so it
# lives in debian/ and is installed after dh_install has laid down the payload.
execute_after_dh_install:
	install -D -m0644 debian/70-aaru5-floppy.rules \
	    debian/aaru5/usr/lib/udev/rules.d/70-aaru5-floppy.rules
