#!/usr/bin/make -f
# -*- makefile -*-

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

PACKAGE := abiword
VER := $(shell dpkg-parsechangelog | perl -ne 'print $$1 if m/Version:\s*([\d\.+svn]+)/')
UREV := $(shell echo $(VER) | perl -ne 'print $$1 if m/\+svn(\d+)/')

# parallel build by default on linux
ifeq ($(DEB_HOST_ARCH_OS),linux)
	ifeq ($(findstring parallel=,$(DEB_BUILD_OPTIONS)),)
		export DEB_BUILD_OPTIONS+=parallel=$(shell grep -c ^processor /proc/cpuinfo)
	endif
endif
$(warning Info: DEB_BUILD_OPTIONS=$(DEB_BUILD_OPTIONS))

#: see http://lists.debian.org/debian-devel-announce/2011/09/msg00001.html
#: Also it does hardening by default, see http://wiki.debian.org/Hardening
DPKG_EXPORT_BUILDFLAGS = 1
-include /usr/share/dpkg/default.mk
#: disabling format hardening due to build problems in some plugins
export DEB_BUILD_MAINT_OPTIONS=hardening=-format

LDFLAGS+= -Wl,--as-needed

series := 2.9
plugins := aiksaurus applix babelfish bmp clarisworks collab command docbook \
	   eml epub freetranslation garble gdict gimp google grammar hancom \
	   hrtext iscii kword latex loadbindings mathview mht mif mswrite \
	   opendocument openwriter openxml opml ots paint passepartout pdb \
	   pdf presentation s5 sdw t602 urldict wikipedia wmf wml \
	   wordperfect wpg xslfo
# goffice
# plugin 'rsvg' deliberately not used due to build problems. Is it deprecated?

%:
	dh $@ --parallel --with autoreconf

override_dh_auto_clean:
	dh_auto_clean

override_dh_autoreconf:
	# make plugins configuration if building from trunk
	[ ! -x "./autogen-common.sh" ] || ./autogen-common.sh
	dh_autoreconf -v --as-needed

#: Build instructions http://www.abisource.com/~fjf/BUILD.phtml
#: configure options reference: http://www.abisource.com/wiki/Compiling_AbiWord
override_dh_auto_configure:
	# clean file only if possible to regenerate
	[ ! -x "$(which flex)" ] || $(RM) plugins/mathview/itex2mml/lex.yy.c
	# arch-dependent patching
	cat debian/patches/series.$(DEB_HOST_ARCH_OS) >> debian/patches/series \
	&& $(RM) debian/patches/series.$(DEB_HOST_ARCH_OS) \
	&& dh_quilt_patch || return 0
	dh_auto_configure -- \
		--without-gnomevfs \
		--with-gio \
		--with-redland \
		--with-libtidy \
		--enable-shared \
		--enable-clipart \
		--enable-templates \
		--enable-collab-backend-tcp \
		--enable-collab-backend-xmpp \
		--enable-collab-backend-sugar \
		--enable-collab-backend-service \
		--enable-collab-backend-telepathy \
		--enable-silent-rules \
		--enable-dependency-tracking \
		--enable-plugins="$(plugins)"
##		--with-goffice
#		--with-gtk2 \

override_dh_auto_test:
	# do not stop if test(s) failed
	dh_auto_test || return 0

override_dh_makeshlibs:
	$(RM) debian/abiword/usr/lib/abiword-$(series)/plugins/grammar.*
	$(RM) debian/abiword/usr/lib/abiword-$(series)/plugins/mathview.*
	dh_makeshlibs -V

override_dh_strip:
	dh_strip --dbg-package=abiword-dbg

get-orig-source:
	@echo " Upstream version: $(UREV)\n Version: $(VER)"
	svn checkout -r {$(UREV)} http://svn.abisource.com/abiword/trunk/ $(PACKAGE)-$(VER) \
	&& echo "Generating ChangeLog..." \
	&& cd "$(PACKAGE)-$(VER)" \
	&& svn2cl --break-before-msg --include-rev \
	&& perl -0pi -e 's{(\d+\])[^:]+?:\s+}{$$1 }sgm' ChangeLog \
	&& find . -depth -name ".svn" -exec $(RM) -r '{}' \;
	@echo "Packing..." \
	&& GZIP="-9" tar czvf "$(PACKAGE)_$(VER).orig.tar.gz" "$(PACKAGE)-$(VER)" \
	&& $(RM) -r "$(PACKAGE)-$(VER)"

