#!/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 CARGO_VENDOR_DIR = rust-vendor
export CARGO_HOME = $(CURDIR)/debian/cargo_home

# Build verbosely
export CARGOFLAGS = -v

VENDOR_TARBALL = rust-coreutils_$(UPSTREAM_VERSION).orig-$(CARGO_VENDOR_DIR).tar.xz

%:
	dh $@ --buildsystem cargo

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 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) /usr/share/cargo/bin/cargo prepare-debian $(CARGO_VENDOR_DIR)
	/usr/share/cargo/bin/dh-cargo-vendored-sources
	dh_auto_configure

override_dh_auto_test:
	CARGO_HOME=$(CURDIR)/debian/cargo_home make PROFILE=release MULTICALL=$(MULTICALL) \
				TEST_NO_FAIL_FAST="-- --skip test_env::test_env_arg_ignore_signal_valid_signals \
				--skip test_env::test_simulation_of_terminal_for_stdin_only \
				--skip test_simulation_of_terminal_size_information \
				--skip test_simulation_of_terminal_true \
				--skip test_expr::test_long_input \
				--skip test_factor::test_parallel \
				--skip test_chown::test_chown_only_group_id_nonexistent_group \
				--skip test_chown::test_chown_only_user_id_nonexistent_user \
				--skip test_install::test_install_and_strip \
				--skip test_install::test_install_and_strip_with_program \
				--skip test_install::test_install_root_combined \
				--skip test_od::test_suppress_duplicates \
				--skip test_pinky::test_no_flag \
				--skip test_pinky::test_short_format_i \
				--skip test_pinky::test_short_format_q \
				--skip test_stat::test_fs_format \
				--skip test_stat::test_multi_files \
				--skip test_stat::test_normal_format \
				--skip test_stat::test_printf \
				--skip test_stat::test_symlinks \
				--skip test_stat::test_terse_fs_format \
				--skip test_stdbuf::test_libstdbuf_preload \
				--skip test_stdbuf::test_no_such \
				--skip test_stdbuf::test_permission \
				--skip test_stdbuf::test_stdbuf_line_buffered_stdout \
				--skip test_stdbuf::test_stdbuf_non_utf8_paths \
				--skip test_stdbuf::test_stdbuf_trailing_var_arg \
				--skip test_stdbuf::test_stdbuf_unbuffered_stdout \
				--skip test_who::test_boot \
				--skip util_invalid_name_invalid_command" \
				test

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
#
execute_after_dh_install:
	install -d debian/rust-coreutils/usr/lib/cargo/bin/coreutils
	cat debian/rust-coreutils.hardlinks | while read a b; do \
		env -C debian/rust-coreutils/ ln $$a $$b; \
	done

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
