#!/usr/bin/make -f
#
# Copyright (C) 2004,2005,2006 Lemur Consulting Ltd
# Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2013 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)

# 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

# >= 7.0.50~ needed for Ruby packaging.
BUILD_DEPS := debhelper (>= 7.0.50~),

# Disabled due to incompatibility of GPL and PHP licence.  Uncomment to build
# your own binary packages (just don't distribute them!)
# PHP_VERSIONS := 5

# Be conservative for now and leave backported versions on old ruby policy.
ifeq ($(findstring .$(CODENAME)., .lucid.precise.quantal.raring.squeeze.wheezy.), )
# For new ruby policy:
XB_RUBY_VERSIONS := XB-Ruby-Versions: $${ruby:Versions}
XS_RUBY_VERSIONS := XS-Ruby-Versions: all
rubylibdir := vendorlibdir
rubyarchdir := vendorarchdir
# Ruby 2.0 will debut in jessie and was in Ubuntu saucy, which neatly matches
# the versions on new ruby policy.
RUBY_VERSIONS := 1.9.1 2.0
else
# For old ruby policy:
XS_RUBY_VERSIONS := X-Dummy: Remove me once old ruby policy support is dropped
rubylibdir := rubylibdir
rubyarchdir := archdir
RUBY_VERSIONS := 1.8 1.9.1
endif

# The python versions to build for:
PYTHON_VERSIONS := $(shell pyversions -r debian/control)

ifeq ($(findstring .$(CODENAME)., .lucid.), )
USING_PYCENTRAL := 0
BUILD_DEPS += python, python-all-dev (>= 2.6.6-3~),
XB_PYTHON_VERSION := X-Dummy: Remove me once python-central support is dropped
X_PYTHON_VERSION := X-Python-Version: >= 2.3
else
USING_PYCENTRAL := 1
BUILD_DEPS += python, python-central (>= 0.4.10), python-all-dev,
XB_PYTHON_VERSION := XB-Python-Version: $${python:Versions}
X_PYTHON_VERSION := XS-Python-Version: all
endif

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)

CFLAGS := $(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)
CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)

# Fallback for suites which don't have dpkg-buildflags.
ifeq (,$(CFLAGS))
    # -g needed for armel to workaround unaligned relocations (#576944)
    CFLAGS := -g
    CXXFLAGS := -g
ifeq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
    CFLAGS += -O2
    CXXFLAGS += -O2
endif
endif

confflags := CFLAGS='$(CFLAGS)' CXXFLAGS='$(CXXFLAGS)'
confflags += LDFLAGS='$(LDFLAGS)' CPPFLAGS='$(CPPFLAGS)'
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, and
# dpkg-buildflags handles noopt.
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.
#
# 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!
ifeq ($(findstring .$(CODENAME)., .lucid.precise.quantal.raring.saucy.trusty.squeeze.wheezy.), )
# /usr/bin/tclsh-default is going to be dropped.
TCLSH=/usr/bin/tclsh
# tclsh8.6 requires us to link to libtclstub.a - upstream doesn't yet do this
# automatically.
TCL_MAKE_OPTS = TCL_LIBS=-ltclstub
else
TCLSH=/usr/bin/tclsh-default
TCL_MAKE_OPTS =
endif

# Build depends for Ruby.
#
# Only the first % in the replacement part is substituted, so we need to do
# this in two goes.
BUILD_DEPS += $(RUBY_VERSIONS:%=ruby%-dev,)
BUILD_DEPS += $(RUBY_VERSIONS:%=ruby%,)

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

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

# testsuite fails with newer ones
export AUTOMAKE = automake-1.11
export ACLOCAL = aclocal-1.11

maint: debian/control

maintclean: clean
	rm -f debian/control \
	      debian/libxapian-ruby1.8.doc-base \
	      debian/libxapian-ruby1.8.install \
	      debian/libxapian-ruby1.8.postinst \
	      debian/libxapian-ruby1.9.1.doc-base \
	      debian/libxapian-ruby1.9.1.install \
	      debian/libxapian-ruby1.9.1.postinst \
	      debian/ruby-xapian.doc-base \
	      debian/ruby-xapian.install

debian/control: debian/rules debian/control.in
	rm -f debian/control.tmp
	sed -e 's/@BUILD_DEPS@/$(BUILD_DEPS)/g' \
	    -e 's/@X_PYTHON_VERSION@/$(X_PYTHON_VERSION)/g' \
	    -e 's/@XB_PYTHON_VERSION@/$(XB_PYTHON_VERSION)/g' \
	    -e 's/@XS_RUBY_VERSIONS@/$(XS_RUBY_VERSIONS)/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
	# Call the doc-base templates <pkg>.d-b.in, as <pkg>.doc-base.* is
	# special to dh_installdocs. (#722096)
ifdef XB_RUBY_VERSIONS
	sed -e 's/@XB_RUBY_VERSIONS@/$(XB_RUBY_VERSIONS)/g' \
	    < debian/control-ruby-new.in >> debian/control.tmp
	cp debian/ruby-xapian.d-b.in debian/ruby-xapian.doc-base
	cp debian/ruby-xapian.install.in debian/ruby-xapian.install
	for v in $(RUBY_VERSIONS); do \
	    if test $$v = 2.0 ; then \
		echo 'usr/lib/*/ruby/vendor_ruby/*/_xapian.so' >> debian/ruby-xapian.install ; \
	    else \
		sed -e "s/@PACKAGE@/libxapian-ruby$$v/" \
		    -e 's/@TARGET@/ruby-xapian/' \
		    < debian/dummy.postinst.in \
		    > debian/libxapian-ruby$$v.postinst ;\
	    fi ;\
	done
