#!/usr/bin/make -f
#
# debian/rules for monotone, by Zack Weinberg
# Based on a public domain template, originally written by Joey Hess.

# Uncomment this to turn on verbose mode. 
export DH_VERBOSE=1

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
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_HOST_GNU_TYPE)" "$(DEB_BUILD_GNU_TYPE)"
# not cross-compiling; don't specify --host, which will confuse configure
BUILD_AND_HOST := --build=$(DEB_BUILD_GNU_TYPE)
else
# cross-compiling: need both switches.  Also, turn off the testsuite
# (the cross-compile environment should have done this anyway, but
# let's make sure).
BUILD_AND_HOST := --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE)
DEB_BUILD_OPTIONS += nocheck
endif

# This enables parallelism.
ifneq (,$(findstring parallel=,$(DEB_BUILD_OPTIONS)))
PAR := -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

# DEB_BUILD_OPTIONS=noopt handling (Policy 10.1) and arch-specific
# compiler options.  Allow total override of CFLAGS from the
# make command line (make -f debian/rules build-arch CFLAGS=whatever).
# Warning options are activated by upstream configure.
DEFAULT_CFLAGS = -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
DEFAULT_CFLAGS += -O0
# HPPA needs -mlong-calls when built with -O0 because the code is so
# huge.  Might be addressed by more use of shared libraries - revisit
# if we ever do anything about bug #318509.
ifneq (,$(findstring hppa,$(DEB_HOST_GNU_TYPE)))
DEFAULT_CFLAGS += -mlong-calls
endif
else # optimization enabled
DEFAULT_CFLAGS += -O2
endif

CFLAGS ?= $(DEFAULT_CFLAGS)

# put this up top so it's the default rule.
#
# Policy (section 4.9) says that build should depend on build-indep.
# However, this is only a "should", and it doesn't work, because
# dpkg-buildpackage uses build even in -B mode.  Thus, if build
# depends on build-indep, even a -B build will try to generate the
# manuals -- and the buildds will do this having installed only the
# Build-Depends, for they are ignorant of B-D-I.  All die, O the
# embarrassment.  (This has been a festering known problem for at
# least five years; see bugs #218893 and #229357.  There is a bunch of
# recent discussion but, as far as I can tell, no actual forward
# progress is being made.)
#
# Conversely, not having build depend on build-indep means that if you
# do a normal dpkg-buildpackage, the manual gets generated in the
# binary-indep phase rather than the build phase, which works fine (as
# long as you are being sensible and using fakeroot, anyway; I haven't
# tried it with actual root privileges and don't propose to).
build: build-arch

config.status: configure
	dh_testdir
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
endif
	./configure $(BUILD_AND_HOST) \
		    --with-system-pcre \
		    --prefix=/usr --mandir='$${prefix}/share/man' \
		    --infodir='$${prefix}/share/info' CFLAGS="$(CFLAGS)" \
		    CXXFLAGS="$(CFLAGS)" LDFLAGS="-Wl,-z,defs"

# We don't want to generate the info documentation in build-arch, so
# we have to bypass the 'all' target.  (Revisit after automake-ectomy.)

# The testsuite is still not entirely reliable in parallel mode, and
# thanks to buildd configuration decisions that no one will explain to
# me in sufficient detail to detect reliably, we have to disable all
# netsync tests.  (We could get away with only doing that on mips, but
# who knows whether another architecture will decide to do the same
# thing.)  The real fix for both these issues will be network tests
# over local domain sockets, coming Real Soon Now from upstream.
build-arch: build-arch-stamp
build-arch-stamp: config.status
	make $(PAR) -C po all
	make $(PAR) mtn
ifeq "$(findstring nocheck, $(DEB_BUILD_OPTIONS))" ""
	make $(PAR) unit_tester tester check_net
	DISABLE_NETWORK_TESTS=1 make check-local || sh contrib/dump-test-logs.sh
endif
	touch $@

build-indep: build-indep-stamp
build-indep-stamp: config.status

# Just the formatted documentation.
	make $(PAR) info html monotone.pdf
	touch $@

clean:
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f build-arch-stamp build-indep-stamp
	dh_clean

# Build architecture-independent files here.
#
# Both monotone-server and monotone-doc are permitted to have their
# documentation directories be symlinks to the main package's
# documentation directory, per Policy 12.5.  This means we should not
# run dh_installexamples or dh_installchangelogs for either, and should
# not run dh_installdocs for monotone-server.  monotone-doc needs us
# to run dh_installdocs for monotone.pdf and doc-base support, but then
# needs a bunch of -X flags to avoid conflicting files.
#
# The upstream makefiles' install targets do nothing useful for these
# packages.  (In particular, we do not want the monolithic
# monotone.html installed.)  Annoyingly, at present we cannot simply
# dh_installdocs the entire html directory, because it contains a
# bunch of unwanted junk; instead we dh_installdirs the directory
# hierarchy that is required and use cp -p to put all the files in
# place.  (Using globs in monotone-doc.docs would get everything dumped
# directly into /usr/share/doc/monotone, which doesn't work.)

binary-indep: build-indep
	dh_testdir
	dh_testroot
	dh_clean -k

	dh_installdirs -i
	cd debian/monotone-doc/usr/share/doc && \
	    ln -s monotone monotone-doc
	cd debian/monotone-server/usr/share/doc && \
	    ln -s monotone monotone-server

	cp -p html/*.html html/*.css \
	    debian/monotone-doc/usr/share/doc/monotone/html
	cp -p html/figures/*.png \
	    debian/monotone-doc/usr/share/doc/monotone/html/figures
	dh_installdocs -p monotone-doc -X copyright -X NEWS

	cp -p examples/read-permissions \
	    debian/monotone-server/usr/share/monotone/read-permissions.base
	cp -p examples/write-permissions \
	    debian/monotone-server/usr/share/monotone/write-permissions.base
	cp -p contrib/get_passphrase_from_file.lua \
	    debian/monotone-server/usr/share/monotone/hooks.lua.base

	dh_installdebconf -i

# we should have logrotate support for monotone-server, but currently we don't
#	dh_installlogrotate -i --name=monotone
	dh_installinit -i --name=monotone
	dh_installinfo -i
	dh_compress -i -X .pdf
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build-arch
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -a

# again because we don't want the documentation in *-arch, we can't
# use the generic install target; instead, use the *nonrecursive*
# install-exec target for the top level makefile and the install-data
# target for the po directory.  (using the recursive install-exec
# target would work too, but there's no point.)
#
# N.B. DEB_BUILD_OPTIONS=nostrip is handled entirely within dh_strip.
	$(MAKE) DESTDIR=$(shell pwd)/debian/monotone install-exec-am
	$(MAKE) DESTDIR=$(shell pwd)/debian/monotone -C po install-data

# dh_installdirs created etc/bash_completion.d.  dh_install cannot install
# a file under a different basename.
	cp contrib/monotone.bash_completion \
	   debian/monotone/etc/bash_completion.d/monotone

	dh_installchangelogs -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installman -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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