#!/usr/bin/make -f
#
# aaru (v6) — repackage of the upstream prebuilt self-contained .NET single-file
# binary. Nothing is compiled; the "build" step generates the manpage from the
# binary's --help output.

# Debian upstream version (e.g. 6.0.0~alpha.19), from the changelog minus the
# -revision (and any ~series suffix a per-series build appended).
UPSTREAM_VERSION := $(shell dpkg-parsechangelog -SVersion | sed -e 's/-[^-]*$$//')

%:
	dh $@

# No compile. Generate the aaru(1) manpage: aaru-manpage.sh walks the shipped
# binary's --help command tree and splices it into the curated template. It is
# self-healing — if the prebuilt binary cannot start on this builder it emits
# the curated page with a short note instead of failing the build. The upstream
# version is passed as the .TH fallback for that degraded case.
# %prep, in Debian's clothes: the binary tarball is the .orig, and the SOURCE
# tarball (icons, the .desktop entry, the aaruformat MIME xml) comes in through
# the .dsc's Debtransform-Files -- the .dsc's answer to an RPM spec's Source1:.
# Unpacking it here is what lets this lane build straight from upstream instead
# of from a merged tarball we would have to assemble and host ourselves.
#
# Nothing is downloaded here and nothing may be: the build root has no network.
execute_before_dh_auto_build:
	mkdir -p src
	tar -xJf aaru-src-*.tar.xz -C src

# On arm64, swap in the linux_arm64 binary before generating the manpage. The
# .orig tarball (Debtransform-Tar) is the linux_amd64 tarball, so ./aaru is 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 ./aaru with the arm64 binary. The generator then runs the
# arm64 binary natively on the arm64 builder. arm64 ships UNTESTED.
override_dh_auto_build:
ifeq ($(DEB_HOST_ARCH),arm64)
	tar -xJf aaru-*_linux_arm64.tar.xz
endif
	sh debian/aaru-manpage.sh ./aaru debian/aaru.1.in "$(UPSTREAM_VERSION)" > debian/aaru.1

override_dh_auto_test:

# Do not touch the upstream release binary: it is a self-contained single-file
# .NET publish shipped as-is; stripping / dwz would corrupt it and make the
# packaged binary differ from the published release.
override_dh_strip:

override_dh_dwz:

# The ICU runtime package carries its soname in the package name (libicu74 on
# noble, libicu70 on jammy), so resolve it per-series from what libicu-dev
# pulled into the build environment and feed it to control's ${dep:icu}.
override_dh_gencontrol:
	echo "dep:icu=$$(dpkg-query -W -f='$${Package}\n' 'libicu[0-9]*' 2>/dev/null | grep -E '^libicu[0-9]+$$' | sort -V | tail -n1)" >> debian/aaru.substvars
	dh_gencontrol

# Two things dh_install cannot do, both handled once it has laid down the payload:
#
#   1. The udev rule is a packaging artifact (not in the upstream tarballs), so it
#      lives in debian/ and is copied in here.
#
#   2. dh_install can only COPY files that exist; it cannot generate any. Upstream
#      ships icons at 32, 64, 128, 256 and 512 (listed in debian/aaru.install and
#      installed byte-for-byte) but NOT at 16, 22, 24 or 48 — precisely the sizes
#      a desktop draws most: panel, dock, menu and the file-manager list view.
#      Without them the theme downscales the 32, or worse the 512, on the fly into
#      a 16px slot every single time. Render them once here from src/icons/aaru.png
#      — upstream's 862x862 master, the largest and least resampled source in the
#      tarball.
#
#      ImageMagick 7 renamed the CLI to `magick` and deprecated `convert`; the
#      older series carry only `convert`. Pick whichever exists rather than
#      assuming a version.
execute_after_dh_install:
	install -D -m0644 debian/70-aaru-floppy.rules \
	    debian/aaru/usr/lib/udev/rules.d/70-aaru-floppy.rules
	if command -v magick >/dev/null 2>&1; then IM=magick; else IM=convert; fi; \
	for sz in 16 22 24 48; do \
	    install -d debian/aaru/usr/share/icons/hicolor/$${sz}x$${sz}/apps; \
	    $$IM src/icons/aaru.png -filter Lanczos -resize $${sz}x$${sz} \
	        debian/aaru/usr/share/icons/hicolor/$${sz}x$${sz}/apps/aaru.png; \
	done
	for sz in 16 22 24 32 48 64 128 256 512; do \
	    test -s debian/aaru/usr/share/icons/hicolor/$${sz}x$${sz}/apps/aaru.png \
	        || { echo "icon $${sz}x$${sz} missing or empty"; exit 1; }; \
	done
