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

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

# parallel build by default on linux
# avoid parallel build maybe incompatible with gir (see commit b1c5e09)
#ifeq ($(DEB_HOST_ARCH_OS),linux)
#    ifeq ($(findstring parallel=,$(DEB_BUILD_OPTIONS)),)
#        export DEB_BUILD_OPTIONS+=parallel=$(shell getconf _NPROCESSORS_ONLN)
#    endif
#endif
#$(info DEB_BUILD_OPTIONS:$(origin DEB_BUILD_OPTIONS)=$(DEB_BUILD_OPTIONS))

#: Disabling format hardening (-format) might help for error:
#: "format not a string literal and "no format arguments [-Werror=format-security]"
export DEB_BUILD_MAINT_OPTIONS=hardening=+bindnow
$(info DEB_BUILD_MAINT_OPTIONS:$(origin DEB_BUILD_MAINT_OPTIONS)=$(DEB_BUILD_MAINT_OPTIONS))

# minimise needless linking
export DEB_LDFLAGS_MAINT_APPEND= -Wl,--as-needed

plugins := aiksaurus applix babelfish bmp clarisworks collab command docbook \
	   eml epub freetranslation garble gdict gimp google grammar hancom \
	   hrtext iscii kword latex loadbindings 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?
# plugin 'mathview' deliberately not used due to bug#785485

PKD  = $(abspath $(dir $(MAKEFILE_LIST)))
PKG  = $(word 2,$(shell dpkg-parsechangelog -l$(PKD)/changelog | grep ^Source))
ifneq (,$(wildcard configure.*))
    # API version
    apiver:=$(shell perl -0ne 'print "$$1.$$2" if m{\[abi_version_major\],\s+\[(\d+)\].*?\[abi_version_minor\],\s+\[(\d+)\]}s' configure.*)
endif
# The base library name
plib=lib$(PKG)-$(apiver)
pdev=lib$(PKG)-$(apiver)-dev

%:
# avoid parallel build maybe incompatible with gir (see commit b1c5e09)
#	dh $@ --parallel --with autoreconf
	dh $@ --with autoreconf,gir

override_dh_clean:
	## adaptive cleaning meant to work for source archive or repository checkout.
	## clean files only if possible to regenerate
	[ ! -x "$(which flex)" ] || $(RM) -v plugins/mathview/itex2mml/lex.yy.c
	[ ! -x "tools/cdump/xp/cdump.pl" ] || $(RM) -v src/wp/ap/xp/ap_wp_sidebar.cpp
	[ ! -x "./autogen-common.sh" ] || $(RM) -v plugin*.m4 po/*.strings
	dh_clean

override_dh_autoreconf:
	$(info I: library-name=$(plib))
	$(if $(wildcard debian/$(plib).install),,$(error invalid package/library name.))
	$(if $(wildcard debian/$(pdev).install),,$(error invalid dev package/library name.))
	# make plugins configuration if building from trunk
	[ ! -x "./autogen-common.sh" ] || ./autogen-common.sh
	dh_autoreconf --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:
	# generate icon
	convert abiword.png -resize 32x32 debian/abiword.xpm
	dh_auto_configure -- \
		--without-gnomevfs \
		--with-gio \
		--with-redland \
		--with-libtidy \
		--with-goffice \
		--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 \
		--disable-silent-rules \
		--enable-dependency-tracking \
		--enable-plugins="$(plugins)" \
		--enable-introspection
#		--with-gtk2 \
#		--enable-debug \

override_dh_auto_test:
	# do not stop on failed test(s)
	-dh_auto_test

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

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