#!/usr/bin/make -f
# -*- makefile -*-

# Notes
# -----
#
# - Bindings for all available Ruby and Python versions (as determined
#   in RUBY_VERSIONS, PYTHON_VERSIONS, respectively) are built, using
#   the minimum number of configure/make/make test/install cycles
#   (BUILD_COUNT, BUILDS). Builds are performed out-of-tree, in
#   directories debian/build-1, debian/build-2, etc..
#
# - Targets for individual builds are generated in GNU Make on the
#   fly, by evaluating expansions of the `DH_AUTO_TEMPLATE' variable
#   multiple times. (See the foreach loop below the variable
#   definition.) Multiple recipies for the `override_dh_auto_clean'
#   target as well as multiple prerequisites for the
#   `override_dh_auto_{configure,build,test}' targets are defined.
#   Only the last build contains the full feature set (including Perl
#   and OCaml bindings).
#
# - Running `debian/rules output_template' can be used to inspect the
#   targets that have been generated using `DH_AUTO_TEMPLATE'.

#export DH_VERBOSE=1

include /usr/share/dpkg/pkg-info.mk

# Influcnce Python sysconfig.get_path() as called from configure so
# that the correct install paths are chosen.
export DEB_PYTHON_INSTALL_LAYOUT = deb_system

PYTHON_VERSIONS := $(shell py3versions --supported)
RUBY_VERSIONS   := $(shell dh_ruby --print-supported)

# Determine the number of builds needed due to script language version
# variants
BUILD_COUNT := $(lastword $(sort $(foreach lang,PYTHON RUBY,$(words $($(lang)_VERSIONS)))))
BUILDS := $(foreach n,$(wordlist 1,$(BUILD_COUNT),1 2 3 4 5 6 7 8 9),$(n))

# Template for generating dh_{clean,configure,build,test} overrides.
# $1 is replaced with the build number.
define DH_AUTO_TEMPLATE
override_dh_auto_clean::
	dh_auto_clean --builddir=$(CURDIR)/debian/build-$1

$(CURDIR)/debian/build-$1/config.status:
	dh_auto_configure --builddir=$(CURDIR)/debian/build-$1 \
		-- \
		$(if $(findstring $1,$(BUILD_COUNT)),\
			,\
			--disable-ocaml --disable-perl )\
		$(if $(word $1,$(RUBY_VERSIONS)),\
			RUBY=$(word $1,$(RUBY_VERSIONS)) \
			RAKE="$(word $1,$(RUBY_VERSIONS)) -S rake" \
			,\
			--disable-ruby) \
		$(if $(word $1,$(PYTHON_VERSIONS)),\
			PYTHON=$(word $1,$(PYTHON_VERSIONS)) \
			,\
			--disable-python)

override_dh_auto_configure: $(CURDIR)/debian/build-$1/config.status

$(CURDIR)/debian/build-$1/build-stamp:
	dh_auto_build --builddir=$(CURDIR)/debian/build-$1 \
		-- INSTALLDIRS=vendor LD_RUN_PATH=""
	touch $$@

override_dh_auto_build: $(CURDIR)/debian/build-$1/build-stamp

$(CURDIR)/debian/build-$1/test-stamp:
	dh_auto_test --builddir=$(CURDIR)/debian/build-$1
	touch $$@

override_dh_auto_test: $(CURDIR)/debian/build-$1/test-stamp

endef

$(foreach build,$(BUILDS),$(eval $(call DH_AUTO_TEMPLATE,$(build))))

output_template:
	$(foreach build,$(BUILDS),$(info $(call DH_AUTO_TEMPLATE,$(build))))

override_dh_auto_install:
	for build in $(BUILDS); do \
		dh_auto_install --builddir=$(CURDIR)/debian/build-$$build \
			-- INSTALLDIRS=vendor; \
	done

%:
	dh $@ \
		--buildsystem=autoconf \
		--without python-support \
		--with ocaml,python3,ruby

override_dh_makeshlibs:
	dh_makeshlibs -X/python3/dist-packages/

override_dh_missing:
	dh_missing -X.la -X.so.owner --fail-missing

override_dh_install:
	dh_install -X.la -X.so.owner

# Workaround for
# dh_dwz: dwz -q -- debian/ruby-hivex/usr/lib/x86_64-linux-gnu/ruby/vendor_ruby/2.5.0/_hivex.so returned exit code 1
override_dh_dwz:
	dh_dwz -X/vendor_ruby/
