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

_gemname='io-nonblock'
pkgname="ruby-${_gemname}"
pkgver=0.1.0
pkgrel=3
pkgdesc='Enables non-blocking mode with IO class'
arch=('x86_64' 'aarch64')
url="https://github.com/ruby/${_gemname}"
license=('BSD' 'RUBY')
depends=('ruby')
makedepends=('ruby-rake' 'ruby-rake-compiler')
options=('!emptydirs')
source=("${url}/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz")
sha512sums=('3bec09537938807276b63303105db692ef5c2a6c9bfdfcb35a96a16b44ad1a61d3bd59fb108fcdc1509816a6d96d5aa0ec370d20488a588763c5fbfef75cd651')
b2sums=('efaff44e79e913e2ccff828065b55c00d766da11bda2b6d492ff20f3956969e35c549f15ac25e65422c1b6790739a31eb3915467aefab71592b68150a81fb922')

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

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

  # we build based on a tar archive, not a git repo
  sed --in-place --regexp-extended 's|git ls-files -z|find . -type f -not -path "*/\.git/*" -printf "%P\\\\0"|' "${_gemname}.gemspec"
}

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

  local _gemdir="$(gem env gemdir)"
  local _platform="$(gem env platform | cut -d':' -f2)"
  local _extension_api_version="$(ruby -e 'puts Gem.extension_api_version')"

  install --verbose --directory --mode=0755 \
    "tmp_install_default/gemspec/specifications/gems/${_gemname}-${pkgver}" \
    "tmp_install/usr/lib/ruby/${_extension_api_version}/${_platform}" \
    "tmp_install${_gemdir}/specifications/default"

  rake compile build

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

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

  mv --verbose "tmp_install_default/gemspec/specifications/default/${_gemname}-${pkgver}.gemspec" "tmp_install${_gemdir}/specifications/default/${_gemname}-${pkgver}.gemspec"
  mv --verbose "tmp_install${_gemdir}/gems/${_gemname}-${pkgver}/lib/io" "tmp_install/usr/lib/ruby/${_extension_api_version}/${_platform}/"

  # remove unrepreducible files
  rm --force --recursive --verbose \
    "tmp_install${_gemdir}/cache/" \
    "tmp_install${_gemdir}/build_info/" \
    "tmp_install${_gemdir}/extensions/" \
    "tmp_install${_gemdir}/gems/" \
    "tmp_install${_gemdir}/plugins/" \
    "tmp_install${_gemdir}/specifications/${_gemname}-${pkgver}.gemspec" \
    "tmp_install${_gemdir}/doc/${_gemname}-${pkgver}/ri/ext/"
}

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

  local _gemdir="$(gem env gemdir)"

  GEM_HOME="tmp_install/${_gemdir}" rake test
}

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

  cp --archive --verbose tmp_install/* "${pkgdir}"

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