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

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
# arch-dependent patching
archpatch=$(wildcard debian/patches/series.$(DEB_HOST_ARCH_OS))
$(info $(if $(archpatch), $(shell cat $(archpatch) >> debian/patches/series \
&& $(RM) $(archpatch) && dh_quilt_patch)))

# parallel build by default on linux
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 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?

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

%:
	dh $@ --parallel --with autoreconf
#	dh $@ --parallel --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 -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:
	# 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)"
#		--with-gtk2 \
#		--enable-debug \
#		--enable-introspection \

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.*
	$(RM) -v debian/abiword/usr/lib/$(DEB_HOST_MULTIARCH)/abiword-*/plugins/mathview.*
	dh_makeshlibs -V

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

override_dh_builddeb:
	dh_builddeb -- -Zxz

.PHONY: get-orig-source
VER ?= $(shell dpkg-parsechangelog -l$(PKD)/changelog | perl -ne 'print $$1 if m/Version:\s*([\d\.~+svn]+)/')
get-orig-source: $(PKG)-$(VER)/ChangeLog
	@echo "Packing..."
	XZ_OPT="-6v" tar -caf "$(PKG)_$(VER).orig.tar.xz" "$(PKG)-$(VER)" \
        && $(RM) -r "$(PKG)-$(VER)"

$(PKG)-$(VER)/ChangeLog: $(PKG)-$(VER)
	@echo "Generating ChangeLog..."
	cd "$(PKG)-$(VER)" \
        && svn2cl --break-before-msg --include-rev \
        && perl -0pi -e 's{(\d+\])[^:]+?:\s+}{$$1 }sgm' ChangeLog \
        && find . -depth -name ".svn" -exec $(RM) -rf '{}' \;

$(PKG)-$(VER):
	svn checkout -r {$$(echo $(VER) | perl -ne 'print $$1 if m/\Dsvn(\d+)/')} \
            http://svn.abisource.com/abiword/branches/ABI-3-0-0-STABLE $(PKG)-$(VER) \
        || $(RM) -r $(PKG)-$(VER)
