#!/usr/bin/make -f

include /usr/share/dpkg/default.mk

# Call Debian's real binaries so rust-toolchain.toml never invokes rustup.
CARGO ?= /usr/bin/cargo
export RUSTC ?= /usr/bin/rustc
export CARGO_HOME = $(CURDIR)/debian/cargo-home
export CARGO_TARGET_DIR = $(CURDIR)/target
export CARGO_NET_OFFLINE = true
export CARGO_HUSKY_DONT_INSTALL_HOOKS = true
# Slint's generated Rust and release LTO use substantial memory.
export CARGO_BUILD_JOBS ?= 2
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

%:
	dh $@ --buildsystem=makefile

# Cargo checksums cover the bundled native sources too.
override_dh_update_autotools_config:

override_dh_autoreconf:

override_dh_auto_configure:
	mkdir -p "$(CARGO_HOME)"
	$(CARGO) metadata --frozen --format-version=1 > debian/cargo-metadata.json

override_dh_auto_build:
	$(CARGO) build --frozen --release --jobs $(CARGO_BUILD_JOBS) \
		-p asusctl -p asusd -p asusd-user -p asus-shutdown -p rog-control-center
	python3 debian/collect-vendor-licenses.py

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(CARGO) test --frozen --release --jobs $(CARGO_BUILD_JOBS) \
		-p rog_profiles --lib
endif

override_dh_auto_install:
	mkdir -p debian/tmp
	$(MAKE) -o target/release/asusctl -o target/release/asusd \
		-o target/release/asusd-user -o target/release/asus-shutdown \
		-o target/release/rog-control-center \
		install DESTDIR=$(CURDIR)/debian/tmp prefix=/usr libdir=/usr/lib
	desktop-file-validate debian/tmp/usr/share/applications/org.opengamingcollective.rog-control-center.desktop

override_dh_installsystemd:
	dh_installsystemd -pasusctl --no-enable --no-start --restart-after-upgrade asusd.service asus-shutdown.service

override_dh_missing:
	dh_missing --fail-missing

override_dh_auto_clean:
	rm -rf target debian/cargo-home debian/cargo-metadata.json debian/generated

# Cargo.toml.orig and other backup-looking files are checksummed crate sources.
# Keep the complete vendor tree immutable through dpkg-buildpackage's clean.
override_dh_clean:
	dh_clean --exclude=vendor/
