#!/usr/bin/make -f
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk
include /usr/share/rustc/architecture.mk
export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE
export CARGO_PROFILE_RELEASE_DEBUG=true
export LIBSTDBUF_DIR = /usr/libexec/rust-coreutils
VERSION := $(shell dpkg-parsechangelog -S Version|sed -e "s|.*~\(.*\)-.*|\1|g")
UPSTREAM_VERSION := $(shell dpkg-parsechangelog -S Version|cut -d- -f1)

# Build a multicall binary instead of multiple binaries
# to reduce the storage footprint
export MULTICALL = y

# Vendoring setup
# FIXME: The upstream Makefile we use doesn't work with the cargo wrapper,
# as it assumes no cross-compilation and hence files appearing in target/release
# rather than the architecture-specific target directory.
#export CARGO = /usr/share/cargo/bin/cargo
export PATH := /usr/lib/rust-1.85/bin:$(PATH)
export CARGO_VENDOR_DIR = rust-vendor
export CARGO_HOME = $(CURDIR)/debian/cargo_home
VENDOR_TARBALL = rust-coreutils_$(UPSTREAM_VERSION).orig-$(CARGO_VENDOR_DIR).tar.xz

%:
	dh $@ --buildsystem makefile

vendor-tarball-quick-check:
	if [ -e ../$(VENDOR_TARBALL) ]; then echo "../$(VENDOR_TARBALL) already exists, bailing!"; exit 1; fi

vendor-deps-cargo:
	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 PATH=$(PATH) cargo-vendor-filterer --all-features  --tier 2  --platform '*-*-linux-gnu' --platform '*-*-linux-gnueabi' $(CARGO_VENDOR_DIR)

vendor-deps: vendor-deps-cargo
	rm -r \
		$(CARGO_VENDOR_DIR)/*/tests
	# Remove the checksum files to allow us to patch the crates to remove extraneous dependencies
	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)

vendor-tarball: vendor-tarball-quick-check vendor-deps
	tar --sort=name --mtime=@0 --owner=0 --group=0 --numeric-owner --pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime -caf ../$(VENDOR_TARBALL) $(CARGO_VENDOR_DIR)

override_dh_auto_configure:
	CARGO_HOME=$(CURDIR)/debian/cargo_home DEB_CARGO_CRATE=coreutils_$(VERSION) debian/cargo prepare-debian $(CARGO_VENDOR_DIR)
	/usr/share/cargo/bin/dh-cargo-vendored-sources

override_dh_auto_test:
	# for now, don't fail the build if tests are failing
	CARGO_HOME=$(CURDIR)/debian/cargo_home make PROFILE=release MULTICALL=$(MULTICALL) test||true

override_dh_auto_clean:
	debian/cargo clean
	rm -rf .cargo-checksum.json debian/cargo_registry

override_dh_auto_install:
#use thin rather than full lto on 32-bit to avoid running out of address space.
ifeq ($(DEB_HOST_ARCH_BITS),32)
	sed -i 's/lto = true/lto = "thin"/' Cargo.toml
endif
	CARGO_HOME=$(CURDIR)/debian/cargo_home DESTDIR=$(CURDIR)/debian/tmp/ make SELINUX_ENABLED=1 PROFILE=release MULTICALL=$(MULTICALL) install
ifeq ($(DEB_HOST_ARCH_BITS),32)
	sed -i 's/lto = "thin"/lto = true/' Cargo.toml
endif
# Create the symlink early to be able to generate the manpages
	cd debian/tmp/usr/share/zsh/site-functions && mkdir rust-coreutils && chmod -x _* && mv _* rust-coreutils
	cd debian/tmp/usr/share/bash-completion/completions/ && chmod -x rust-*
	cd debian/tmp/usr/share/fish/vendor_completions.d/ && chmod -x *.fish
	cd debian/tmp/usr/bin && mv rust-coreutils coreutils && rm rust-*
#	cd debian/tmp/usr/share/fish/vendor_completions.d && for f in *fish; do mv $$f rust-$$f; done
	dh_link

override_dh_missing:
	dh_missing --fail-missing

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

#use lower debuginfo level on 32-bit to avoid running out of address space.
ifeq ($(DEB_HOST_ARCH_BITS),32)
execute_after_dh_auto_configure:
	if test -f debian/cargo_home/config; then \
		sed -i s/debuginfo=2/debuginfo=1/ debian/cargo_home/config; \
	fi
endif

override_dh_clean:
	rm -f Cargo.toml.orig Cargo.lock
	dh_clean
