#!/usr/bin/make -f

# Copyright © 2006-2010 John MacFarlane <jgm@berkeley.edu>
# Copyright © 2007-2008 Recai Oktaş <roktas@debian.org>
# Copyright © 2008-2014 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/upstream-tarball.mk
include /usr/share/cdbs/1/rules/utils.mk
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/hlibrary.mk

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

DEB_UPSTREAM_URL = https://hackage.haskell.org/package/$(DEB_UPSTREAM_PACKAGE)-$(DEB_UPSTREAM_TARBALL_VERSION)

DEB_UPSTREAM_REPACKAGE_EXCLUDES = data/LaTeXMathML.js tests/s5.fancy.html

DEB_COPYRIGHT_CHECK_IGNORE_REGEX = ^.*\.(gif|jpg|odt)|debian/(changelog|copyright(|_hints|_newhints))$

DEB_INSTALL_MANPAGES_pandoc = man/man1/*.1 man/man5/*.5 debian/hsmarkdown.1

# Use threaded RTS only when supported
# TODO: use _GHC_ after jessie: supported since haskell-devscripts 0.8.5
DEB_SETUP_GHC6_CONFIGURE_ARGS += $(if $(wildcard /usr/lib/ghc-$(GHC6_VERSION)/libHSrts_thr.a),,--flags=-threaded)

# Disable timer to help build on slow arches like hppa
# TODO: use _GHC_ after jessie: supported since haskell-devscripts 0.8.5
DEB_SETUP_GHC6_CONFIGURE_ARGS += --ghc-options="+RTS -V0 -RTS"

# put aside upstream-shipped temp files during build but after copyright-check
upstreamtmpfiles = man/man1/pandoc.1 man/man5/pandoc_markdown.5
pre-build:: debian/stamp-upstreamtmpstuff
debian/stamp-upstreamtmpstuff: debian/stamp-copyright-check
	for file in $(upstreamtmpfiles); do \
		[ ! -e $$file ] || [ -e $$file.upstream ] || cp $$file $$file.upstream; \
	done
	touch $@
clean::
	for file in $(upstreamtmpfiles); do \
		[ ! -e $$file.upstream ] || mv -f $$file.upstream $$file; \
	done
	rm -f debian/stamp-upstreamtmpstuff

# install data to varying path, and add symlink when datadir versioned
# TODO: simplify after jessie+1: unversioned since haskell-devscripts 0.8.19
datadir := $(if $(shell grep datasubdir /usr/share/cdbs/1/class/hlibrary.mk),pandoc,pandoc-$(abi))
DEB_DH_INSTALL_ARGS_pandoc-data = debian/tmp-inst-*/usr/share/$(datadir)/data /usr/share/$(datadir)/
DEB_DH_LINK_pandoc-data = $(if $(call cdbs_streq,$(datadir),pandoc),,/usr/share/$(datadir) /usr/share/pandoc)

# Haskell dependencies for any build from pandoc.cabal
#  * Versions only relevant post Squeeze when libhc-* namespace was introduced
deps-ghc += unordered-containers,<<,3
deps-ghc += parsec3,<<,3.2
deps-ghc += mtl,<<,2.3
deps-ghc += network,<<,2.6
deps-ghc += text,>=,0.11,<<,1.2
deps-ghc += zip-archive,>=,0.1.3.3,<<,0.3
deps-ghc += http,<<,1:4000.3
deps-ghc += texmath,>=,0.6.6.1,<<,0.7
deps-ghc += xml,<<,1.4
deps-ghc += random,<<1.1
deps-ghc += extensible-exceptions,<<,0.2
deps-ghc += pandoc-types,>=,1.12.3.3,<<,1.13
deps-ghc += aeson,>=,0.7,<<,0.8
deps-ghc += tagsoup,>=,0.13.1,<<,0.14
deps-ghc += base64-bytestring,<<,1.1
deps-ghc += zlib,<<,0.6
deps-ghc += highlighting-kate,>=,0.5.8,<<,0.6
deps-ghc += data-default,>=,0.4,<<,0.6
deps-ghc += temporary,<<,1.3
deps-ghc += blaze-html,>=,0.5,<<,0.8
deps-ghc += blaze-markup,<<,0.8
deps-ghc += attoparsec,<<,0.12
deps-ghc += yaml,>=,0.8.8.2,<<,0.9
deps-ghc += scientific,>=,0.2,<<,0.3
deps-ghc += hslua,<<,0.4
deps-ghc += http-conduit,>=,1.9,<<,2.2
deps-ghc += http-types,>=,0.8,<<,0.9
deps-ghc += text,>=,0.11,<<,0.12
# Should be safe to drop versioning for Squeeze
deps-ghc += syb,<<,0.5

# 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.4,<<,2.8
deps-ghc-test += hunit,<<,1.3
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
CDBS_BUILD_DEPENDS += , haskell-devscripts

# Needed (always/often/seldom) at runtime
#  texlive-* needed for PDF output
#  etoolbox needed for PDF output of data with YAML metadata
# TODO: recommend pandoc-citeproc (and add to description) when in Debian
# TODO: suggest slide show Javascript libraries when in Debian
CDBS_DEPENDS_pandoc = pandoc-data
CDBS_SUGGESTS_pandoc = texlive-latex-recommended, texlive-xetex, texlive-luatex
CDBS_SUGGESTS_pandoc +=, pandoc-citeproc, etoolbox
