#!/usr/bin/make -f

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

pkg = $(DEB_SOURCE_PACKAGE)

ABI = $(shell dpkg-query --showformat='$${source:Version}' --show libghc-pandoc-types-dev)

# 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 += exceptions,>=,0.8,<<,0.11
ghc-deps += safe,<<,0.4
ghc-deps += zip-archive,<<,0.5
ghc-deps += http,<<,1:4000.4
ghc-deps += texmath,>=,0.12.0.1,<<,0.13
ghc-deps += xml,<<,1.4
ghc-deps += split,<<,0.3
ghc-deps += random,<<,1.2
ghc-deps += pandoc-types,>=,1.20,<<,1.21
ghc-deps += aeson,<<,1.5
ghc-deps += scientific,<<,0.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.8.3.2,<<,0.9
ghc-deps += skylighting-core,>=,0.8.3.2,<<,0.9
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 += vector,<<,0.13
ghc-deps += hslua,>=,1.0.1,<<,1.1
ghc-deps += jira-wiki-markup,>=,1.1,<<,1.2
ghc-deps += hslua-module-text,>=,0.2,<<,0.3
ghc-deps += hslua-module-system,>=,0.2,<<,0.3
ghc-deps += sha,<<,1.7
ghc-deps += haddock-library,>=,1.8,<<,1.9
ghc-deps += juicypixels,<<,3.4
ghc-deps += glob,<<,0.11
ghc-deps += cmark-gfm,>=,0.2,<<,0.3
ghc-deps += doctemplates,>=,0.8,<<,0.9
ghc-deps += network-uri,<<,2.7
ghc-deps += network,>=,2.6
ghc-deps += http-client,>=,0.4.30,,<<,0.7
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 += unicode-transforms,<<,0.4
ghc-deps += hsyaml,>=,0.2,<<,0.3
ghc-deps += doclayout,>=,0.3,<<,0.4
ghc-deps += ipynb,<<,0.2
ghc-deps += attoparsec,<<,0.14
ghc-deps += text-conversions,<<,0.4
ghc-deps += emojis,<<,0.2

# Haskell dependencies for testsuite from pandoc.cabal
ghc-deps-test += diff,<<,0.5
ghc-deps-test += tasty,>=,0.11,<<,1.3
ghc-deps-test += tasty-hunit,<<,0.11
ghc-deps-test += tasty-lua,<<,0.3
ghc-deps-test += tasty-quickcheck,<<,0.11
ghc-deps-test += tasty-golden,>=,2.3,<<,2.4
ghc-deps-test += quickcheck2,<<,2.14
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 += ansi-terminal
ghc-deps-doc += asn1-encoding
ghc-deps-doc += asn1-parse
ghc-deps-doc += asn1-types
ghc-deps-doc += async
ghc-deps-doc += base16-bytestring
ghc-deps-doc += base-compat
ghc-deps-doc += base-compat-batteries
ghc-deps-doc += basement
ghc-deps-doc += base-orphans
ghc-deps-doc += bitarray
ghc-deps-doc += blaze-builder
ghc-deps-doc += cereal
ghc-deps-doc += colour
ghc-deps-doc += connection
ghc-deps-doc += cookie
ghc-deps-doc += cryptonite
ghc-deps-doc += data-default-class
ghc-deps-doc += data-default-instances-containers
ghc-deps-doc += data-default-instances-dlist
ghc-deps-doc += data-default-instances-old-locale
ghc-deps-doc += digest
ghc-deps-doc += dlist
ghc-deps-doc += errors
ghc-deps-doc += hashable
ghc-deps-doc += hourglass
ghc-deps-doc += hs-bibutils
ghc-deps-doc += hsyaml
ghc-deps-doc += hxt
ghc-deps-doc += hxt-charproperties
ghc-deps-doc += hxt-regex-xmlschema
ghc-deps-doc += hxt-unicode
ghc-deps-doc += integer-logarithms
ghc-deps-doc += memory
ghc-deps-doc += mime-types
ghc-deps-doc += old-locale
ghc-deps-doc += pcre-light
ghc-deps-doc += pem
ghc-deps-doc += primitive
ghc-deps-doc += quickcheck2
ghc-deps-doc += regex-base
ghc-deps-doc += regex-pcre
ghc-deps-doc += rts
ghc-deps-doc += socks
ghc-deps-doc += splitmix
ghc-deps-doc += streaming-commons
ghc-deps-doc += tagged
ghc-deps-doc += th-abstraction
ghc-deps-doc += time-compat
ghc-deps-doc += tls
ghc-deps-doc += transformers
ghc-deps-doc += transformers-compat
ghc-deps-doc += unicode-transforms
ghc-deps-doc += utf8-string
ghc-deps-doc += uuid-types
ghc-deps-doc += x509
ghc-deps-doc += x509-store
ghc-deps-doc += x509-system
ghc-deps-doc += x509-validation

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

# 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 arm64 hppa mips mipsel riscv64))
DEB_SETUP_GHC_CONFIGURE_ARGS += --ghc-options="-optc--param -optcggc-min-expand=10 -O0"
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

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