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

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
# 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

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

ifeq ($(CODENAME),hardy)
# We need -lz on hardy since adding the libmagic-dev B-D.  I suspect in hardy
# the .la file for libmagic lists -lz, though I haven't verified this.
BUILD_DEPS += zlib1g-dev,
endif

commonconfflags := \
	$(confflags) \
	--prefix=/usr \
	--sysconfdir=/etc \
	$(shell dpkg-buildflags --export=configure)

# 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
	mv debian/control.tmp debian/control

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

	# Use the latest config.sub and config.guess from the autotools-dev
	# package.
	dh_autotools-dev_updateconfig

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

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

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: configure-stamp
	dh_testdir
	$(MAKE) -C debian/build
	$(MAKE_CHECK) -C debian/build
	touch $@

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

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

	# Replace omega.conf with one with paths suitable for debian.
	rm -f debian/tmp/etc/omega.conf
	install -m 644 debian/omega.conf.debian debian/tmp/etc/omega.conf

	# Install the icons.
	install -d -m 755 debian/tmp/usr/share/images/xapian-omega
	install -m 644 images/*.png debian/tmp/usr/share/images/xapian-omega

	# Install the omegascript templates.
	install -d -m 755 debian/tmp/usr/share/xapian-omega/templates
	install -d -m 755 debian/tmp/usr/share/xapian-omega/templates/inc
	install -m 644 templates/inc/[a-z]* debian/tmp/usr/share/xapian-omega/templates/inc
	install -m 644 templates/[a-hj-z]* debian/tmp/usr/share/xapian-omega/templates

	# Fix paths in query template, and install that instead.
	sed 's!/icons/omega/!/images/xapian-omega/!g' templates/query > debian/query.tmp
	install -m 644 debian/query.tmp debian/tmp/usr/share/xapian-omega/templates/query
	rm -f debian/query.tmp

	# Symlink them in as examples (they can't just go under /usr/share/doc
	# because policy 12.3 says "Packages must not require the existence of
	# any files in `/usr/share/doc/' in order to function").
	install -d -m 755 debian/tmp/usr/share/doc/xapian-omega/examples
	ln -s ../../../xapian-omega/templates debian/tmp/usr/share/doc/xapian-omega/examples/templates

	# Create the /var directories.
	install -d -m 755 debian/tmp/var/lib/xapian-omega/data
	install -d -m 755 debian/tmp/var/lib/xapian-omega/cdb
	install -d -m 755 debian/tmp/var/log/xapian-omega

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

	touch $@

binary: binary-arch binary-indep
binary-indep: DH_OPTIONS=-i
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
	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

	dh_gencontrol
	dh_md5sums
	dh_builddeb

clean:
	dh_testdir
	dh_testroot
	rm -rf debian/build
	dh_autotools-dev_restoreconfig
	dh_clean

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