#!/usr/bin/make -f
#
# Copyright (C) 2004,2005,2006 Lemur Consulting Ltd
# Copyright (C) 2006,2007,2008,2009,2010 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. lenny or jaunty) in debian/codename.  If that file doesn't
# exist, the default is sid.
CODENAME := $(shell cat debian/codename 2>/dev/null||echo sid)

# Version without the "-N" suffix.
XAPIAN_VERSION := $(shell sed 's/.*(\([^-]*\)\(-.*\)*).*/\1/;q' debian/changelog)

# The next incompatible version - i.e. the first release of the next release
# branch.  So for XAPIAN_VERSION "1.0.<n>", this should be "1.1.0".
XAPIAN_VERSION_CEILING := $(shell echo '$(XAPIAN_VERSION)'|awk -F. '{print $$1"."$$2+1".0"}')

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

# Debian "etch" (and later), and Ubuntu "edgy" (and later) have pyversions.
USE_PYVERSIONS := no
ifeq ($(findstring .$(CODENAME)., .sarge.dapper.), )
USE_PYVERSIONS := yes
endif

ifeq ($(USE_PYVERSIONS), yes)

# The python versions to build for:
PYTHON_VERSIONS := $(shell pyversions -r debian/control)
ifeq ($(PYTHON_VERSIONS), )
PYTHON_VERSIONS := python2.3 python2.4
endif
# python >= 2.3.5-7 is required for the pyversions command.
BUILD_DEPS := debhelper (>= 5.0.37.1), python (>= 2.3.5-7), python-central (>= 0.4.10), python-all-dev,

else

# When we finally drop support for both sarge and dapper, we can remove this
# branch as all newer distros have pyversions.  On dapper, python2.2 is in
# universe and 2.3 and 2.4 are available, so we could drop 2.2 support if
# necessary.

# The python versions to build for (Debian sarge also has Python 2.1, but
# xapian-bindings now requires at least Python 2.2):
PYTHON_VERSIONS := python2.2 python2.3 python2.4

# We need python itself for dh_python to work.
BUILD_DEPS := debhelper (>= 5), python, $(PYTHON_VERSIONS:%=%-dev,)

endif

# The PHP versions to build for:
ifeq ($(CODENAME), sarge)
# Debian sarge doesn't have PHP5.
PHP_VERSIONS := 4
DH_INSTALL_EXCLUDE += --exclude=.php5
else
ifneq ($(findstring .$(CODENAME)., .etch.dapper.edgy.), )
PHP_VERSIONS := 4 5
else
# PHP4 support was dropped in Debian sid (and lenny) and Ubuntu feisty.
PHP_VERSIONS := 5
DH_INSTALL_EXCLUDE += --exclude=.php4
endif
endif

# We'll need this if we ever generate different .py modules for different
# supported python versions.
#export DH_PYCENTRAL=nomove

export DH_OPTIONS

export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
    confflags += --build $(DEB_HOST_GNU_TYPE)
else
    confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
    # Disable the testsuite when cross-compiling.
    DEB_BUILD_OPTIONS += nocheck
endif

# Handle DEB_BUILD_OPTIONS.  Note that dh_strip handles nostrip for us.
ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
    # -g needed for armel to workaround unaligned relocations (#576944)
    confflags += CFLAGS="-O0 -g" CXXFLAGS="-O0 -g"
endif
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    MAKEFLAGS += -j$(NUMJOBS)
endif
ifneq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
    MAKE_CHECK := :
else
    MAKE_CHECK := make check
endif

# Build depends for PHP.  We need phpN-cli so we can run "make check" on
# the bindings and to get the phpapi version we need to depend on.
#
# Note that we check for /usr/bin/php-configN before trying to build PHP
# bindings below.
#
# Only the first % in the replacement part is substituted, so we need to do
# this in two goes.
BUILD_DEPS += $(PHP_VERSIONS:%=php%-dev,)
BUILD_DEPS += $(PHP_VERSIONS:%=php%-cli,)

# Build depends for Tcl.  We use Tcl's stubs mechanism, so the built
# bindings will work with any Tcl version from 8.0.6 upwards, whichever
# version they were built against.
#
ifeq ($(findstring .$(CODENAME)., .sarge.etch.dapper.edgy.feisty.gutsy.), )
# lenny/hardy onwards have a "tcl-dev" dummy package which pulls in the package
# for the current preferred version.
BUILD_DEPS += tcl-dev,# NB Trailing ',' required here!
TCLSH=/usr/bin/tclsh
else
# All the older distributions which we want to build packages for have
# tcl8.4-dev, so just build against that.
BUILD_DEPS += tcl8.4-dev,# NB Trailing ',' required here!
TCLSH=/usr/bin/tclsh8.4
endif

