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

# ${binary:Version} was added after sarge and dapper, so use the old name for
# it (${Source-Version}) there.
ifneq ($(findstring .$(CODENAME)., .sarge.dapper.), )
BINARY_VERSION := $${Source-Version}
else
BINARY_VERSION := $${binary:Version}
endif

# The soversion number needs to match that generated by libtool.  The following
# lines should read configure.ac to get the current interface number and
# subtract it from the interface age, to get the oldest interface number
# supported by the library. This should correspond to the soversion assigned
# to the library by libtool.  (This isn't documented, and is system dependent,
# so we need to check it doesn't break with future versions of libtool.)
libxapian_soversion:=$(shell sed 's/^LIBRARY_VERSION_INFO=\([0-9][0-9]*\):[0-9][0-9]*:\([0-9][0-9]*\).*/\1-\2/p;d' configure.ac)
libxapian_soversion:=$(shell echo $$(($(libxapian_soversion))))

# For now, we don't support installation of development files for multiple
# versions concurrently.  Incompatible ABI changes should be rare, so this
# seems reasonable.
libxapian_dev_soversion=

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)
export DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

confflags := --prefix=/usr --sysconfdir=/etc
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
    confflags += --build $(DEB_HOST_GNU_TYPE)
    ifeq ($(DEB_HOST_ARCH), alpha)
	# The alpha buildd is failing with a SEGV while trying to skip testcase
	# nomoredocids1 for the inmemory backend.  Skipping works by throwing
	# an exception so this smells like a toolchain bug to me.  Disabling
	# optimisation seems to avoid this (and fixes a SEGV in the generated
	# code for firefox #503051).
	DEB_BUILD_OPTIONS += noopt
    endif
else
    confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
    # Disable the testsuite when cross-compiling.
    DEB_BUILD_OPTIONS += nocheck
endif

ifeq ($(DEB_HOST_ARCH), avr32)
    # The avr32 buildd is repeatedly failing with "Took too long", so disable
    # optimisation and the testsuite for now for a faster build.
    DEB_BUILD_OPTIONS += noopt nocheck
endif

CFLAGS :=
CXXFLAGS := -g

ifeq ($(DEB_HOST_ARCH), hurd-i386)
    # Testcase topercent2 fails without -ffloat-store - smells like an excess
    # precision issue.
    CXXFLAGS += -ffloat-store
endif

# Handle DEB_BUILD_OPTIONS.  Note that dh_strip handles nostrip for us.

ifeq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
    CFLAGS += -O2
    CXXFLAGS += -O2
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 VALGRIND=
endif

confflags += CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)"

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

maint: \
	debian/control \
	debian/libxapian$(libxapian_soversion).install \
	debian/libxapian$(libxapian_dev_soversion)-dev.install

maintclean: clean
	rm -f debian/control
	rm -f debian/libxapian$(libxapian_soversion).install
	rm -f debian/libxapian$(libxapian_dev_soversion)-dev.install
	rm -rf debian/libxapian$(libxapian_soversion)
	rm -rf debian/libxapian$(libxapian_dev_soversion)-dev
	rm -rf debian/xapian-doc
	rm -rf debian/xapian-tools
	rm -rf debian/tmp

debian/control: debian/rules debian/control.in debian/compat debian/patches/series
	rm -f debian/control.tmp
	sed -e 's/@LIBXAPIAN_SOVERSION@/$(libxapian_soversion)/g' \
	    -e 's/@LIBXAPIAN_DEV_SOVERSION@/$(libxapian_dev_soversion)/g' \
	    -e 's/@BINARY_VERSION@/$(BINARY_VERSION)/g' \
	    -e 's/@BUILD_DEPS@/debhelper (>= $(shell cat debian/compat)),$(shell grep -qv '^#' debian/patches/series && echo " quilt,")/g' \
	     debian/control.in > debian/control.tmp
	mv debian/control.tmp debian/control

debian/libxapian$(libxapian_soversion).install: debian/rules debian/libxapianVERSION.install
	cp -f debian/libxapianVERSION.install debian/libxapian$(libxapian_soversion).install

debian/libxapian$(libxapian_dev_soversion)-dev.install: debian/rules debian/libxapianVERSION-dev.install
	cp -f debian/libxapianVERSION-dev.install debian/libxapian$(libxapian_dev_soversion)-dev.install

# Include quilt's fragment if it is installed.
-include /usr/share/quilt/quilt.make

# Dummy patch and unpatch targets in case quilt isn't installed.
patch unpatch:

configure: configure-stamp
configure-stamp: $(QUILT_STAMPFN) debian/control
	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

	# Report kernel and compiler version so upstream PLATFORMS file can be
	# updated from buildd logs.
	-uname -a
	-g++ --version

	# Configure in a subdirectory, for neatness.
	mkdir -p build
	cd build && ../configure $(confflags)

	# Touch the stamp file, to avoid repeating the configure step.
	touch $@

build: build-stamp
build-stamp: configure
	dh_testdir
	$(MAKE) -C build
	$(MAKE_CHECK) -C build
	touch $@

install: DH_OPTIONS=
install: build \
	 debian/libxapian$(libxapian_soversion).install \
	 debian/libxapian$(libxapian_dev_soversion)-dev.install
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Install the files into debian/tmp
	$(MAKE) -C build DESTDIR=$(CURDIR)/debian/tmp install

	# Install the example source code
	mkdir -p debian/tmp/usr/share/doc/xapian-examples/examples
	cp examples/*.cc debian/tmp/usr/share/doc/xapian-examples/examples
	ln -s ../../../../lib/xapian-examples/examples debian/tmp/usr/share/doc/xapian-examples/examples/built

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


binary: binary-arch binary-indep
binary-indep: DH_OPTIONS=-i
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installman
	dh_installchangelogs ChangeLog
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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 --dbg-package=libxapian$(libxapian_soversion)-dbg
	dh_link
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -L libxapian$(libxapian_soversion) -ldebian/libxapian$(libxapian_soversion)/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

clean: debian/control unpatch
	dh_testdir
	dh_testroot
	rm -rf build
	#rm -f config.sub config.guess
	dh_clean
	rm -f build-stamp configure-stamp

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