# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Andreas 'Segaja' Schleifer <segaja at archlinux dot org>

_gemname='kpeg'
pkgname="ruby-${_gemname}"
pkgver=1.3.3
pkgrel=2.1
pkgdesc='A simple PEG library for ruby'
arch=(any)
url="https://github.com/evanphx/${_gemname}"
license=(MIT)
depends=(
  ruby
)
makedepends=(
  ruby-hoe
  ruby-rake
)
checkdepends=(
  ruby-minitest
)
options=('!emptydirs')
source=("${url}/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz")
sha512sums=('42d3de3b8f7132e073c6e4fc9af2b606c2ce98d7666b2606f5d962b9e84b306958b824752c7649684b64f957b17ce60966ab149cc358a01d435d28467b0a030b')
b2sums=('1f89ccef059e10e062e6e6c1da4cfe8f141a060494dacf07551da35b6902017174212a7ec7e0c349a4d2cb9f55586866d19976baef95845ef5512a1872429a58')

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

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

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

  local _gemdir="$(gem env gemdir)"

  gem build "${_gemname}.gemspec"

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

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

  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

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

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 --directory --mode=0755 "${pkgdir}/usr/share/vim/vimfiles/"
  mv --verbose vim/syntax_kpeg/* "${pkgdir}/usr/share/vim/vimfiles/"

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

# vim: ts=2 sw=2 et:
