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

_gemname='rdoc'
pkgname="ruby-${_gemname}"
pkgver=6.5.1.1
pkgrel=2.1
pkgdesc='Command-line documentation generator for Ruby projects'
arch=(any)
url='https://ruby.github.io/rdoc/'
license=(
	Ruby
)
depends=(
  ruby
  ruby-erb
  ruby-psych
)
makedepends=(
  ruby-bundler
  ruby-kpeg
  ruby-racc
  ruby-rake
  ruby-rdoc
)
checkdepends=(
  ruby-test-unit
)
options=('!emptydirs')
source=("https://github.com/ruby/rdoc/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz")
sha512sums=('ed7dbfd2a085b044a311222237f7153b7b22ee34af7e6ac18826ede2fe3afdc25120546f789ff03fec952dc100d33c0f86da50c61bf9d60ba3957650318a27c9')

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}"

  # rake build - can't be used as it requires rubocop: https://github.com/ruby/rdoc/blob/v6.4.0/Rakefile#L99-L107
  rake generate
  gem build "${_gemname}.gemspec"

  rake rdoc
}

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

  rake normal_test
  rake rubygems_test
}

package() {
  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')"

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

  install --verbose --directory --mode=0755 \
    "${pkgdir}/usr/lib/ruby/${_extension_api_version}/${_platform}" \
    "${pkgdir}${_gemdir}/specifications/default"

  mv --verbose "${pkgdir}${_gemdir}/specifications/${_gemname}-${pkgver}.gemspec" "${pkgdir}${_gemdir}/specifications/default/${_gemname}-${pkgver}.gemspec"
  mv --verbose "${pkgdir}${_gemdir}/gems/${_gemname}-${pkgver}/lib/${_gemname}.rb" "${pkgdir}/usr/lib/ruby/${_extension_api_version}/${_gemname}.rb"
  mv --verbose "${pkgdir}${_gemdir}/gems/${_gemname}-${pkgver}/lib/${_gemname}" "${pkgdir}/usr/lib/ruby/${_extension_api_version}/${_gemname}"

  # remove unrepreducible files
  rm --force --recursive --verbose \
    "${pkgdir}${_gemdir}/cache/" \
    "${pkgdir}${_gemdir}/build_info/" \
    "${pkgdir}${_gemdir}/extensions/" \
    "${pkgdir}${_gemdir}/gems/${_gemname}-${pkgver}/"{.git*,bin,man,*.*,Gemfile,Rakefile} \
    "${pkgdir}${_gemdir}/plugins/" \
    "${pkgdir}${_gemdir}/doc/${_gemname}-${pkgver}/ri/ext/"

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

# vim: ts=2 sw=2 et:
