#!/usr/bin/make -f

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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
export DEB_CXXFLAGS_MAINT_APPEND  = -Wall -pedantic

# trust upstream optimization level unless explicitly disabled
ifeq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
export DEB_CFLAGS_MAINT_STRIP=-O2
export DEB_CXXFLAGS_MAINT_STRIP=-O2
endif

# 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

# generate manual pages from output of clap-based Rust binary
#  arguments: 1:MANFILEPATH 2:COMMANDPATH 3:SUBCOMMAND(S)
_help2man = \
 $(eval suffix = $(lastword $(subst ., ,$(1))))\
 $(eval stem = $(patsubst %.$(suffix),%,$(1)))\
 help2man --section $(suffix) --no-info \
 --version-string='$(DEB_VERSION_UPSTREAM)' \
 --name $(shell $(2) $(3) --help | head --lines=1 | xargs -0 shell-quote --) \
 --output '$(stem)$(if $(3),-$(subst $() ,-,$(strip $(3)))).$(suffix)' \
 '$(2)$(if $(3), $(3))' \
 || { $(2) $(3) --help; false; }

# 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)

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

%:
	dh $@

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

execute_after_dh_auto_install-arch: $(MANPAGES)

# declare version for virtually provided embedded crates
# TODO: drop library version prefix "really" when prefix is superseded
packages = $(patsubst %,librust-%-dev,\
 oxigraph oxrdf oxrocksdb-sys oxsdatatypes sparesults spargebra)
packages_virtual = $(patsubst %,librust-%-dev,\
 oxrdfio oxrdfxml oxttl sparopt)
override_dh_gencontrol:
	$(eval librust-oxigraph-dev_version = $(call crate_version,.))\
	$(eval librust-oxrdf-dev_version = $(call crate_version,lib/oxrdf))\
	$(eval librust-oxrdfio-dev_version = $(call crate_version,lib/oxrdfio))\
	$(eval librust-oxrdfxml-dev_version = $(call crate_version,lib/oxrdfxml))\
	$(eval librust-oxrocksdb-sys-dev_version = $(call crate_version,.))\
	$(eval librust-oxsdatatypes-dev_version = $(call crate_version,lib/oxsdatatypes))\
	$(eval librust-oxttl-dev_version = $(call crate_version,lib/oxttl))\
	$(eval librust-sparesults-dev_version = $(call crate_version,lib/sparesults))\
	$(eval librust-spargebra-dev_version = $(call crate_version,lib/spargebra))\
	$(eval librust-sparopt-dev_version = $(call crate_version,lib/sparopt))\
	set -e; $(foreach p,$(packages),\
	 dh_gencontrol -p$p -- \
	 -v'0.4.0~really$($p_version)' \
	 $(foreach q,$(filter-out $p,$(packages) $(packages_virtual)),\
	 -V'rust:Version:$q=$($q_version)');)
	dh_gencontrol --remaining-packages

# build manpage
debian/oxigraph.1: debian/%.1: debian/oxigraph/usr/bin/%
	mkdir --parents $(dir $@)
	$(call _help2man,$@,$<)
	set -e; $(foreach x,serve serve-read-only serve-secondary backup load dump query update optimize convert,\
	 $(call _help2man,$@,$<,$x);)
