#!/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/utils.mk
include /usr/share/cdbs/1/rules/debhelper.mk
-include /usr/share/cdbs/1/class/hlibrary.mk

pkg = $(DEB_SOURCE_PACKAGE)
pkg-data = $(pkg)-data

# ABI by default follows upstream version (without repackaging suffix)
abi = $(DEB_UPSTREAM_TARBALL_VERSION)

DEB_COPYRIGHT_EXTRACT_EXTS = png jpg jpeg gif ttf otf
DEB_COPYRIGHT_CHECK_IGNORE_EXTS = docx odt epub
DEB_COPYRIGHT_CHECK_MERGE_SAME_LICENSE = yes

DEB_INSTALL_MANPAGES_$(pkg) = man/pandoc.1

# 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_DH_INSTALL_ARGS_pandoc-data = debian/tmp-inst-*/usr/share/pandoc/data /usr/share/pandoc/

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

# Haskell dependencies for testsuite from pandoc.cabal
deps-ghc-test += diff,>=,0.2,<<,0.3
deps-ghc-test += test-framework,<<,0.9
deps-ghc-test += test-framework-hunit,<<,0.4
deps-ghc-test += test-framework-quickcheck2,<<,0.4
deps-ghc-test += quickcheck2,<<,2.10
deps-ghc-test += hunit,<<,1.4
deps-ghc-test += template-haskell,<<,2.9
deps-ghc-test += ansi-terminal,<<,0.7

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

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)))))

# Needed by upstream build process
CDBS_BUILD_DEPENDS +=, ghc, alex, happy
CDBS_BUILD_DEPENDS += $(call expand-ghc-deps,dev,$(deps-ghc))
# FIXME: enable, set DEB_ENABLE_TESTS = yes, and configure with -ftests
#CDBS_BUILD_DEPENDS += $(call expand-ghc-deps,dev,$(deps-ghc-test))
CDBS_BUILD_DEPENDS +=, ghc-prof
CDBS_BUILD_DEPENDS +=, $(call strip-ghc-deps,prof,$(deps-ghc))
CDBS_BUILD_DEPENDS_INDEP +=, ghc-doc
CDBS_BUILD_DEPENDS_INDEP +=, $(call strip-ghc-deps,doc,$(deps-ghc) $(deps-ghc-doc))

# Needed for our packaging
#  * recent haskell-devscripts needed for version-less datadir
CDBS_BUILD_DEPENDS +=, haskell-devscripts (>= 0.8.19~)

# Needed (always/often/seldom) at runtime
#  texlive-* needed for PDF output
#  recent texlive-latex-extra needed for PDF output of data with YAML metadata
# 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 (>= 2015.20150625-1), wkhtmltopdf

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