#!/usr/bin/make -f

include /usr/share/dpkg/default.mk
include /usr/share/rustc/architecture.mk

export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE
export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS

export PATH := /usr/share/cargo/bin:$(PATH)
export CARGO=/usr/share/cargo/bin/cargo
export CARGO_HOME=$(CURDIR)/debian/cargo_home
export CARGO_REGISTRY=$(CURDIR)/debian/cargo_registry
export DEB_CARGO_CRATE=$(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

export PYBUILD_NAME=rookiepy
export PYBUILD_DIR=bindings/python/

%:
	dh $@ --buildsystem=none

override_dh_auto_configure:
	dh_auto_configure --buildsystem=rust
	dh_auto_configure --buildsystem=pybuild
	
	# This has to be after dh_auto_configure --builsystem=rust, because
	# dh-rust creates a wrong crago config without enabling debug info
	$(CARGO) prepare-debian debian/cargo_registry --link-from-system

execute_before_dh_auto_build:
	# A change of the build path by a Salsa CI pipeline update broke the
	# blhc ignore regex (#1037521)
	@echo "blhc: ignore-line-regexp: \\s*Compiling (.+)"

override_dh_auto_build:
	dh_auto_build --buildsystem=rust
	dh_auto_build --buildsystem=pybuild
	pod2man -c "User commands" --release "rookie $(DEB_VERSION_UPSTREAM)" --section 1 debian/rookie.pod > debian/rookie.1

override_dh_auto_install:
	dh_auto_install --buildsystem=rust
	dh_auto_install --buildsystem=pybuild
	/usr/share/dh-rust/bin/dh-rust-built-using target/$(DEB_HOST_RUST_TYPE)/release python3-rookiepy

execute_after_dh_installdocs:
	mv debian/python3-rookiepy/usr/share/doc/python3-rookiepy/Python.md debian/python3-rookiepy/usr/share/doc/python3-rookiepy/README.md
	mv debian/librust-rookie-dev/usr/share/doc/librust-rookie-dev/Rust.md debian/librust-rookie-dev/usr/share/doc/librust-rookie-dev/README.md

override_dh_auto_test:
	dh_auto_test --buildsystem=rust
	dh_auto_test --buildsystem=pybuild

override_dh_auto_clean:
	dh_auto_clean --buildsystem=rust
	dh_auto_clean --buildsystem=pybuild
