#!/usr/bin/make -f

ifneq ($(DEB_HOST_ARCH),i386)
export UBUNTU_ENABLE_CARGO_AUDITABLE=1
endif

export SUDO_RS_VERSION := $(shell dpkg-parsechangelog -S Version)
UPSTREAM_VERSION := $(shell dpkg-parsechangelog -S Version|cut -d- -f1)

# Vendoring setup
# NOTE: debian/tests/cargo-test needs to be updated in case of
# vendor directory change as it is hardcoded there
export CARGO_VENDOR_DIR = debian/rust-vendor
export CARGO_HOME = $(CURDIR)/debian/cargo_home

CARGO_FEATURES := $(shell sed -n 's/^CARGO_FEATURES=//p' debian/cargo-features)

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

include /usr/share/rustc/architecture.mk
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE

%:
	dh $@ --buildsystem cargo

vendor:
	rm -rf $(CARGO_VENDOR_DIR)
	# Deliberately don't use the wrapper, as it expects the configure step
	# to have occurred already.
	# If you have to modify the path here, don't forget to change the README.source doc
	# as well.
	env -i cargo-vendor-filterer --all-features  --tier 2  --platform '*-*-linux-gnu' --platform '*-*-linux-gnueabi' $(CARGO_VENDOR_DIR)
	# Remove the checksum files to allow us to patch the crates
	@for crate in $(CARGO_VENDOR_DIR)/*; do \
		sed -i 's/^{"files":.*"package":"\([a-z0-9]\+\)"}$$/{"files":{},"package":"\1"}/' "$$crate/.cargo-checksum.json"; \
	done
	# Cleanup temp files
	rm -rf $(CARGO_HOME)
	find $(CARGO_VENDOR_DIR) -type f -name 'Cargo.toml.orig' -delete
	# Update include-binaries
	rm -f Cargo.lock
	rm -f debian/source/include-binaries
	dpkg-source --include-binaries -b .
	# Update XS-Vendored-Sources-Rust field
	VENDORED_SOURCES=$$(/usr/share/cargo/bin/dh-cargo-vendored-sources 2>&1 | grep "^XS-Vendored-Sources-Rust:") \
	  && sed -i "s/^XS-Vendored-Sources-Rust:.*/$${VENDORED_SOURCES}/" debian/control || true
	# Commit changes
	git add -f $(CARGO_VENDOR_DIR)
	git commit -m "Update vendored rust crates" || true
	git add debian/source/
	git commit -m "Update debian/source/include-binaries" || true
	git add debian/control
	git commit debian/control -m "Update XS-Vendored-Sources-Rust field" || true

override_dh_auto_test:
	CARGO_HOME=$(CURDIR)/debian/cargo_home \
	    debian/tests/run-cargo-tests \
		    --skip test_build_run_context \
		    --skip test_traverse_secure_open_positive \
		    --skip test_traverse_secure_open_negative

ifeq ($(DEB_HOST_ARCH),i386)
override_dh_installman:
	echo "Skipping pandoc for i386"
else
override_dh_installman:
	pandoc -s -t man docs/man/su.1.md -o su-rs.1
	pandoc -s -t man docs/man/sudo.8.md  -o sudo-rs.8
	pandoc -s -t man docs/man/sudoers.5.md -o sudoers-rs.5
	pandoc -s -t man docs/man/visudo.8.md -o visudo-rs.8
	dh_installman
endif

override_dh_auto_install:
	# /usr/bin/{su,sudo,visudo} are already used, install into /usr/lib/cargo/bin
	DEB_CARGO_INSTALL_PREFIX=/usr/lib/cargo \
	    dh_auto_install -O--buildsystem=cargo -- \
		--features=$(CARGO_FEATURES)

execute_after_dh_fixperms:
	chmod 4755 debian/sudo-rs/usr/lib/cargo/bin/sudo
	chmod 4755 debian/sudo-rs/usr/lib/cargo/bin/su

override_dh_dwz:
	# Don't do anything. fails because of the
	# https://github.com/rust-lang/rust/issues/66118

override_dh_auto_clean:
	rm -f *.1 *.8
	dh_auto_clean

