#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

PYTHONS        := $(shell pyversions --installed; py3versions --installed)
PYTHON_DEFAULT := $(shell pyversions --default)
RUBIES         := $(shell find /usr/bin/ -name 'ruby[12]\*' | xargs -n1 readlink -f | xargs -n1 basename)
RUBY_DEFAULT   := $(shell which ruby | xargs readlink -f | xargs basename)

%:
	dh $@ \
		--without python-support \
		--with autoreconf,ocaml,python2,python3,ruby

override_dh_auto_configure:
	dh_auto_configure --builddir=$(CURDIR)/debian/build-default \
		-- \
		PYTHON=${PYTHON_DEFAULT} \
		RUBY=${RUBY_DEFAULT}

	set -e; for p in $(filter-out $(PYTHON_DEFAULT), $(PYTHONS)) \
	; do \
		dh_auto_configure --builddir=$(CURDIR)/debian/build-$$p \
		-- \
		--disable-ocaml --disable-perl \
		PYTHON=$$p RUBY=no RAKE=no ; \
	done
	set -e; for v in $(patsubst ruby%,%,$(filter-out $(RUBY_DEFAULT), $(RUBIES))) \
	; do \
		if [ $$v = 1.8 ]; then rake=rake; else rake=rake$$v; fi ; \
		dh_auto_configure --builddir=$(CURDIR)/debian/build-ruby$$v \
		-- \
		--disable-ocaml --disable-perl --disable-python \
		RUBY=ruby$$v RAKE=$$rake ; \
	done

override_dh_auto_build:
	set -e; for p in \
		default \
		$(filter-out $(PYTHON_DEFAULT), $(PYTHONS)) \
		$(filter-out $(RUBY_DEFAULT), $(RUBIES)) \
	; do \
		dh_auto_build --builddir=$(CURDIR)/debian/build-$$p \
			-- INSTALLDIRS=vendor LD_RUN_PATH=""; \
	done

override_dh_auto_test:
	dh_auto_test --builddir=$(CURDIR)/debian/build-default \
	set -e; for p in $(filter-out $(PYTHON_DEFAULT), $(PYTHONS)) ; do \
		dh_auto_test --builddir=$(CURDIR)/debian/build-$$p -- -C python ; \
	done
	set -e; for p in $(filter-out $(RUBY_DEFAULT), $(RUBIES)) ; do \
		dh_auto_test --builddir=$(CURDIR)/debian/build-$$p -- -C ruby ; \
	done

override_dh_auto_install:
	dh_auto_install --builddir=$(CURDIR)/debian/build-default \
			-- INSTALLDIRS=vendor
	set -e; for p in $(filter-out $(PYTHON_DEFAULT), $(PYTHONS)) ; do \
		dh_auto_install --builddir=$(CURDIR)/debian/build-$$p \
			-- -C python ; \
	done
	set -e; for p in $(filter-out $(RUBY_DEFAULT), $(RUBIES)) ; do \
		dh_auto_install --builddir=$(CURDIR)/debian/build-$$p \
			-- -C ruby INSTALLDIRS=vendor ; \
	done

override_dh_auto_clean:
	set -e; for p in \
		default \
		$(filter-out $(PYTHON_DEFAULT), $(PYTHONS)) \
		$(filter-out $(RUBY_DEFAULT), $(RUBIES)) \
	; do \
		dh_auto_clean --builddir=$(CURDIR)/debian/build-$$p ; \
	done

override_dh_strip:
	dh_strip --dbg-package=libhivex0-dbg

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