#!/usr/bin/make -f

include /usr/share/rustc/architecture.mk

export CARGO_HOME=$(CURDIR)/debian/cargo_home
# Set codegen-units to 1 for reproducible builds (parallel codegen is non-deterministic)
export CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1
# Enable debug logging if 'debug' is in DEB_BUILD_OPTIONS
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
export RUST_LOG=debug
endif
ZSH_COMPDIR = /usr/share/zsh/vendor-completions/

CARGO_BASE_ARGS= --release --target $(DEB_HOST_RUST_TYPE)

%:
	dh $@ --with bash_completion

execute_before_dh_auto_configure:
	if test -f Cargo.lock; then mv Cargo.lock Cargo.lock.upstream; fi

override_dh_auto_build:
	cargo build $(CARGO_BASE_ARGS)

override_dh_auto_test:
	make testsuite

CRATE_VERSION = $(shell cargo metadata --manifest-path lintian-brush/Cargo.toml --no-deps --format-version 1 | python3 -c "import json,sys; pkgs=json.load(sys.stdin)['packages']; print(next(p['version'] for p in pkgs if p['name']=='lintian-brush'))")
CRATE_REGISTRY_DIR = debian/librust-lintian-brush-dev/usr/share/cargo/registry/lintian-brush-$(CRATE_VERSION)
MULTIARCH_HINTS_VERSION = $(shell cargo metadata --manifest-path multiarch-hints/Cargo.toml --no-deps --format-version 1 | python3 -c "import json,sys; pkgs=json.load(sys.stdin)['packages']; print(next(p['version'] for p in pkgs if p['name']=='multiarch-hints'))")
MULTIARCH_HINTS_REGISTRY_DIR = debian/librust-multiarch-hints-dev/usr/share/cargo/registry/multiarch-hints-$(MULTIARCH_HINTS_VERSION)
DEBIAN_WORKSPACE_VERSION = $(shell cargo metadata --manifest-path debian-workspace/Cargo.toml --no-deps --format-version 1 | python3 -c "import json,sys; pkgs=json.load(sys.stdin)['packages']; print(next(p['version'] for p in pkgs if p['name']=='debian-workspace'))")
DEBIAN_WORKSPACE_REGISTRY_DIR = debian/librust-debian-workspace-dev/usr/share/cargo/registry/debian-workspace-$(DEBIAN_WORKSPACE_VERSION)

execute_after_dh_auto_install:
	mkdir -p debian/lintian-brush/$(ZSH_COMPDIR)
	install -m644 debian/lintian-brush.zsh-completion \
	    debian/lintian-brush/$(ZSH_COMPDIR)/_lintian-brush
	mkdir -p $(CRATE_REGISTRY_DIR)
	cp -r lintian-brush/src lintian-brush/build.rs lintian-brush/Cargo.toml \
	    lintian-brush/spdx.json lintian-brush/renamed-tags.json \
	    $(CRATE_REGISTRY_DIR)/
	cp debian/cargo-checksum.json $(CRATE_REGISTRY_DIR)/.cargo-checksum.json
	touch -d@$(SOURCE_DATE_EPOCH) $(CRATE_REGISTRY_DIR)/Cargo.toml
	mkdir -p $(MULTIARCH_HINTS_REGISTRY_DIR)
	cp -r multiarch-hints/src multiarch-hints/Cargo.toml \
	    $(MULTIARCH_HINTS_REGISTRY_DIR)/
	cp debian/cargo-checksum.json $(MULTIARCH_HINTS_REGISTRY_DIR)/.cargo-checksum.json
	touch -d@$(SOURCE_DATE_EPOCH) $(MULTIARCH_HINTS_REGISTRY_DIR)/Cargo.toml
	mkdir -p $(DEBIAN_WORKSPACE_REGISTRY_DIR)
	cp -r debian-workspace/src debian-workspace/Cargo.toml \
	    $(DEBIAN_WORKSPACE_REGISTRY_DIR)/
	cp debian/cargo-checksum.json $(DEBIAN_WORKSPACE_REGISTRY_DIR)/.cargo-checksum.json
	touch -d@$(SOURCE_DATE_EPOCH) $(DEBIAN_WORKSPACE_REGISTRY_DIR)/Cargo.toml

override_dh_auto_clean:
	if test -f Cargo.lock.upstream; then mv Cargo.lock.upstream Cargo.lock; fi
	cargo clean || true

update-deps:
	update-rust-deps --drop-unreferenced --exclude-local-crate
