# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: Arch Haskell Team <arch-haskell@haskell.org>

pkgname=pandoc
pkgver=2.14.0.2
pkgrel=2
pkgdesc='Conversion between markup formats'
url='https://pandoc.org'
license=('GPL')
arch=('x86_64')
depends=('ghc-libs' 'haskell-http' 'haskell-juicypixels' 'haskell-sha' 'haskell-aeson'
         'haskell-aeson-pretty' 'haskell-attoparsec' 'haskell-base64-bytestring'
         'haskell-blaze-html' 'haskell-blaze-markup' 'haskell-case-insensitive'
         'haskell-citeproc' 'haskell-commonmark' 'haskell-commonmark-extensions'
         'haskell-commonmark-pandoc' 'haskell-connection' 'haskell-data-default'
         'haskell-doclayout' 'haskell-doctemplates' 'haskell-emojis' 'haskell-file-embed'
         'haskell-glob' 'haskell-haddock-library' 'haskell-ipynb' 'haskell-jira-wiki-markup'
         'haskell-skylighting' 'haskell-skylighting-core' 'haskell-hslua'
         'haskell-hslua-module-path' 'haskell-hslua-module-system' 'haskell-hslua-module-text'
         'haskell-http-client' 'haskell-syb' 'haskell-hsyaml' 'haskell-http-client-tls'
         'haskell-http-types' 'haskell-safe' 'haskell-split' 'haskell-texmath' 'haskell-network'
         'haskell-pandoc-types' 'haskell-random' 'haskell-scientific' 'haskell-tagsoup'
         'haskell-temporary' 'haskell-text-conversions' 'haskell-network-uri'
         'haskell-unicode-collation' 'haskell-unicode-transforms' 'haskell-unordered-containers'
         'haskell-zip-archive' 'haskell-xml' 'haskell-xml-conduit' 'haskell-zlib')
optdepends=('pandoc-crossref: for numbering figures, equations, tables and cross-references to them with pandoc-crossref filter'
            'texlive-core: for pdf output')
conflicts=('haskell-pandoc')
replaces=('haskell-pandoc')
makedepends=('ghc' 'uusi' 'haskell-diff' 'haskell-tasty' 'haskell-tasty-hunit' 'haskell-tasty-lua'
             'haskell-tasty-quickcheck' 'haskell-tasty-golden' 'haskell-quickcheck')
source=("https://hackage.haskell.org/packages/archive/${pkgname}/${pkgver}/${pkgname}-${pkgver}.tar.gz")
sha512sums=('95904a7badae751a12fef407192edd341527575a7f53183e01c49d11ea033ff81ddb48f2fc35de580aca9b74f82cebd2836333d6fdbdaa893da8717fd23bf9bd')

prepare() {
    cd $pkgname-$pkgver

    # TODO: find a better solution
    sed -i "s|let env' = dynlibEnv ++ |let env' = dynlibEnv ++ [(\"LD_LIBRARY_PATH\", \"$PWD/dist/build\")] ++ |" test/Tests/Command.hs
}

build() {
    cd $pkgname-$pkgver

    runhaskell Setup configure -O --enable-shared --enable-executable-dynamic --disable-library-vanilla \
        --prefix=/usr --docdir="/usr/share/doc/${pkgbase}" --datasubdir="$pkgname" --enable-tests \
        --dynlibdir=/usr/lib --libsubdir=\$compiler/site-local/\$pkgid \
            -f-trypandoc -f-embed_data_files -f-static
    runhaskell Setup build $MAKEFLAGS
    runhaskell Setup register --gen-script
    runhaskell Setup unregister --gen-script
    sed -i -r -e "s|ghc-pkg.*update[^ ]* |&'--force' |" register.sh
    sed -i -r -e "s|ghc-pkg.*unregister[^ ]* |&'--force' |" unregister.sh
}

check() {
    cd $pkgname-$pkgver
    LC_CTYPE=en_US.UTF-8 runhaskell Setup test || echo "Tests failed"
}

package() {
    cd $pkgname-$pkgver
    install -D -m744 register.sh   "${pkgdir}/usr/share/haskell/register/${pkgname}.sh"
    install -D -m744 unregister.sh "${pkgdir}/usr/share/haskell/unregister/${pkgname}.sh"
    runhaskell Setup copy --destdir="${pkgdir}"
    rm "${pkgdir}/usr/share/doc/${pkgname}/COPYING.md"
    install -Dm644 man/pandoc.1 -t "${pkgdir}"/usr/share/man/man1/
}
