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

_hkgname=http2
pkgname=haskell-http2
pkgver=5.1.0
pkgrel=7
pkgdesc="HTTP/2 library"
url="https://github.com/kazu-yamamoto/http2"
license=('BSD-3-Clause')
arch=('x86_64' 'aarch64')
depends=('ghc-libs' 'haskell-async' 'haskell-case-insensitive' 'haskell-http-types'
         'haskell-network' 'haskell-network-byte-order' 'haskell-network-control'
         'haskell-time-manager' 'haskell-unix-time' 'haskell-unliftio' 'haskell-utf8-string')
makedepends=('ghc' 'uusi' 'haskell-aeson' 'haskell-aeson-pretty' 'haskell-base16-bytestring'
             'haskell-crypton' 'haskell-glob' 'haskell-hspec' 'haskell-hspec-discover'
             'haskell-network-run' 'haskell-random' 'haskell-typed-process'
             'haskell-unordered-containers' 'haskell-vector')
source=("https://hackage.haskell.org/packages/archive/${_hkgname}/${pkgver}/${_hkgname}-${pkgver}.tar.gz")
sha512sums=('1c6f7b8e679c8dd40ae753abb71bce4a202c41fefebd8e42dbb21df8180c11ac1ee0859857544b1a787a3d879197fabf674fe0c17fb74e1b5181f7f72d6a1f27')

prepare() {
    cd $_hkgname-$pkgver
    uusi -u network-control
    sed -i 's/rxfWindow/rxfBufSize/' Network/HTTP2/Client/Run.hs Network/HTTP2/H2/Receiver.hs
    # Choose another port to bind, 8080 is too easy to conflict with build host
    sed -i 's/"8080"/"59824"/' test2/ServerSpec.hs test/HTTP2/ServerSpec.hs
}

build() {
    cd $_hkgname-$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-devel
    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 $_hkgname-$pkgver
    runhaskell Setup test --show-details=direct
}

package() {
    cd $_hkgname-$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"
    install -D -m644 LICENSE "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
    rm -f "$pkgdir"/usr/share/doc/$pkgname/LICENSE
}
