#!/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.
CPUS ?= $(shell getconf _NPROCESSORS_ONLN)

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

build-arch: build-arch-stamp
build-arch-stamp: config.status

# 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.
	make -j$(CPUS) -C po all
	make -j$(CPUS) mtn
ifeq "$(findstring nocheck, $(DEB_BUILD_OPTIONS))" ""
	make -j$(CPUS) unit_tester tester
	make check-local
endif
	touch $@

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

# Just the formatted documentation.
	make -j$(CPUS) 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.
binary-indep: build-indep
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -i
# 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.
	cp -p html/*.html html/*.css debian/monotone-doc/usr/share/doc/html
	cp -p html/figures/*.png debian/monotone-doc/usr/share/doc/html/figures

	dh_installchangelogs -i
	dh_installdebconf -i
	dh_installdocs -i
	dh_installexamples -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
	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
