#!/usr/bin/make -f

# export DH_VERBOSE = 1

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 DEB_BUILD_MAINT_OPTIONS = hardening=+all

export PYBUILD_NAME = jellyfish
export PYBUILD_TEST_PYTEST = 1

export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE

export CARGO=/usr/share/cargo/bin/cargo
export CARGO_HOME=$(CURDIR)/debian/cargo_home
export DEB_CARGO_CRATE=$(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM)


%:
	dh $@ --buildsystem=pybuild

execute_before_dh_auto_configure:
	mv Cargo.lock Cargo.lock.saved
	cp debian/cargo_home/config.toml debian/cargo_home_config.toml.saved
	$(CARGO) prepare-debian debian/cargo_registry --link-from-system
	rm -f Cargo.lock

ifeq (,$(findstring nodoc,$(DEB_BUILD_PROFILES)))
execute_after_dh_auto_build-indep:
	mkdocs build
	# Clean generated HTML files to avoid some privacy breaches
	# We cannot currently remove the Google fonts reference as
	# Roboto Mono is not suitably packaged in Debian.
	perl -i -ne '/<img .*src="http/ || print' site/index.html

override_dh_installdocs-indep:
	dh_installdocs -ppython-jellyfish-doc --doc-main-package=python3-jellyfish
	dh_installdocs --remaining-packages
	mv debian/python-jellyfish-doc/usr/share/doc/python3-jellyfish/site debian/python-jellyfish-doc/usr/share/doc/python3-jellyfish/html
endif

override_dh_installchangelogs:
	dh_installchangelogs docs/changelog.md

ifeq (,$(findstring nocheck,$(DEB_BUILD_PROFILES)))
execute_after_dh_auto_test:
	cargo test
endif

execute_after_dh_auto_clean:
	if test -f Cargo.lock.saved; then mv Cargo.lock.saved Cargo.lock; fi
	if test -f debian/cargo_home_config.toml.saved; then \
	    mv debian/cargo_home_config.toml.saved debian/cargo_home/config.toml; \
	    fi
