# Contributor (Arch): Thomas Dziedzic <gostrc@gmail.com>
# Contributor (Arch): Allan McRae <allan@archlinux.org>
# Contributor (Arch): John Proctor <jproctor@prium.net>
# Contributor (Arch): Jeramy Rutley <jrutley@gmail.com>

# Maintainer: Omar Vega Ramos <ovruni@gnu.org.pe>
# Contributor: Daniel Milewski <niitotantei@riseup.net>
# Contributor: André Silva <emulatorman@hyperbola.info>
# Contributor: Luke Shumaker <lukeshu@parabola.nu>
# Contributor: fauno <fauno@parabola.nu>

# Rationale for inclusion in [libre]: Problematic license of
# generator.c in bundled json extension.
#
# https://labs.parabola.nu/issues/674
# https://bugs.ruby-lang.org/issues/11844
# https://github.com/flori/json/issues/277


pkgname=(ruby ruby-docs)
pkgver=3.0.1
_jsonver=2.5.1
pkgrel=1
pkgrel+=.parabola1
_srcrel=1 # increment this to rebuild the sourceball without bumping pkgver
arch=(x86_64)
arch+=(i686 armv7h)
url='https://www.ruby-lang.org/en/'
license=(BSD2 custom)
depends=(libxcrypt)
makedepends=(gdbm openssl libffi doxygen graphviz libyaml ttf-dejavu tk)
options=(!emptydirs)
source=(https://cache.ruby-lang.org/pub/ruby/${pkgver:0:3}/ruby-${pkgver}.tar.xz
        json-${_jsonver}.tar.gz::https://github.com/flori/json/archive/v${_jsonver}.tar.gz
        remove-json-ext.patch
        add-json_pure.patch
        json-libre.patch)
sha512sums=('97d2e883656060846b304368d9d836e2f3ef39859c36171c9398a0573818e4ed75bfd7460f901a9553f7f53518c505327a66e74f83704a881469f5ac61fe13d7'
            '4df7ec9b86692376e89c2019c2c1ab3ae9af28fb4742864acc6f985c0551fc10795742cc1124d6435e820617c00d4c5f4aa6c83b21d2fff43ed05cd9d0a89087'
            'e3cb4d17872d69d7bc2697b751e27c7f76edbf996f4e3007241cd8a60d2251056cf8dd858f249c0b28088da5b969c63bc5c0d52eef9c95e52e190234d3e46609'
            'c3933f1c6d7ddf669e0cfd8ffdd60abeb59a10bdfcab05a1c1b826ab65bed7019df85c15dd8af0b127a5728d5f6c322fcd724e0b9ecf0d9cdb5d28956050cc9a'
            'bb2cc232a0b096a3c40e219258aaa008183ed9a1f8b3bfd5f84680809f4b60e659f02e1db24480b30e6f11ad726b258da9dab17462621e8bfb8f9b6ed98dfbfd')

prepare() {
  cd "$srcdir/json-${_jsonver}"

  # Modify 'json' so that it doesn't try to load (non-free!) 'json/ext'
  msg "applying json-libre.patch"
  rm -rf -- json.gemspec ext lib/json/ext*
  patch -p1 -i ../json-libre.patch

  # Now remove the json ext from the ruby sources
  cd "$srcdir/ruby-${pkgver}"
  msg "applying remove-json-ext.patch"
  rm -r ext/json test/json
  patch -p1 -i ../remove-json-ext.patch

  # and insert the json_pure lib instead
  msg "applying add-json_pure.patch"
  cp -r  ../json-${_jsonver}/lib/*             -t lib/
  cp     ../json-${_jsonver}/json_pure.gemspec -t lib/json/
  cp -rT ../json-${_jsonver}/tests                test/json
  sed -i "s/File.read(\"VERSION\").chomp/\"${_jsonver}\"/g" lib/json/json_pure.gemspec
  patch -p1 -i ../add-json_pure.patch

  # Clean up the original json sources
  cd "$srcdir"
  rm -rf "json-${_jsonver}"
}

build() {
  cd ruby-${pkgver}

  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --sharedstatedir=/var/lib \
    --libexecdir=/usr/lib/ruby \
    --enable-shared \
    --disable-rpath \
    --with-dbm-type=gdbm_compat

  make
}

check() {
  cd ruby-${pkgver}

  make test
}

package_ruby() {
  pkgdesc='An object-oriented language for quick and easy programming'
  depends=(gdbm openssl libffi libyaml gmp zlib rubygems ruby-irb)
  optdepends=(
      'ruby-docs: Ruby documentation'
      'tk: for Ruby/TK'
  )

  cd ruby-${pkgver}

  make DESTDIR="${pkgdir}" install-nodoc

  install -D -m644 COPYING "${pkgdir}/usr/share/licenses/ruby/LICENSE"
  install -D -m644 BSDL "${pkgdir}/usr/share/licenses/ruby/BSDL"

  rubyver=${pkgver:0:3}.0

  # remove rubygems as it shipped as a separate package
  rm -r "${pkgdir}"/usr/lib/ruby/${rubyver}/{rubygems,rubygems.rb}
  rm "${pkgdir}"/usr/bin/gem

  # remove bundler as it shipped as a separate package
  rm -r "${pkgdir}"/usr/lib/ruby/${rubyver}/{bundler,bundler.rb}
  rm "${pkgdir}"/usr/bin/{bundle,bundler}
  rm "${pkgdir}"/usr/lib/ruby/gems/${rubyver}/specifications/default/bundler-*.gemspec

  # remove bundled rdoc gem
  rm -r "${pkgdir}"/usr/lib/ruby/${rubyver}/{rdoc,rdoc.rb}
  rm "${pkgdir}"/usr/bin/{rdoc,ri}
  rm "${pkgdir}"/usr/lib/ruby/gems/${rubyver}/specifications/default/rdoc-*.gemspec
  rm "${pkgdir}"/usr/share/man/man1/ri.1

  # remove irb as it is a separate package now
  rm -r "${pkgdir}"/usr/lib/ruby/${rubyver}/{irb,irb.rb}
  rm "${pkgdir}"/usr/bin/irb
  rm "${pkgdir}"/usr/lib/ruby/gems/${rubyver}/specifications/default/irb-*.gemspec
  rm "${pkgdir}"/usr/share/man/man1/irb.1

  # remove other binaries that are shipped as separate packages
  rm "${pkgdir}"/usr/bin/{rake,rbs,typeprof,erb,racc}
  rm "${pkgdir}"/usr/share/man/man1/erb.1

  # remove all bundled gems to avoid conflicts with ruby-* Arch packages
  rm -r "${pkgdir}"/usr/lib/ruby/gems/${rubyver}/gems/*
  rm "${pkgdir}"/usr/lib/ruby/gems/${rubyver}/specifications/*.gemspec
  rm "${pkgdir}"/usr/lib/ruby/gems/${rubyver}/cache/*.gem
}

package_ruby-docs() {
  pkgdesc='Documentation files for ruby'

  cd ruby-${pkgver}

  make DESTDIR="${pkgdir}" install-doc install-capi

  install -D -m644 COPYING "${pkgdir}/usr/share/licenses/ruby-docs/LICENSE"
  install -D -m644 BSDL "${pkgdir}/usr/share/licenses/ruby-docs/BSDL"

  # sometimes it installs ruby/capi/doxygen_objdb_XXXXXXX.tmp;
  # otherwise /usr/share/doc is empty
  rm -rf "$pkgdir/usr/share/doc"
}
