#!/usr/bin/make -f
#
# This file was copied from the Xapian package. Copyright:
#
# Copyright (C) 2004,2005,2006 Lemur Consulting Ltd
# Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Olly Betts
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

# Codename we're building packages for.  For backported packages, put the
# codename (e.g. wheezy or precise) in debian/codename.  If that file doesn't
# exist, the default is sid.
CODENAME := $(shell cat debian/codename 2>/dev/null||echo sid)

# `--exclude=.EXT' arguments to pass dh_install
DH_INSTALL_EXCLUDE := --exclude=.pyc --exclude=.pyo

BUILD_DEPS := debhelper (>= $(shell cat debian/compat)),

dh_with := --with autotools-dev

DEFAULT_LANGUAGES := python python3
LANGUAGES := $(shell cat debian/bindings-to-package 2>/dev/null || echo '$(DEFAULT_LANGUAGES)')


X_PYTHON_VERSION := X-Dummy-Python: Python bindings not being packaged
# Use filter instead of findstring so we only match against whole words
# (in particular, we don't want to match "python3").
ifneq ($(filter python, $(LANGUAGES)), )
# The python versions to build for:
PYTHON_VERSIONS := $(shell pyversions -vr debian/control)

BUILD_DEPS += dh-python, python, python-all-dev (>= 2.6.6-3~)
X_PYTHON_VERSION := X-Python-Version: >= 2.3
dh_with += --with python2
endif

ifneq ($(filter python3, $(LANGUAGES)), )
# The python3 versions to build for:
PYTHON3_VERSIONS := $(shell py3versions -vr debian/control)
ifeq ($(filter python, $(LANGUAGES)), )
# Need dh-python for python and python3 bindings, but only list it once when
# building both.
BUILD_DEPS += dh-python,
endif
BUILD_DEPS += python3, python3-all-dev
dh_with += --with python3
endif

confflags := $(shell dpkg-buildflags --export=configure)

commonconfflags := \
	$(confflags) \
	--prefix=/usr \
	--sysconfdir=/etc

# With GCC3 and later this won't make a huge difference, but it'll save
# a bit of time and diskspace while building.
commonconfflags += --disable-dependency-tracking

%:
	dh $@ $(dh_with)

override_dh_auto_configure:
	for python in $(PYTHON_VERSIONS) $(PYTHON3_VERSIONS); do \
	    set -e; \
	    mkdir -p debian/build/python$$python; \
	    cd debian/build/python$$python; \
	    ../../../configure $(commonconfflags) \
		PYTHON_VERSION=$$python; \
	    cd ../../..; \
	done

override_dh_auto_build:
	for python in $(PYTHON_VERSIONS) $(PYTHON3_VERSIONS); do \
	    set -e; \
	    $(MAKE) -C debian/build/python$$python ; \
	done

override_dh_auto_install:
	# Install the files under 'debian/tmp'.
	for python in $(PYTHON_VERSIONS) $(PYTHON3_VERSIONS); do \
	    set -e; \
	    $(MAKE) -C debian/build/python$$python DESTDIR=$(CURDIR)/debian/tmp install; \
	done
