# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Maintainer: Caleb Maclennan <caleb@alerque.com>
# Contributor: Arch Haskell Team <arch-haskell@haskell.org>

pkgname=pandoc-crossref
pkgver=0.3.20
pkgrel=6
pkgdesc="Pandoc filter for cross-references"
url="https://hackage.haskell.org/package/${pkgname}"
license=("GPL2")
arch=('x86_64')
depends=('ghc-libs' 'haskell-data-default' 'haskell-gitrev' 'haskell-microlens'
         'haskell-microlens-ghc' 'haskell-microlens-mtl' 'haskell-microlens-th' 'haskell-open-browser'
         'haskell-optparse-applicative' 'haskell-pandoc' 'haskell-pandoc-types' 'haskell-syb'
         'haskell-utility-ht')
makedepends=('ghc' 'haskell-hspec' 'pandoc-cli' 'uusi')
conflicts=('haskell-pandoc-crossref')
replaces=('haskell-pandoc-crossref')
source=("https://hackage.haskell.org/packages/archive/${pkgname}/${pkgver}/${pkgname}-${pkgver}.tar.gz"
        pandoc-3.8-build-fix.patch)
sha512sums=('533371ff2b813cf46d92e9fb33ccddc5eae422fdc2fc986f75066b2f648af40da232a287a81c814bf04b1eef8724771ebd4cef1562711e9c6fb5c09e07bbc3ae'
            'ed39079c53be3063d26f20bc55895d92f454c82f4ba7b36fe4b484f0b91260464d697cb7a0e314376ad9470cf5a20268299106390bdc4b0155d455ec237cf309')

prepare() {
  cd $pkgname-$pkgver
  uusi -u pandoc
  patch -Np1 -i ../pandoc-3.8-build-fix.patch
}

build() {
  cd $pkgname-$pkgver
  runhaskell Setup configure -O --enable-shared --enable-debug-info --enable-executable-dynamic --disable-library-vanilla \
    --prefix=/usr --docdir=/usr/share/doc/$pkgname --datasubdir=$pkgname --enable-tests \
    --dynlibdir=/usr/lib --libsubdir=\$compiler/site-local/\$pkgid \
    -f-enable_flaky_tests
  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
  runhaskell Setup test --show-details=direct || 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 -f "$pkgdir"/usr/share/doc/$pkgname/LICENSE
}
