#!/usr/bin/make -f

# Enable all hardening options
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Keep cargo state inside the build tree.
export HOME = $(CURDIR)/debian/home
export CARGO_HOME = $(HOME)/.cargo
export CARGO_TARGET_DIR = $(CURDIR)/target
export CARGO_NET_OFFLINE = true

RUSTC := $(shell for c in rustc-1.95 rustc-1.94 rustc-1.93 rustc; do command -v $$c >/dev/null 2>&1 && { printf '%s' "$$c"; break; }; done)
CARGO := $(shell for c in cargo-1.95 cargo-1.94 cargo-1.93 cargo; do command -v $$c >/dev/null 2>&1 && { printf '%s' "$$c"; break; }; done)

%:
	dh $@

override_dh_auto_configure:
	mkdir -p $(CARGO_HOME)
	test -d vendor
	test -f .cargo/config.toml
	$(RUSTC) --version
	$(CARGO) --version
	rustc_version=$$($(RUSTC) --version | awk '{print $$2}'); \
	cargo_version=$$($(CARGO) --version | awk '{print $$2}'); \
	dpkg --compare-versions "$$rustc_version" ge 1.93 || { \
		echo "rustc 1.93+ is required for the current dependency set (found $$rustc_version)"; \
		exit 1; \
	}; \
	dpkg --compare-versions "$$cargo_version" ge 1.93 || { \
		echo "cargo 1.93+ is required for the current dependency set (found $$cargo_version)"; \
		exit 1; \
	}

override_dh_auto_build:
	$(CARGO) build --release --frozen

override_dh_auto_install:
	install -D -m 0755 target/release/all-smi debian/all-smi/usr/bin/all-smi

override_dh_auto_test:
	@echo "Skipping tests for PPA build"

override_dh_auto_clean:
	rm -rf $(CARGO_TARGET_DIR) $(HOME)

override_dh_builddeb:
	dh_builddeb --destdir=../
