# Maintainer: Felix Yan <felixonmars@archlinux.org>

pkgname=ruby-async-http
pkgver=0.94.3
pkgrel=1
pkgdesc='A HTTP client and server library'
arch=(any)
url='https://github.com/socketry/async-http'
license=(MIT)
depends=(
  ruby
  ruby-async
  ruby-async-pool
  ruby-io-endpoint
  ruby-io-stream
  ruby-metrics
  ruby-protocol-http
  ruby-protocol-http1
  ruby-protocol-http2
  ruby-protocol-url
  ruby-traces
)
makedepends=(
  git
  ruby-rdoc
)
checkdepends=(
  ruby-async-container
  ruby-bake
  ruby-bake-test
  ruby-bake-test-external
  ruby-bundler
  ruby-covered
  ruby-decode
  ruby-localhost
  ruby-rack-test
  ruby-sus
  ruby-sus-fixtures-async
  ruby-sus-fixtures-async-http
  ruby-sus-fixtures-benchmark
  ruby-sus-fixtures-openssl
)
options=(!emptydirs)
source=(git+https://github.com/socketry/async-http.git#tag=v$pkgver)
sha512sums=('a932f9ae0124c31738ce6f99dc8d9d765c38460bf52df5c4891042673a2619753c81eea21b193c49ecca25b07b2ec499536659b465f84f8fb7fa62366143f224')
b2sums=('9886e52ec6756bb5d90aa5a9724e6ea34c44b308ab075c0a63ec862df59f3fd99929b7ab7eb7f064dfaf903e9558e8e8ae37badb8df9566a42e4875acdc658f6')

prepare() {
  cd async-http

  # Fix the HTTP/1.1 body-write test with protocol-http1 >= 0.37.1:
  # https://github.com/socketry/async-http/commit/7a8850e6fb081af1c672a537828fc3b596f6e8f5
  git cherry-pick -n 7a8850e6fb081af1c672a537828fc3b596f6e8f5

  sed -r \
    -e 's|~>|>=|g' \
    -e '/signing_key/d' \
    -i async-http.gemspec

  sed --in-place \
    --expression '/group :maintenance/,/end/d' \
    --expression '/rubocop/d' \
    gems.rb
}

build() {
  local _gemdir="$(gem env gemdir)"
  cd async-http
  gem build async-http.gemspec
  gem install \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "tmp_install/$_gemdir" \
    --bindir "tmp_install/usr/bin" \
    async-http-$pkgver.gem
  find "tmp_install/$_gemdir/gems/" \
    -type f \
    \( \
        -iname "*.o" -o \
        -iname "*.c" -o \
        -iname "*.so" -o \
        -iname "*.time" -o \
        -iname "gem.build_complete" -o \
        -iname "Makefile" \
    \) \
    -delete
  rm -r tmp_install/$_gemdir/cache
}

check() {
  local _gemdir="$(gem env gemdir)"
  cd async-http
  GEM_HOME="tmp_install/$_gemdir" bake test
  GEM_HOME="tmp_install/$_gemdir" sus
}

package() {
  cd async-http
  cp -a tmp_install/* "$pkgdir"/
}
