#!/usr/bin/make -f
# debhelper-compat 13, dh sequencer.

# webview_go's CGo header asks for the legacy webkit2gtk-4.0 .pc
# name; Debian/Ubuntu have shipped only -4.1 since 24.04. The shim
# under packaging/pkgconfig/ aliases it. See the corresponding note
# in the Makefile.
export PKG_CONFIG_PATH := $(CURDIR)/packaging/pkgconfig:$(PKG_CONFIG_PATH)

# Don't fight Go's vendoring: install module cache under a build-
# private GOPATH so debhelper isn't surprised by the cache writes.
export GOPATH := $(CURDIR)/.gopath-debbuild
export GOFLAGS := -trimpath
export GO111MODULE := on
export DH_GOLANG_INSTALL_ALL := 1

PKG := pppro-browser-linux
APPID := com.printplatformpro.BrowserAgent
DESTDIR := $(CURDIR)/debian/$(PKG)
VERSION := $(shell dpkg-parsechangelog -SVersion | cut -d- -f1)

%:
	dh $@

override_dh_auto_build:
	# Build the Go agent.
	go build -trimpath -ldflags '-s -w -X main.Version=$(VERSION)' \
	    -o $(PKG) ./cmd/$(PKG)
	# Build the Rust libadwaita UI via meson. The sibling source is
	# staged into ui/ by scripts/build-deb.sh (or by debian-source
	# preparation when building from a tarball). Meson handles
	# blueprint compilation, gresource bundling, schema/desktop/
	# metainfo configuration, and the cargo invocation.
	meson setup ui/_build ui \
	    --prefix=/usr \
	    --buildtype=release \
	    --auto-features=enabled
	meson compile -C ui/_build

override_dh_auto_test:
	go test ./...
	go vet ./...

override_dh_auto_install:
	# --- Agent ---
	install -d $(DESTDIR)/usr/bin
	install -m 0755 $(PKG) $(DESTDIR)/usr/bin/$(PKG)
	install -d $(DESTDIR)/usr/lib/systemd/user
	install -m 0644 packaging/systemd/$(PKG).service \
	    $(DESTDIR)/usr/lib/systemd/user/$(PKG).service
	sed -i 's|@@BINARY@@|/usr/bin/$(PKG)|g' \
	    $(DESTDIR)/usr/lib/systemd/user/$(PKG).service
	install -d $(DESTDIR)/lib/udev/rules.d
	install -m 0644 packaging/udev/99-$(PKG).rules \
	    $(DESTDIR)/lib/udev/rules.d/99-$(PKG).rules
	install -d $(DESTDIR)/usr/share/polkit-1/rules.d
	install -m 0644 packaging/polkit/50-$(PKG).rules \
	    $(DESTDIR)/usr/share/polkit-1/rules.d/50-$(PKG).rules
	# --- Rust libadwaita UI (binary + .desktop + metainfo + gschema
	# + icons + .mo translations all installed by meson) ---
	DESTDIR=$(DESTDIR) meson install -C ui/_build --no-rebuild
	# Validate the meson-installed user-visible files.
	desktop-file-validate $(DESTDIR)/usr/share/applications/$(APPID).desktop
	appstreamcli validate --no-net --pedantic \
	    $(DESTDIR)/usr/share/metainfo/$(APPID).metainfo.xml || true

override_dh_shlibdeps:
	# CI runners install the -dev variants of GTK4 + libadwaita via
	# apt-cache; dpkg-shlibdeps then can't resolve libadwaita-1.so.0
	# (etc) to a binary package because the runtime libadwaita-1-0
	# isn't named in the cache list. The runtime Depends: are
	# declared explicitly in debian/control already (libadwaita-1-0,
	# libgtk-4-1, libwebkit2gtk-4.1-0, etc), so we tell shlibdeps to
	# skip the missing-info check rather than installing duplicates
	# of every -dev / runtime pair into the build env.
	dh_shlibdeps -- --ignore-missing-info

override_dh_auto_clean:
	# Go's module cache marks downloaded files read-only to detect
	# corruption; plain `rm -rf` fails on them. Make them writable
	# first. `|| true` keeps clean idempotent on a fresh tree where
	# .gopath-debbuild may not exist.
	chmod -R u+w .gopath-debbuild 2>/dev/null || true
	rm -rf .gopath-debbuild
	rm -rf ui/_build ui/target
	rm -f $(PKG)
	dh_auto_clean
