#!/usr/bin/make -f

# Copyright © 2006-2010 John MacFarlane <jgm@berkeley.edu>
# Copyright © 2007-2008 Recai Oktaş <roktas@debian.org>
# Copyright © 2008-2016 Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for Pandoc
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

include /usr/share/cdbs/1/rules/debhelper.mk
-include /usr/share/cdbs/1/class/hlibrary.mk

pkg = $(DEB_SOURCE_PACKAGE)

# helper macros for GHC dependencies, e.g.: foo bar,>=,1.2,<<,1.3 baz
expand-ghc-dep = $(comma) libghc-$2-$1 $(if $3,($3 $4)$(if $5,$(comma) libghc-$2-$1 ($5 $6)))
strip-ghc-deps = $(foreach dep,$2,$(call expand-ghc-dep,$1,$(word 1,$(subst $(comma),$(space),$(dep)))))
expand-ghc-deps = $(foreach dep,$2,$(call expand-ghc-dep,$1,$(word 1,$(subst $(comma),$(space),$(dep))),$(word 2,$(subst $(comma),$(space),$(dep))),$(word 3,$(subst $(comma),$(space),$(dep))),$(word 4,$(subst $(comma),$(space),$(dep))),$(word 5,$(subst $(comma),$(space),$(dep)))))

# Haskell dependencies for any build from pandoc.cabal
ghc-deps += unordered-containers,<<,3
ghc-deps += parsec3,<<,3.2
ghc-deps += mtl,>=,2.2,<<,2.3
ghc-deps += network,>=,2.6
ghc-deps += network-uri,<<,2.7
ghc-deps += text,<<,1.3
ghc-deps += zip-archive,<<,0.4
ghc-deps += http,<<,1:4000.4
ghc-deps += texmath,>=,0.9,<<,0.10
ghc-deps += xml,<<,1.4
ghc-deps += random,<<1.2
ghc-deps += extensible-exceptions,<<,0.2
ghc-deps += pandoc-types,>=,1.17,<<,1.17.1
ghc-deps += aeson,>=,0.7.0.5,<<,1.3
ghc-deps += tagsoup,<<,0.15
ghc-deps += base64-bytestring,<<,1.1
ghc-deps += zlib,<<,0.7
ghc-deps += skylighting,>=,0.3.3,<<,0.4
ghc-deps += data-default,<<,0.8
ghc-deps += temporary,<<,1.3
ghc-deps += blaze-html,<<,0.10
ghc-deps += blaze-markup,<<,0.9
ghc-deps += yaml,<<,0.9
ghc-deps += scientific,<<,0.4
ghc-deps += vector,<<,0.13
ghc-deps += hslua,<<,0.5
ghc-deps += sha,<<,1.7
ghc-deps += haddock-library,<<,1.5
ghc-deps += juicypixels,<<,3.3
ghc-deps += filemanip,<<,0.4
ghc-deps += cmark,<<,0.6
ghc-deps += doctemplates,<<,0.2
ghc-deps += http-client,<<,0.6
ghc-deps += http-client-tls,<<,0.4
ghc-deps += http-types,<<,0.10
ghc-deps += syb,<<,0.8

# Haskell dependencies for testsuite from pandoc.cabal
ghc-deps-test += diff,<<,0.4
ghc-deps-test += test-framework,<<,0.9
ghc-deps-test += test-framework-hunit,<<,0.4
ghc-deps-test += test-framework-quickcheck2,<<,0.4
ghc-deps-test += quickcheck2,<<,2.11
ghc-deps-test += hunit,<<,1.7
ghc-deps-test += ansi-terminal,<<,0.8

# Haskell dependencies (maybe) needed for documentation
# TODO: actually use these (not maintain Build-depends-indep by hand)
ghc-deps-doc += blaze-builder
ghc-deps-doc += digest
ghc-deps-doc += dlist
ghc-deps-doc += hs-bibutils
ghc-deps-doc += pcre-light
ghc-deps-doc += rts
ghc-deps-doc += transformers
ghc-deps-doc += utf8-string

# Needed by upstream build process
CDBS_BUILD_DEPENDS +=, ghc, alex, happy
CDBS_BUILD_DEPENDS += $(call expand-ghc-deps,dev,$(ghc-deps))
CDBS_BUILD_DEPENDS += $(call expand-ghc-deps,dev,$(ghc-deps-test))
CDBS_BUILD_DEPENDS +=, ghc-prof
CDBS_BUILD_DEPENDS +=, $(call strip-ghc-deps,prof,$(ghc-deps))
CDBS_BUILD_DEPENDS_INDEP +=, ghc-doc
CDBS_BUILD_DEPENDS_INDEP +=, $(call strip-ghc-deps,doc,$(ghc-deps) $(ghc-deps-doc))

# Needed for our packaging
#  * recent haskell-devscripts needed for version-less datadir
CDBS_BUILD_DEPENDS +=, haskell-devscripts

# Needed (always/often/seldom) at runtime
#  texlive-* needed for PDF 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, wkhtmltopdf

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

# 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"

ifneq (,$(filter $(DEB_BUILD_ARCH),mips mipsel))
    DEB_SETUP_GHC_CONFIGURE_ARGS += --ghc-options="-optc--param -optcggc-min-expand=10"
endif

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

DEB_INSTALL_DOCS_ALL += README.md

DEB_INSTALL_MANPAGES_$(pkg) = man/pandoc.1

CDBS_BUILD_DEPENDS +=, bash-completion
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