# Build depends for Ruby.
BUILD_DEPS += ruby1.8-dev, ruby1.8,# NB Trailing ',' required here!
RUBY=/usr/bin/ruby1.8
RUBY_LIB=$(shell $(RUBY) -rrbconfig -e 'puts Config::CONFIG["rubylibdir"]')
RUBY_LIB_ARCH=$(shell $(RUBY) -rrbconfig -e 'puts Config::CONFIG["archdir"]')

# We need a versioned build dependency on libxapian-dev.
BUILD_DEPS += libxapian-dev (>= $(XAPIAN_VERSION)),# NB Trailing ',' required here!
BUILD_DEPS += libxapian-dev (<< $(XAPIAN_VERSION_CEILING)),# NB Trailing ',' required here!

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

maint: debian/control

maintclean: clean
	rm -f debian/control

debian/control: debian/rules debian/control.in
	rm -f debian/control.tmp
	sed -e "s/@BUILD_DEPS@/$(BUILD_DEPS)/g" \
	    < debian/control.in > debian/control.tmp
	for v in $(PHP_VERSIONS); do \
	    sed "s/@PHP_VERSION@/$$v/g" debian/control-php.in ;\
	done >> debian/control.tmp
	mv debian/control.tmp debian/control

configure: debian/configure-stamp
debian/configure-stamp:
	dh_testdir

	# Use the latest config.sub and config.guess from the autotools-dev
	# package.
	rm -f config.sub config.guess
	ln -s /usr/share/misc/config.sub config.sub
	ln -s /usr/share/misc/config.guess config.guess

	for python in $(PYTHON_VERSIONS); do \
	    set -e; \
	    mkdir -p build-$$python; \
	    cd build-$$python; \
	    ../configure $(commonconfflags) --with-python \
		PYTHON=/usr/bin/$$python; \
	    cd ..; \
	done

	for v in $(PHP_VERSIONS); do \
	    c=/usr/bin/php-config$$v; \
	    if test -x $$c ; then \
		set -e; \
		mkdir -p build-php$$v; \
		cd build-php$$v; \
		../configure $(commonconfflags) --with-php PHP_CONFIG=$$c; \
		cd ..; \
	    fi \
	done

	mkdir -p build-tcl-and-ruby
	cd build-tcl-and-ruby && ../configure $(commonconfflags) \
	    --with-tcl TCLSH=$(TCLSH) TCL_LIB=/usr/lib/tcltk \
	    --with-ruby RUBY=$(RUBY) RUBY_LIB=$(RUBY_LIB) RUBY_LIB_ARCH=$(RUBY_LIB_ARCH)

	touch $@

build: debian/build-stamp
debian/build-stamp: configure
	dh_testdir
	for python in $(PYTHON_VERSIONS); do \
	    set -e; \
	    $(MAKE) -C build-$$python ; \
	    $(MAKE_CHECK) -C build-$$python ; \
	done
	for v in $(PHP_VERSIONS); do \
	    if test -x /usr/bin/php-config$$v ; then \
		set -e; \
		$(MAKE) -C build-php$$v ; \
		$(MAKE_CHECK) -C build-php$$v ; \
	    fi \
	done
	$(MAKE) -C build-tcl-and-ruby
	$(MAKE_CHECK) -C build-tcl-and-ruby
	touch $@

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

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

	# Install xapian.php in /usr/share/php for PHP5 so that it's in the
	# PHP include_path, but for the PHP4 backport put it in /usr/share/php4
	# to avoid a file conflict with the php5-xapian package.  The file won't
	# be automatically found there, but it isn't vital for the PHP4 bindings
	# while it is for the PHP5 ones.
	#
	# Always install /etc/xapian/phpN/conf.d/xapian.ini, even for backports
	# to suites where PHP doesn't support it - the extra file is harmless,
	# and it means we'll work nicely with backported PHP packages too.
	for v in $(PHP_VERSIONS); do \
	    c=/usr/bin/php-config$$v; \
	    if test -x $$c ; then \
		set -e; \
		$(MAKE) -C build-php$$v DESTDIR=$(CURDIR)/debian/tmp \
		    phpincdir=/usr/share/php`echo $$v|tr -d 5` install; \
		install -d -m 755 debian/tmp/etc/php$$v/conf.d; \
		install -m 644 debian/etc-php-conf.d-xapian.ini debian/tmp/etc/php$$v/conf.d/xapian.ini; \
	    fi \
	done

