#!/usr/bin/make -f
# SPDX-License-Identifier: Apache-2.0
# Copyright 2026 IT Beratung Hermann GmbH

# Builds offline, from a vendored dependency tree, the same reasoning as
# wusel.spec: a real build (sbuild, OBS) runs in a network-less chroot, so
# anything not built by dh itself there cannot exist. build-deb.sh is what
# produces that vendored tree (cargo vendor, same as build-rpm.sh) — this
# file only ever assumes it is already present as ./vendor.

export CARGO_TARGET_DIR = $(CURDIR)/target

%:
	dh $@

override_dh_auto_configure:
	mkdir -p .cargo
	printf '[source.crates-io]\nreplace-with = "vendored-sources"\n\n[source.vendored-sources]\ndirectory = "vendor"\n' \
	    > .cargo/config.toml

override_dh_auto_build:
	cargo build --release --offline --features fuse -p wusel
	$(MAKE) -C integration/nautilus

override_dh_auto_test:
	# Workspace tests run in CI, not per-package build.

override_dh_auto_install:
	install -Dm755 target/release/wusel debian/wusel/usr/bin/wusel
	install -Dm644 packaging/rpm/wusel@.service debian/wusel/usr/lib/systemd/user/wusel@.service

	# Extension + emblems at the paths pkg-config reports for *this* system —
	# same Makefile target the AUR PKGBUILD uses, same reason (see its README).
	$(MAKE) -C integration/nautilus install DESTDIR=$(CURDIR)/debian/wusel

	install -Dm644 integration/icons/at.itbh.Wusel.svg \
	    debian/wusel/usr/share/icons/hicolor/scalable/apps/at.itbh.Wusel.svg

	install -Dm644 integration/gnome-search/at.itbh.Wusel.desktop \
	    debian/wusel/usr/share/applications/at.itbh.Wusel.desktop
	install -Dm644 integration/gnome-search/wusel-search-provider.desktop \
	    debian/wusel/usr/share/gnome-shell/search-providers/wusel-search-provider.desktop
	install -d debian/wusel/usr/share/dbus-1/services
	sed 's#Exec=.*/wusel search-provider#Exec=/usr/bin/wusel search-provider#' \
	    integration/gnome-search/at.itbh.Wusel.SearchProvider.service \
	    > debian/wusel/usr/share/dbus-1/services/at.itbh.Wusel.SearchProvider.service

	# Cloud-provider (Nautilus sidebar) registration for the default account —
	# generated by the freshly built binary, same trick as the RPM spec and
	# the AUR PKGBUILD, for the same reason: never drifts from the code.
	install -d debian/wusel/usr/share/applications
	debian/wusel/usr/bin/wusel desktop install-provider --account default \
	    --dir debian/wusel/usr/share/applications >/dev/null
	rm -f debian/wusel/usr/share/applications/mimeinfo.cache

override_dh_dwz:
	# No split debug info to process — see wusel.spec's debug_package note.

# dh_clean deletes `*.orig` — patch leftovers, normally. `cargo vendor` writes
# one per crate whose manifest it rewrites (289 of them here) and lists each in
# that crate's .cargo-checksum.json, so removing them makes the offline build
# fail on "failed to calculate checksum of vendor/<crate>/Cargo.toml.orig".
#
# It bites in the *source* build, before anything is compiled: building the
# source package runs `debian/rules clean` first, so the tarball was packed
# without them and every rebuild from it was doomed — including the one the
# Open Build Service does.
#
# Same class of bug as the tar-ignore list in debian/source/options, and the
# same lesson: a vendored tree is source, not build output, and the tools that
# tidy build output must be told so one at a time.
override_dh_clean:
	dh_clean -X.orig
