#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk
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=/usr/share/cargo/bin/cargo
export CARGO_HOME=$(shell pwd)/debian/tmp/cargo_home
export CARGO_NET_OFFLINE=true
export CARGO_VENDOR_DIR = rust-vendor
export DEB_CARGO_CRATE=$(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM)

export DEB_BUILD_MAINT_OPTIONS=hardening=+bindnow

export PYBUILD_NAME=cryptography
export PYBUILD_TEST_PYTEST=1

VENDOR_TARBALL = $(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM).orig-$(CARGO_VENDOR_DIR).tar.xz
VENDOR_OPENSSL_SYS := $(CARGO_VENDOR_DIR)/openssl-sys

%:
	dh $@ --buildsystem=pybuild

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)
	env -i cargo-vendor-filterer --all-features  --tier 2  --platform '*-*-linux-gnu' --platform '*-*-linux-gnueabi' $(CARGO_VENDOR_DIR)
	# Remove everything vcpkg because it ships arm64 iOS .a files which trip
	# further steps and serve no purpose
	rm -r "$(CARGO_VENDOR_DIR)/vcpkg"
	jq -r '{ "files": {}, "package": .package }' < "$(VENDOR_OPENSSL_SYS)/.cargo-checksum.json" > "$(VENDOR_OPENSSL_SYS)/.cargo-checksum.json.tmp"
	mv "$(VENDOR_OPENSSL_SYS)/.cargo-checksum.json.tmp" "$(VENDOR_OPENSSL_SYS)/.cargo-checksum.json"
	sed -E -i -e '/^\[build-dependencies\.vcpkg\]$$/,+2 d' "$(VENDOR_OPENSSL_SYS)/Cargo.toml"
	sed -E -i -e '/ +try_vcpkg\(\);/ d' -e '/^fn try_vcpkg\(\) \{$$/,/^\}$$/ d' "$(VENDOR_OPENSSL_SYS)/build/find_normal.rs"
	sed -E -i -e '/^extern crate vcpkg;$$/ d' "$(VENDOR_OPENSSL_SYS)/build/main.rs"

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_clean:
	if [ -f Cargo.lock.orig ] ; then mv Cargo.lock.orig Cargo.lock ; fi
	dh_auto_clean
	rm -rf src/rust/target
	rm -rf debian/cargo_registry

override_dh_sphinxdoc:
ifeq (,$(findstring nodoc, $(DEB_BUILD_OPTIONS)))
	PYTHONPATH=./src python3 -m sphinx -b html -N docs/ \
		   $(CURDIR)/debian/python-cryptography-doc/usr/share/doc/python-cryptography-doc/html
	dh_sphinxdoc
endif


override_dh_auto_configure:
	cargo generate-lockfile --offline
	dh_auto_configure
	# This runs cargo prepare-debian and dh-cargo-vendored-sources when
	# CARGO_HOME is set, and other prepare-debian debian/cargo_registry
	# --link-from-system otherwise
	dh_auto_configure --buildsystem=cargo


override_dh_dwz:
	dh_dwz --no-dwz-multifile


override_dh_python3:
	# We patch it out of .dist-info to avoid pkg_resources complaining (#1026537)
	dh_python3 --depends=cffi

.PHONY: $(VENDOR_TARBALL)
