#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

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

# API version
apiver=$(shell grep -i '^GOFFICE_API_VER=[0-9]' configure | sed -e 's/^.*=//')
# ABI major version
somajor=$(shell grep -i '^VERSION_INFO=[0-9]' configure | sed -e 's/^.*=//' -e 's/:.*//')
# The base library name
baselibname=libgoffice-$(apiver)-$(somajor)
pruntime=$(baselibname)
pcommon=$(baselibname)-common
pdevel=libgoffice-$(apiver)-dev
pdbg=libgoffice-0.8-dbg

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS = -Wall -g

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

# Ensure the build aborts when there are still references to undefined
# symbols.
LDFLAGS += -Wl,-z,defs

# 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

# Make the linker find the X libraries
LDFLAGS += -L/usr/X11R6/lib

confflags := -v \
	--host=$(DEB_HOST_GNU_TYPE) \
	--build=$(DEB_BUILD_GNU_TYPE) \
	--prefix=/usr \
	--mandir=\$${prefix}/share/man \
	--infodir=\$${prefix}/share/info \
	--with-html-dir=\$${prefix}/share/doc/$(pdevel)/html

# FHS locations
confflags +=	--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--datadir=\$${prefix}/share \
		--libdir=\$${prefix}/lib

# Compiler, language dialect
confflags +=    --disable-iso-c \
                --enable-compile-warnings=maximum

# Features
confflags +=    --enable-static --enable-gtk-doc

DPKG_GENSYMBOLS_CHECK_LEVEL ?= 4
export DPKG_GENSYMBOLS_CHECK_LEVEL

.PHONY: source-updates
source-updates:
	# Update files/links generated by autotools
	env srcdir=`pwd` NOCONFIGURE=1 sh autogen.sh
	# Replace links by copies of their targets
	for file in `find -type l` ; do \
		tf=`tempfile` && \
		install -p $$file $$tf && \
		rm -f $$file && \
		install -p $$tf $$file && \
		rm -f $$tf ; \
	done
	rm -rf autom4te.cache config-stamp

.PHONY: configure
configure: config-stamp

config-stamp:
	dh_testdir
	dh_prep
	if ! test -r debian/$(pruntime).install ; then \
		echo "Packaging files are not set up for base library name $(pruntime). Aborting." ; \
		exit 1 ; \
	fi

	# Generate a .pot file
	cd po && intltool-update -p
	
	# Add here commands to configure the package.

	rm -rf build && mkdir build
	rm -f goffice/goffice-paths.h
	dh_autotools-dev_updateconfig
	cd build && env "CC=$(CC) $(LDFLAGS)" ../configure $(confflags)

	touch config-stamp

build-stamp: config-stamp
	dh_testdir

	$(MAKE) -C build

	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 config-stamp build-stamp

	if test -d build && test -f build/Makefile ; then \
		$(MAKE) -C build distclean; \
	fi
	rm -rf build
	-find -type f -name 'Makefile.am' | sed -e 's/\.am$$//' | xargs rm -f

	# Update config.{sub,guess} from autotools-dev, if possible.
	dh_autotools-dev_restoreconfig

	dh_clean 

.PHONY: install
install: build
	dh_testdir
	dh_testroot
	dh_installdirs

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

#	# FIXME: where do we put the debug for the plugins?
#	mkdir -p $(CURDIR)/debian/tmp/usr/lib/debug
#	for l in `find $(CURDIR)/debian/tmp/usr/lib -name '*.so*'`; do \
#		cp -vdf $$l $(CURDIR)/debian/tmp/usr/lib/debug ; \
#	done

	rm -f $(CURDIR)/debian/tmp/usr/lib/libgoffice-*.la
	rm -f $(CURDIR)/debian/tmp/usr/lib/goffice/*/plugins/*/*.la

# This single target is used to build all the packages, all at once, or
# one at a time. So keep in mind: any options passed to commands here will
# affect _all_ packages. Anything that should only affect one package
# should be put in another target (such as the install target) or be put
# into conditional constructs.
.PHONY: binary-common
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
ifeq ($(DH_OPTIONS),-s)
	rm -rvf $(CURDIR)/debian/$(pdbg)/usr/share/doc/$(pdbg)
endif
	dh_install --sourcedir=debian/tmp
ifeq ($(DH_OPTIONS),-s)
	dh_install -p$(pdbg) --sourcedir=debian/tmp
endif
	# 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/$(pruntime)/$$l ; \
	done < debian/$(pcommon).install

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

#	dh_installdebconf -s
	dh_installdocs -s
	dh_installexamples -s
#	dh_installmenu -s
#	dh_installlogrotate -s
#	dh_installemacsen -s
#	dh_installpam -s
	dh_installmime -s
#	dh_installinit -s
#	dh_installcron -s
	dh_installman -s
	dh_installinfo -s
	dh_installdocs
	dh_installexamples
#	dh_install
#	dh_installmenu
#	dh_installdebconf
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
	dh_installmime
#	dh_installinit
#	dh_installcron
	dh_installinfo
	dh_installman
	dh_gconf
ifeq ($(DH_OPTIONS),-s)
	rm -rvf $(CURDIR)/debian/$(pdbg)/usr/share/doc/$(pdbg)
endif
	dh_link
	dh_buildinfo
	dh_compress
#	dh_perl
#	dh_python
ifeq ($(DH_OPTIONS),-s)
	dh_strip -v --dbg-package=$(pdbg)

        # Unfortunately, "-s" overrides "-p" for dh_makeshlibs and by acting
        # on all architecture-dependent packages we would get wrong shlibs
        # here, so we need to undo the "-s".
	env -u DH_OPTIONS dh_makeshlibs -s -p$(pruntime) -V '$(pruntime) (>= $(upstreamversion))'

	dh_shlibdeps -s -l debian/$(pruntime)/usr/lib/ -L $(pruntime)
endif
	dh_fixperms
	dh_installdeb
	dh_gencontrol -v -- -VUpstream-Version=$(upstreamversion)
	dh_md5sums
	dh_builddeb

# Build architecture independent packages using the common target.
.PHONY: binary-indep
binary-indep: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
.PHONY: binary-arch
binary-arch: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common

## Any other binary targets build just one binary package at a time.
#binary-%: build install
#	make -f debian/rules binary-common DH_OPTIONS=-p$*
#

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