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

_hkgname=http-client-tls
pkgname=haskell-http-client-tls
pkgver=0.3.6.3
pkgrel=55
pkgdesc="http-client backend using the connection package and tls library"
url="https://github.com/snoyberg/http-client"
license=("MIT")
arch=('x86_64' 'aarch64')
depends=('ghc-libs' 'haskell-case-insensitive' 'haskell-crypton' 'haskell-crypton-connection'
         'haskell-data-default-class' 'haskell-http-client' 'haskell-http-types' 'haskell-memory'
         'haskell-network' 'haskell-network-uri' 'haskell-tls')
makedepends=('ghc' 'uusi' 'haskell-hspec')
source=("https://hackage.haskell.org/packages/archive/${_hkgname}/${pkgver}/${_hkgname}-${pkgver}.tar.gz")
sha512sums=('2b4475a9fdcfed07512f19af989289d8b8eacc8568392c03839a806327d5b1cd6dfcbc5afe29a0dd3f63d9c93a7043c4fea594b73c46b43db94518dd7a39325f')

prepare() {
    cd $_hkgname-$pkgver
    # https://github.com/snoyberg/http-client/commit/89770ff263da242f0004443e0b3ae0a162233c6e#diff-ba1d2cc64da913f00a57c0ea78a399740ba9bec10e610e620803eab2eb049688
    uusi -r hspec:hspec,crypton-connection $_hkgname.cabal

    # httpbin.org is very unstable
    sed -i 's|httpbin.org|ddosgan.com|' test/Spec.hs
}

build() {
    cd $_hkgname-$pkgver
    
    runhaskell Setup configure -O --enable-shared --enable-executable-dynamic --disable-library-vanilla \
        --prefix=/usr --docdir="/usr/share/doc/$pkgname" --enable-tests \
        --dynlibdir=/usr/lib --libsubdir=\$compiler/site-local/\$pkgid \
        --ghc-option=-optl-Wl\,-z\,relro\,-z\,now \
        --ghc-option='-pie'
    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
}
