#!/usr/bin/make -f

# Copyright © 2006-2010 John MacFarlane <jgm@berkeley.edu>
# Copyright © 2007-2008 Recai Oktaş <roktas@debian.org>
# Copyright © 2008-2018 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,dev,prof doc,>=,1.2,<<,1.3 baz
expand-or-strip-ghc-dep = $(foreach expandsuffix,$1,\
 $(comma) libghc-$3-$(expandsuffix) $(if $4,($4 $5))\
 $(if $6,\
  $(comma) libghc-$3-$(expandsuffix) ($6 $7)))\
 $(foreach stripsuffix,$2,\
  $(comma) libghc-$3-$(stripsuffix))
expand-ghc-deps = $(foreach dep,$(sort $3),$(call expand-or-strip-ghc-dep,\
 $1,\
 $2,$(strip \
 $(word 1,$(subst $(comma),$(space),$(dep)))),$(strip \
 $(word 2,$(subst $(comma),$(space),$(dep)))),$(strip \
 $(word 3,$(subst $(comma),$(space),$(dep)))),$(strip \
 $(word 4,$(subst $(comma),$(space),$(dep)))),$(strip \
 $(word 5,$(subst $(comma),$(space),$(dep))))))

# Haskell dependencies for any build from pandoc.cabal
ghc-deps += syb,<<,0.8
ghc-deps += unordered-containers,<<,0.3
ghc-deps += parsec3,<<,3.2
ghc-deps += mtl,>=,2.2,<<,2.3
ghc-deps += exceptions,>=,0.8,<<,0.11
ghc-deps += text,<<,1.3
ghc-deps += safe,<<,0.4
ghc-deps += zip-archive,<<,0.4
ghc-deps += http,<<,1:4000.4
ghc-deps += texmath,>=,0.10,<<,0.12
ghc-deps += xml,<<,1.4
ghc-deps += split,<<,0.3
ghc-deps += random,<<,1.2
ghc-deps += pandoc-types,>=,1.17.4.2,<<,1.18
ghc-deps += aeson,<<,1.4
ghc-deps += aeson-pretty,>=,0.8.5,<<,0.9
ghc-deps += tagsoup,>=,0.14.6,<<,0.15
ghc-deps += base64-bytestring,<<,1.1
ghc-deps += zlib,<<,0.7
ghc-deps += skylighting,>=,0.5.1,<<,0.8
ghc-deps += data-default,<<,0.8
ghc-deps += temporary,<<,1.4
ghc-deps += blaze-html,>=,0.9,<<,0.10
ghc-deps += blaze-markup,>=,0.8,<<,0.9
ghc-deps += yaml,<<,0.9
ghc-deps += scientific,<<,0.4
ghc-deps += vector,<<,0.13
ghc-deps += hslua,>=,0.9.5,<<,0.9.6
ghc-deps += hslua-module-text,<<,0.2
ghc-deps += sha,<<,1.7
ghc-deps += haddock-library,<<,1.6
ghc-deps += juicypixels,<<,3.3
ghc-deps += glob,<<,0.10
ghc-deps += cmark-gfm,<<,0.2
ghc-deps += doctemplates,>=,0.2.1,<<,0.3
ghc-deps += http-client,>=,0.4.30,,<<,0.6
ghc-deps += http-client-tls,>=,0.2.4,<<,0.4
ghc-deps += http-types,<<,0.13
ghc-deps += case-insensitive,>=,1.2,<<,1.3
ghc-deps += network,>=,2.6
ghc-deps += network-uri,<<,2.7

# Haskell dependencies for testsuite from pandoc.cabal
ghc-deps-test += diff,<<,0.4
ghc-deps-test += tasty,>=,0.11,<<,1.2
ghc-deps-test += tasty-hunit,<<,0.11
ghc-deps-test += tasty-quickcheck,<<,0.11
ghc-deps-test += tasty-golden,>=,2.3,<<,2.4
ghc-deps-test += quickcheck2,<<,2.12
ghc-deps-test += executable-path,<<,0.1

# 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, ghc-prof, alex, happy
CDBS_BUILD_DEPENDS += $(call expand-ghc-deps,dev,prof,$(ghc-deps))
CDBS_BUILD_DEPENDS += $(call expand-ghc-deps,dev,,$(ghc-deps-test))
CDBS_BUILD_DEPENDS_INDEP +=, ghc-doc
CDBS_BUILD_DEPENDS_INDEP +=, $(call expand-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
#  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, node-katex

# 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
