#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# OBS has no network at build time, so cargo builds from the vendor/ directory
# the orig tarball carries and never from crates.io (debian/README.source).
# CARGO_HOME has to be inside the tree as well: the build user on an OBS worker
# has no writable home.
export CARGO_HOME = $(CURDIR)/debian/cargo_home
export CARGO_NET_OFFLINE = true

# cargo's release profile carries no debug info, which would leave dh_strip
# producing an empty -dbgsym package.
export CARGO_PROFILE_RELEASE_DEBUG = true

# ci-release is upstream's switch for build.rs to write the shell completions
# and the man page into the source tree (completions/, man/) rather than only
# into OUT_DIR, which is where execute_after_dh_install and tensaku.manpages
# pick them up.
FEATURES := --features ci-release

%:
	dh $@

override_dh_auto_build:
	cargo build --release --locked --offline $(FEATURES)

# every_available_encoder_writes_the_selected_format_from_rgb_and_rgba fails for TIFF
# only in a trixie build (measured on build01); PNG, JPEG and WebP pass, and Omarchy's
# capture flow saves PNG. Skipped here rather than holding the package on TIFF.
override_dh_auto_test:
	cargo test --release --locked --offline $(FEATURES) -- --skip every_available_encoder_writes_the_selected_format_from_rgb_and_rgba

# Upstream's Makefile install target writes to /usr/local/bin; the package installs
# through tensaku.install and the rule below instead.
override_dh_auto_install:

# Upstream names these files differently from where Debian puts them, and a
# .install file cannot rename.
execute_after_dh_install:
	install -D -m 0644 completions/tensaku.bash debian/tensaku/usr/share/bash-completion/completions/tensaku
	install -D -m 0644 completions/tensaku.fish debian/tensaku/usr/share/fish/vendor_completions.d/tensaku.fish
	install -D -m 0644 completions/_tensaku debian/tensaku/usr/share/zsh/vendor-completions/_tensaku
	install -D -m 0644 assets/tensaku.svg debian/tensaku/usr/share/icons/hicolor/scalable/apps/dev.tensaku.Tensaku.svg

# dh_clean deletes every *.orig file in the tree, and cargo vendor ships a
# Cargo.toml.orig in each crate that its .cargo-checksum.json lists, so a build
# that runs clean first would refuse every vendored crate.
override_dh_clean:
	dh_clean -XCargo.toml.orig

override_dh_auto_clean:
	cargo clean --offline
	$(RM) -r $(CARGO_HOME) completions man