ifneq ($(findstring .$(CODENAME)., .sarge.dapper.edgy.), )
	# If PHP doesn't support /etc/phpN/conf.d then add README.Debian with
	# a note explaining that user needs to modify php.ini themselves.  This
	# affects Debian sarge, and Ubuntu edgy (and earlier).
	for v in $(PHP_VERSIONS); do \
	    c=/usr/bin/php-config$$v; \
	    if test -x $$c ; then \
		set -e; \
		install -d -m 755 debian/php$$v-xapian/usr/share/doc/php$$v-xapian; \
		install -m 644 debian/php$$v-README.Debian debian/php$$v-xapian/usr/share/doc/php$$v-xapian/README.Debian; \
	    fi \
	done
endif

	$(MAKE) -C build-tcl-and-ruby DESTDIR=$(CURDIR)/debian/tmp install

	# Reads the '*.install' files to decide where to install everything.
	dh_install --sourcedir=debian/tmp --fail-missing $(DH_INSTALL_EXCLUDE)

binary: binary-arch
binary-arch: DH_OPTIONS=-a
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installman
	dh_installchangelogs ChangeLog
	dh_strip
	dh_link
	dh_compress
	dh_fixperms
	dh_makeshlibs
	# This allows the rules file to be used for backports to releases
	# which predate python-central.  Once python-central is in all the
	# Debian and Ubuntu releases we want to build packages for, the
	# second case can be removed.
ifeq ($(USE_PYVERSIONS), yes)
	DH_PYCENTRAL=include-links dh_pycentral
else
	# Recent Lintian versions warn if this file matches: /^\s+dh_python\s/
	# But we need to call dh_python for backports, so to avoid the warning
	# we insert `$(strip )' which expands to nothing.
	dh_$(strip )python
endif
	dh_installdeb

	dh_shlibdeps
	# Rewrite the dependency on libxapianN to be >= our version, since
	# we may require features added in that version.
	perl -pi -e \
	    's/^(shlibs:Depends=.*libxapian\d+)/$$1 (>= $(XAPIAN_VERSION))/' \
	    debian/*.substvars

	# php-config4 in sarge doesn't understand --phpapi and we want to
	# support backporting so get the api version from the phpN-cli
	# package's "Provides".
	for v in $(PHP_VERSIONS); do \
	    c=/usr/bin/php-config$$v; \
	    if test -x $$c ; then \
		set -e; \
		echo "php$$v:Depends=`dpkg -s php$$v-cli|sed 's/^Provides: //p;d'|sed 's/, */\n/g'|grep '^phpapi-'`, php$$v-common" >> debian/php$$v-xapian.substvars; \
	    fi \
	done

ifeq ($(findstring .$(CODENAME)., .sarge.etch.dapper.edgy.feisty.gutsy.), )
	# lenny/hardy onwards have a "tcl" dummy package which pulls in the
	# package for the current preferred version.  "tclsh" is a virtual
	# package which the Debian Tcl/Tk Policy recommends we specify as an
	# alternative if we work with all available Tcl/Tk versions:
	# http://pkg-tcltk.alioth.debian.org/tcltk-policy.html/ch-module_packages.html
	echo "tcl:Depends=tcl|tclsh" >> debian/tclxapian.substvars
else
	# sarge onwards don't have tcl8.2 or earlier, though we would work with
	# 8.2 and 8.1 if they were packaged.
	echo "tcl:Depends=tcl8.4|tcl8.3" >> debian/tclxapian.substvars
endif

	echo "ruby:Depends=libruby1.8" >> debian/libxapian-ruby1.8.substvars

	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep:
	echo "Nothing to do!"

clean:
	dh_testdir
	dh_testroot
	# Run distclean since upstream testsuites can create files in $srcdir
	# currently (e.g. python/testsuite.pyc).
	for d in build-* ; do \
	    test -d "$$d" || continue; \
	    set -e; \
	    $(MAKE) -C "$$d" distclean ; \
	    rm -rf "$$d" ; \
	done
	rm -f config.sub config.guess
	dh_clean
	rm -f debian/build-stamp debian/configure-stamp

.PHONY: maint maintclean configure build install binary binary-arch binary-indep clean
