#!/usr/bin/make -f

# resolve DEB_VERSION DEB_VERSION_UPSTREAM DEB_VERSION_UPSTREAM_REVISION DEB_DISTRIBUTION
include /usr/share/dpkg/pkg-info.mk

# resolve if release is experimental
DEB_SUITE_EXP = $(filter experimental% UNRELEASED,$(DEB_DISTRIBUTION))

# generate documentation unless nodoc requested
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
MANPAGES = debian/oxigraph.1
endif

# use local fork of dh-cargo and cargo wrapper
PATH := $(CURDIR)/debian/dh-cargo/bin:$(PATH)
PERL5LIB = $(CURDIR)/debian/dh-cargo/lib
export PATH PERL5LIB

# resolve crate version
# * get topmost version except prerelease suffix
#   from Cargo.toml file in dir passed as argument
# * source package version if matching upstream part modulo prerelease
# * otherwise append source package version
crate_version = \
 $(shell perl -n \
 -E '/version\W+\K\d+\.\d+\.\d+/ ' \
 -E 'and say "$(DEB_VERSION_UPSTREAM)" =~ /^\Q$$&\E(?:~[a-z]+\d*)?$$/ ? "$(DEB_VERSION)" : "$$&+$(DEB_VERSION_UPSTREAM_REVISION)" and exit' \
 $1/Cargo.toml)

# tests are seemingly too exhaustive for some architectures
ifneq (,$(filter $(DEB_HOST_ARCH),armel armhf arm64))
TOLERATE_TEST_FAILURE = 1
endif

# * blank_node::tests::test_size_and_alignment panicks on i386
#   <https://github.com/oxigraph/oxigraph/issues/916>
# * test_backward_compatibility require write access to hardcoded path
#   <tests/rocksdb_bc_data>
# * tests test_backup test_secondary expect linking against RocksDB 8.x
#   <https://github.com/oxigraph/oxigraph/issues/914>
TEST_BROKEN = \
 blank_node::tests::test_size_and_alignment \
 test_backup \
 test_backward_compatibility \
 test_secondary

%:
	dh $@ --buildsystem cargo

override_dh_auto_test:
	$(if $(DEB_SUITE_EXP)$(TOLERATE_TEST_FAILURE),\
		dh_auto_test -- -- $(addprefix --skip ,$(TEST_BROKEN)) || true,\
		dh_auto_test -- -- $(addprefix --skip ,$(TEST_BROKEN)))

override_dh_auto_install:
	dh_auto_install -Xoxrocksdb-sys

execute_after_dh_auto_install-arch: $(MANPAGES)

override_dh_dwz:

# declare version for virtually provided embedded crates
libpkg = $(addprefix rust-,\
 oxigraph oxrdf oxrocksdb-sys oxsdatatypes sparesults spargebra)
libpkg_virtual = $(addprefix rust-,\
 oxrdfio oxrdfxml oxttl sparopt)
override_dh_gencontrol:
	$(eval oxigraph_version = $(DEB_VERSION))\
	$(eval rust-oxigraph_version = $(call crate_version,.))\
	$(eval rust-oxrdf_version = $(call crate_version,lib/oxrdf))\
	$(eval rust-oxrdfio_version = $(call crate_version,lib/oxrdfio))\
	$(eval rust-oxrdfxml_version = $(call crate_version,lib/oxrdfxml))\
	$(eval rust-oxrocksdb-sys_version = $(call crate_version,.))\
	$(eval rust-oxsdatatypes_version = $(call crate_version,lib/oxsdatatypes))\
	$(eval rust-oxttl_version = $(call crate_version,lib/oxttl))\
	$(eval rust-sparesults_version = $(call crate_version,lib/sparesults))\
	$(eval rust-spargebra_version = $(call crate_version,lib/spargebra))\
	$(eval rust-sparopt_version = $(call crate_version,lib/sparopt))\
	$(foreach p,oxigraph $(libpkg),\
	 dh_gencontrol -p$(if $(filter rust-%,$p),lib$p-dev,$p) -- \
	 -v'$($p_version)' \
	 $(foreach c,$(libpkg) $(libpkg_virtual),\
	 -V'$c:Version=$($c_version)');)

# build manpage
debian/oxigraph.1: debian/%.1: debian/oxigraph/usr/bin/%
	mkdir --parents $(dir $@)
	help2man --section 1 --no-info \
		--name "Command-line graph database tool to manipulate, SPARQL-query and serve RDF data" \
		--output $@ $< \
		|| { $1 --help; false; }