else
	for v in $(RUBY_VERSIONS); do \
	    sed "s/@RUBY_VERSION@/$$v/g" debian/control-ruby-old.in >> debian/control.tmp ;\
	    cp debian/libxapian-ruby$$v.d-b.in debian/libxapian-ruby$$v.doc-base ;\
	    cp debian/libxapian-ruby$$v.install.in debian/libxapian-ruby$$v.install ;\
	done
endif
	mv debian/control.tmp debian/control

configure: configure-stamp
configure-stamp:
	dh_testdir

	dh_autoreconf

	for python in $(PYTHON_VERSIONS); do \
	    set -e; \
	    mkdir -p debian/build/$$python; \
	    cd debian/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 debian/build/php$$v; \
		cd debian/build/php$$v; \
		../../../configure $(commonconfflags) --with-php PHP_CONFIG=$$c; \
		cd ../../..; \
	    fi \
	done

	for v in $(RUBY_VERSIONS); do \
	    set -e; \
	    mkdir -p debian/build/ruby$$v; \
	    cd debian/build/ruby$$v; \
	    RUBY=/usr/bin/ruby$$v; \
	    ../../../configure $(commonconfflags) --with-ruby \
		--docdir=/usr/share/doc/xapian-bindings-ruby$$v \
		RUBY=$$RUBY \
		RUBY_LIB=`$$RUBY -rrbconfig -e 'puts RbConfig::CONFIG["$(rubylibdir)"]'` \
		RUBY_LIB_ARCH=`$$RUBY -rrbconfig -e 'puts RbConfig::CONFIG["$(rubyarchdir)"]'` \
		RUBY_INC_ARCH=`$$RUBY -rrbconfig -e 'd = RbConfig::CONFIG["rubyhdrdir"]; print RbConfig::CONFIG["rubyarchhdrdir"] || (d ? d + "/" + RbConfig::CONFIG["arch"] : RbConfig::CONFIG["archdir"])'`; \
	    cd ../../..; \
	done

	mkdir -p debian/build/tcl
	cd debian/build/tcl && ../../../configure $(commonconfflags) \
	    --with-tcl TCLSH=$(TCLSH) TCL_LIB=/usr/lib/tcltk

	touch $@

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

install: DH_OPTIONS=
install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Install the files under 'debian/tmp'.
	for python in $(PYTHON_VERSIONS); do \
	    set -e; \
	    $(MAKE) -C debian/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.
	for v in $(PHP_VERSIONS); do \
	    c=/usr/bin/php-config$$v; \
	    if test -x $$c ; then \
		set -e; \
		$(MAKE) -C debian/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

	for v in $(RUBY_VERSIONS); do \
	    set -e; \
	    $(MAKE) -C debian/build/ruby$$v DESTDIR=$(CURDIR)/debian/tmp install; \
	done
ifdef XB_RUBY_VERSIONS
	# We only need one xapian.rb for ruby-xapian, and it should be the same
	# for all versions, so if present, check that the 1.8 and 2.0 versions
	# are the same as the 1.9.1, then throw them away.
	test ! -f debian/tmp/usr/lib/ruby/vendor_ruby/1.8/xapian.rb || \
	    diff debian/tmp/usr/lib/ruby/vendor_ruby/1.8/xapian.rb \
		 debian/tmp/usr/lib/ruby/vendor_ruby/1.9.1/xapian.rb
	test ! -f debian/tmp/usr/lib/ruby/vendor_ruby/2.0*/xapian.rb || \
	    diff debian/tmp/usr/lib/ruby/vendor_ruby/2.0*/xapian.rb \
		 debian/tmp/usr/lib/ruby/vendor_ruby/1.9.1/xapian.rb
	rm -f debian/tmp/usr/lib/ruby/vendor_ruby/1.8/xapian.rb \
	      debian/tmp/usr/lib/ruby/vendor_ruby/2.0*/xapian.rb
	# The same goes for the docs.  Docs for 1.9.1 are installed by
	# ruby-xapian.install.
	rm -rf debian/tmp/usr/share/doc/xapian-bindings-ruby1.8 \
	       debian/tmp/usr/share/doc/xapian-bindings-ruby2.0
endif

	$(MAKE) -C debian/build/tcl 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)
	touch $@

binary: binary-arch binary-indep
binary-arch: DH_OPTIONS=-a
binary-arch: build-arch 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
ifeq ($(USING_PYCENTRAL), 1)
	DH_PYCENTRAL=include-links dh_pycentral
else
	dh_python2
endif
	dh_installdeb

	dh_shlibdeps
	# Rewrite the dependency on libxapianN to be >= our version, since
	# we may require features added in that version.
	sed -i \
	    's/^shlibs:Depends=.*libxapian[0-9]\+/& (>= $(XAPIAN_VERSION))/' \
	    debian/*.substvars

	for v in $(PHP_VERSIONS); do \
	    c=/usr/bin/php-config$$v; \
	    if test -x $$c ; then \
		set -e; \
		echo "php$$v:Depends=phpapi-`$$c --phpapi`, php$$v-common" >> debian/php$$v-xapian.substvars; \
	    fi \
	done

	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep: DH_OPTIONS=-i
binary-indep: build-indep install
	dh_testdir
	dh_testroot
	for v in $(RUBY_VERSIONS); do \
	    set -e; \
	    d=debian/libxapian-ruby$$v/usr/share/doc; \
	    mkdir -p "$$d"; \
	    ln -s ruby-xapian "$$d/libxapian-ruby$$v"; \
	done
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

clean:
	dh_testdir
	dh_testroot
	rm -rf debian/build
	rm -f config.sub config.guess
	dh_autoreconf_clean
	dh_clean

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