#!/usr/bin/make -f

# resolve DEB_VERSION_UPSTREAM
include /usr/share/dpkg/pkg-info.mk

# TODO: simplify rules when haskell-devscripts supports DEB_SRCDIR
#DEB_SRCDIR = pandoc-cli

DEB_CABAL_FILE = pandoc-cli/pandoc-cli.cabal
DEB_ENABLE_TESTS = yes
DEB_CABAL_PACKAGE = pandoc-cli

include /usr/share/cdbs/1/rules/debhelper.mk

# use forked Haskell build helper script to support DEB_CABAL_FILE
-include debian/hlibrary.mk

pkg = $(DEB_SOURCE_PACKAGE)

ABI = $(shell dpkg-query --showformat='$${source:Version}' --show libghc-pandoc-types-dev)
LIB = $(firstword $(subst +, ,$(DEB_VERSION_UPSTREAM)))

# Needed (always/often/seldom) at runtime
#  texlive-* needed for PDF output
#  librsvg2-bin (rsvg-convert) needed for SVG in PDF output
#  groff needed for groff man and groff ms output
# TODO: suggest slide show Javascript libraries when in Debian
CDBS_SUGGESTS_$(pkg) = texlive-latex-recommended, texlive-xetex, texlive-luatex
CDBS_SUGGESTS_$(pkg) +=, pandoc-citeproc, texlive-latex-extra, context
CDBS_SUGGESTS_$(pkg) +=, wkhtmltopdf, librsvg2-bin, groff, ghc, nodejs
CDBS_SUGGESTS_$(pkg) +=, php, perl, python, ruby, r-base-core
CDBS_SUGGESTS_$(pkg) +=, libjs-mathjax, libjs-katex
CDBS_SUGGESTS_$(pkg) +=, citation-style-language-styles

# Fix exception thrown building documentation:
# haddock: internal error: <stdout>: commitBuffer: invalid argument (invalid character)
export LANG=C.UTF-8

# Omit Lua scripting engine on unsupported architectures
ifneq (,$(filter $(DEB_BUILD_ARCH),ppc64 ppc64el))
DEB_SETUP_GHC_CONFIGURE_ARGS += --flags=-lua
else
DEB_SETUP_GHC_CONFIGURE_ARGS += --flags=+lua
endif

# Use threaded RTS only when supported
DEB_SETUP_GHC_CONFIGURE_ARGS += $(if $(wildcard /usr/lib/ghc-$(GHC_VERSION)/libHSrts_thr.a),,--flags=-threaded)

# Disable timer to help build on slow arches like hppa
DEB_SETUP_GHC_CONFIGURE_ARGS += --ghc-options="+RTS -V0 -RTS"

# Reduce compile-time memory utilization on low-memory architectures
ifneq (,$(filter $(DEB_BUILD_ARCH),armel armhf hppa mips mipsel))
DEB_SETUP_GHC_CONFIGURE_ARGS += --ghc-options="-optc--param -optcggc-min-expand=10 -O0"
endif

ifneq (,$(filter $(DEB_HOST_ARCH_CPU), i386))
DEB_SETUP_GHC_CONFIGURE_ARGS += --ghc-options="-O0"
endif

DEB_SETUP_GHC_CONFIGURE_ARGS += $(if $(filter nocheck,$(DEB_BUILD_OPTIONS)),,-ftests)

DEB_INSTALL_DOCS_ALL += README.md

build/$(pkg):: build-ghc-stamp

post-install/$(pkg)::
	debian/tmp-inst-*/usr/bin/pandoc --bash-completion \
		> debian/pandoc.bash-completion
binary-install/$(pkg)::
	dh_bash-completion
clean::
	rm -f debian/pandoc.bash-completion

# populate project at root where haskell-devscripts can use it
SRC_DATA = $(filter-out COPYING.md,\
 $(notdir $(wildcard pandoc-cli/*)))
pre-build::
	rm -rf $(SRC_DATA)
	cp --archive --target-directory=. $(addprefix pandoc-cli/,$(SRC_DATA))
clean::
	rm -rf $(SRC_DATA)

DEB_DH_GENCONTROL_ARGS_$(pkg) = -- \
 -V"pandoc:Abi=$(ABI)" \
 -V"pandoc:Lib=$(LIB)"

# generate man page only when built with Lua supprt
ifeq (,$(filter $(DEB_BUILD_ARCH),ppc64 ppc64el))
DEB_INSTALL_MANPAGES_$(pkg) = man/pandoc.1
build/$(pkg):: man/pandoc.1
man/pandoc.1: MANUAL.txt man/pandoc.1.before man/pandoc.1.after
	dist-ghc/build/pandoc/pandoc $< -f markdown -t man -s \
		--lua-filter man/manfilter.lua \
		--include-before-body man/pandoc.1.before \
		--include-after-body man/pandoc.1.after \
		--metadata author="" \
		--variable footer="pandoc $(LIB)" \
		-o $@
clean::
	rm -f man/pandoc.1
endif
