#!/usr/bin/make -f

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 = http://pandoc.googlecode.com/files
DEB_UPSTREAM_TARBALL_MD5 = f5eb2d711cd65f2842487a6599de59b7

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

DEB_SETUP_GHC6_CONFIGURE_ARGS = -fhighlighting

# Use threaded RTS only when supported
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
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/markdown2pdf.1 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

# Generate (and cleanup) files containing variables static per build
infiles = $(filter-out debian/control.in, $(wildcard debian/*.in))
outfiles = $(basename $(infiles))
DEB_PHONY_RULES += $(outfiles)
pre-build:: $(outfiles)
$(outfiles): %: %.in
	perl -pe 's/__ABI__/$(abi)/g' <$< >$@
clean::
	rm -f $(outfiles)

# Let Haskell cleanup after itself (apparently forgotten in hlibrary.mk)
DEB_PHONY_RULES += haskell-clean
cleanbuilddir:: haskell-clean
haskell-clean:
	[ ! -x "$(DEB_SETUP_BIN_NAME)" ] || $(DEB_SETUP_BIN_NAME) clean

deps-doc = ghc-doc
deps-stems = syb parsec3 xhtml mtl network zip-archive utf8-string http texmath xml citeproc-hs
deps-stems += pandoc-types json dlist tagsoup highlighting-kate diff testpack
deps-stems += pcre-light
deps-doc += , $(patsubst %, libghc-%-doc, $(deps-doc-stems))

# Needed by upstream build process
CDBS_BUILD_DEPENDS += , ghc, libghc-syb-dev
CDBS_BUILD_DEPENDS += , libghc-parsec3-dev (<< 3.2)
CDBS_BUILD_DEPENDS += , libghc-xhtml-dev (<< 3000.3)
CDBS_BUILD_DEPENDS += , libghc-mtl-dev (<< 2.1)
CDBS_BUILD_DEPENDS += , libghc-network-dev (<< 2.4)
CDBS_BUILD_DEPENDS += , libghc-zip-archive-dev (<< 0.2)
CDBS_BUILD_DEPENDS += , libghc-utf8-string-dev
CDBS_BUILD_DEPENDS += , libghc-http-dev (<< 1:4000.2)
CDBS_BUILD_DEPENDS += , libghc-texmath-dev (<< 0.6)
CDBS_BUILD_DEPENDS += , libghc-xml-dev (<< 1.4)
CDBS_BUILD_DEPENDS += , libghc-citeproc-hs-dev (<< 0.4)
CDBS_BUILD_DEPENDS += , libghc-pandoc-types-dev (<< 1.9)
CDBS_BUILD_DEPENDS += , libghc-json-dev (<< 0.5)
CDBS_BUILD_DEPENDS += , libghc-dlist-dev (<< 0.6)
CDBS_BUILD_DEPENDS += , libghc-tagsoup-dev (<< 0.13)
CDBS_BUILD_DEPENDS += , libghc-highlighting-kate-dev (<< 0.3)
CDBS_BUILD_DEPENDS += , libghc-diff-dev, libghc-testpack-dev
CDBS_BUILD_DEPENDS += , hscolour (>= 1.8)
CDBS_BUILD_DEPENDS += , $(deps-doc)

# Needed for our packaging
CDBS_BUILD_DEPENDS += , haskell-devscripts (>=0.5.12)

# Needed (always/often/seldom) at runtime
#  texlive-* needed for markdown2pdf script
#  (texlive-latex-recommended mostly ok except ucs.sty - see bug#571678)
CDBS_SUGGESTS_pandoc = texlive-latex-recommended, texlive-latex-extra, texlive-xetex
CDBS_SUGGESTS_libghc-pandoc-doc = $(deps-doc)
