#! /usr/bin/make -f
# Based on the sample debian/rules that uses debhelper. 
# GNU copyright 1997 by Joey Hess.

#DEB_BUILD_OPTIONS?="debug nostrip"
export DEB_BUILD_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),m68k-linux)
	DEB_BUILD_OPTIONS+=noopt
endif

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

# Try to preserve timestamps as much as possible
export INSTALL=/usr/bin/install -p

# Are we building a release version, or are we doing development?
CVS_BUILD=$(shell test ! -d CVS; echo $$?)

upstreamversion=$(shell dpkg-parsechangelog | grep Version | head -1 | sed -e 's/Version: //g' -e 's/-[A-Za-z0-9+\.]*$$//g')

# Features configuration
# ---------------------- 
#
# We're aiming for a package that's stable, with as much functionality as is
# reasonably possible.

features:= 

# Spreadsheet indexer for beagle
features+= --enable-ssindex

# Plugins to access databases through the GDA framework.
#
# The GnomeDB plugin for gnumeric requires a version of GnomeDB that is not
# available as packages yet.
#	Tue, 01 Sep 2009 20:49:41 +0200
features+= --without-gda

# Psion files support?
features+= --without-psiconv

# Paradox database files plugin
features+= --with-paradox

# Perl plugin
features+= --with-perl

# The Python plugin
features+= PYTHON=/usr/bin/python --with-python

# Don't build a PDF of the documentation.
features+= --disable-pdfdocs

## Enable it for my development tree:
#features+= --enable-pdfdocs

confflags :=	--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--sysconfdir=/etc \
		--localstatedir=/var/lib \
		--libexecdir=/usr/bin \
                --enable-compile-warnings=minimum \
		--enable-introspection=yes \
		--disable-maintainer-mode \
		$(features)

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)
endif

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

CFLAGS += -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
        CFLAGS += -O0
else
        CFLAGS += -O2
endif

# Make the linker work a bit harder so dynamic loading can be done faster.
LDFLAGS += -Wl,-O1

# Make the linker only include actual dependencies on libraries, rather than
# for all libraries specified in the link line.
LDFLAGS += -Wl,--as-needed

.PHONY: source-updates
source-updates:
# This target is not part of the regular package build process. It is used
# by the package maintainer for development purposes and may have
# dependencies beyond the build dependencies specified in debian/control.
	rm -f configure-stamp
	# Update files generated by autotools
	env srcdir=`pwd` NOCONFIGURE=1 \
		LIBTOOLIZE="libtoolize --copy" \
		INTLTOOLIZE="intltoolize --copy" \
		sh autogen.sh $(confflags)
	convert icons/32x32/gnumeric.png -depth 4 debian/gnome-gnumeric.xpm

configure-stamp:
	dh_testdir

	cd po; intltool-update -p
	cd po-functions; intltool-update -p

	# Prepare for build
	dh_autotools-dev_updateconfig 
	mkdir build
	cd build && env srcdir=`pwd`/.. \
		../configure $(confflags) "CC=$(CC) $(LDFLAGS)" "CFLAGS=$(CFLAGS)"

	touch configure-stamp

.PHONY: configure
configure: configure-stamp

build-stamp: configure
	dh_testdir

	cd build && $(MAKE)
	touch build-stamp

.PHONY: build-arch
build-arch: build-stamp

.PHONY: build-indep
build-indep: build-stamp

.PHONY: build
build: build-arch build-indep

