# Maintainer: Andreas 'Segaja' Schleifer <archlinux at segaja dot de>

_gemname='puma'
pkgname="ruby-$_gemname"
pkgver=5.6.5
pkgrel=2
pkgdesc='A Ruby/Rack web server built for concurrency'
arch=('x86_64' 'aarch64')
url='https://puma.io/'
license=('BSD-3-CLAUSE')
options=(!emptydirs)
depends=('ruby' 'ruby-nio4r' 'ruby-sd_notify')
makedepends=('ruby-bundler' 'ruby-minitest' 'ruby-minitest-proveit' 'ruby-minitest-retry' 'ruby-minitest-stub-const' 'ruby-rack' 'ruby-rake' 'ruby-rake-compiler' 'ruby-rdoc')
source=("https://github.com/puma/puma/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz"
        "${pkgname}_fix_tests.patch"
        "update-ssl.patch")
sha512sums=('0ad41f3372732bdea6594a20b116ec4fc03a8a1d2ff807b3c315e7769aef4e5361ff775975575125002fbfc83315e6906fa8691ce666161bd8beee8c469289d8'
            '79a098f70fcb52a716bbbb25bac99bdce569a0741395589a28533281e57a532a40dfbd5e2737aaeab3cef82d0a37beec5923b3a43437d7ddedb45426766d5f53'
            '2808978c34870ce979a2661447cd7fb0005095016d847430d4086f19f25bd9fc654c9559eb3b2b2eaad01a10e58246a449fd0993fac24faa399416d752fc3f86')

prepare() {
  cd "${_gemname}-${pkgver}"

  # remove some tests and fix others
  patch --verbose --strip=1 --input="../${pkgname}_fix_tests.patch"

  # we built based on a tar archive, not a git repo
  sed --in-place 's/git ls-files/find/' "${_gemname}.gemspec"

  # update gemspec/Gemfile to allow newer version of the deps
  sed --in-place --regexp-extended 's|~>|>=|g' "${_gemname}.gemspec" Gemfile

  # disable rubocop, we don't have that in our repos and it doesn't provide any benefits during building
  sed --in-place '/rubocop/Id' Rakefile Gemfile

  # disable m. Only required for debugging during development
  sed --in-place "/'m'/d" Gemfile

  # disable localhost. Only required for self-signed certificates during development
  sed --in-place "/localhost/d" Gemfile

  # update test certificates to a newer upstream version due to expiry
  patch -Np1 -i "${srcdir}/update-ssl.patch"
}

build() {
  cd "${_gemname}-${pkgver}"

  rake compile
  rake build
}

check() {
  cd "${_gemname}-${pkgver}"

  # https://github.com/puma/puma/blob/master/CONTRIBUTING.md#file-limits
  ulimit -Sn 5000

  # some seeds cause random errors ( https://github.com/puma/puma/issues/2825 ) so we use a fixed seed
  export TESTOPTS="--seed=21082"

  # avoid hard coding the srcdir inside the tests...
  export SRCDIR="${srcdir}"

  rake test:all
}

package() {
  cd "${_gemname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"

  gem install \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "${pkgdir}/${_gemdir}" \
    --bindir "${pkgdir}/usr/bin" \
    "pkg/${_gemname}-${pkgver}.gem"

  # remove unrepreducible files
  rm --force --recursive --verbose \
    "${pkgdir}/${_gemdir}/cache/" \
    "${pkgdir}/${_gemdir}/gems/${_gemname}-${pkgver}/vendor/" \
    "${pkgdir}/${_gemdir}/doc/${_gemname}-${pkgver}/ri/ext/"

  find "${pkgdir}/${_gemdir}/gems/" \
    -type f \
    \( \
      -iname "*.o" -o \
      -iname "*.c" -o \
      -iname "*.so" -o \
      -iname "*.time" -o \
      -iname "gem.build_complete" -o \
      -iname "Makefile" \
    \) \
    -delete

  find "${pkgdir}/${_gemdir}/extensions/" \
    -type f \
    \( \
      -iname "mkmf.log" -o \
      -iname "gem_make.out" \
    \) \
    -delete

  install -D --mode=644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
  install -D --mode=644 *.md --target-directory "${pkgdir}/usr/share/doc/${pkgname}"
}