.PHONY: clean
clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp debian/*substvars debian/*~ \
		debian/buildinfo

	rm -rf debian/tmp build

	debconf-updatepo

	-find -type f -name 'Makefile.in' | sed -e 's/\.in$$//' | xargs rm -f
	-rm -f po/*.gmo

	# Clean out files generated as part of the documentation generation
	-rm -f doc/C/index.html doc/C/gnumeric.html doc/C/ld-id*.html \
		omf-install/gnumeric-C.omf
	-rm -rf doc/C/gnumeric doc/C/DBTOHTML_OUTPUT_DIR*

	-rm -rf aclocal.m4 config.h.in stamp-h.in autom4te.cache \
		doc/C/gnumeric.junk

	-rm -f GNOME_Gnumeric.server.in GNOME_Gnumeric.server src/gnumeric-paths.h

	dh_clean

.PHONY: install
install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	cd build && env GCONF_DISABLE_MAKEFILE_SCHEMA_INSTALL=1 \
		$(MAKE) install \
		DESTDIR=`pwd`/../debian/tmp/ \
		localstatedir=`pwd`/../debian/tmp/var/lib \
		libexecdir=`pwd`/../debian/tmp/usr/bin \
		scrollkeeper_localstate_dir=/var/lib/scrollkeeper

	# Plugins are not intended as stand-alone executables
	find `pwd`/debian/tmp/usr/lib/*/*/plugins -type f -print0 | xargs -0 chmod a-x
	# .la files are not needed for plugins
	find `pwd`/debian/tmp/usr/lib/*/*/plugins -type f -name '*.la' -print0 | xargs -0 rm -f

	# Remove unwanted files: static libraries and documentation sources
	find `pwd`/debian/tmp/usr/ -name '*.a' -print0 | xargs -0 rm -f
	find `pwd`/debian/tmp/ -name '*.sgml' -print0 | xargs -0 rm -f
	# Bonobo-only files
	rm -rf debian/tmp/usr/lib/bonobo
	# libspreadsheet is far from a mature library; loose the development
	# files
	rm -f debian/tmp/usr/lib/libspreadsheet*.la
	rm -f debian/tmp/usr/lib/pkgconfig/libspreadsheet-*.pc
	rm -rf debian/tmp/usr/include/libspreadsheet-*

	# Outdated
	rm -f debian/tmp/usr/share/mc/templates/gnumeric.desktop

	# Remove scrollkeeper generated files as well; for a package they
	# need to be (re)generated at package (de)install time.
	rm -rf `pwd`/debian/tmp/var/lib/scrollkeeper

	# Low-colour icon for use by the menu system
	$(INSTALL) -m 644 debian/gnome-gnumeric.xpm `pwd`/debian/tmp/usr/share/pixmaps/

	dh_install --sourcedir=`pwd`/debian/tmp --fail-missing

	# Name the binary just "gnumeric"
	cd debian/gnumeric/usr/bin && mv gnumeric-* gnumeric

	dh_link usr/share/gnumeric/$(upstreamversion)/idl/GNOME_Gnumeric.idl usr/share/idl/GNOME_Gnumeric.idl

	# Make the documentation accessible to gnumeric
	dh_link -pgnumeric-doc usr/share/gnome/help/gnumeric usr/share/gnumeric/$(upstreamversion)/doc

	dh_link -pgnumeric-doc usr/share/xml/docbook/stylesheet/nwalsh/images/ usr/share/gnumeric/$(upstreamversion)/doc/C/images

	# Remove files from the main package that belong to a more
	# specialist package
	# (dh_install doesn't do the right thing here IMO; perhaps it
	# doesn't like multiple globs in one line)
	while read l ; do \
		rm -rvf debian/gnumeric/$$l ; \
	done < debian/gnumeric-plugins-extra.install

	# Remove dirs that are now empty
	-find debian/gnumeric -depth -type d | xargs rmdir >/dev/null 2>&1

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installdebconf -i
	dh_installdocs -i

	dh_installexamples -i
	dh_python2 -i
	dh_installmenu -i
#	dh_installlogrotate -i
#	dh_installemacsen -i
#	dh_installpam -i
	dh_installmime -i
#	dh_installinit -i
	dh_installcron -i
#	dh_installman -i
	dh_link -i
	dh_installinfo -i
#	dh_undocumented -i
	dh_installchangelogs ChangeLog -i
	dh_gconf -i
	dh_icons -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_perl -i
	dh_gencontrol -v -i -- -VUpstream-Version=$(upstreamversion)
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
.PHONY: binary-arch
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installdebconf -a
	dh_installdocs -a --noscripts
	rm -rf `pwd`/debian/gnumeric-doc/usr/share/doc-base
	dh_installexamples -a
	dh_python2 -a
	dh_installmenu -a
#	dh_installlogrotate -a
#	dh_installemacsen -a
#	dh_installpam -a
	dh_installmime -a
#	dh_installinit -a
	dh_installcron -a
	dh_installman -a
	dh_link -a
	dh_installinfo -a
#	dh_undocumented -a
	dh_installchangelogs ChangeLog -a
	dh_gconf -a
	dh_icons -a
	dh_strip -a
	dh_link -a
	dh_compress -a

	dh_buildinfo

	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_perl -a

	dh_shlibdeps -v -a

	dh_gencontrol -v -a -- -VUpstream-Version=$(upstreamversion)
	dh_md5sums -a
	dh_builddeb -a

.PHONY: binary
binary: binary-indep binary-arch
