# Contributor (arch): Thomas Dziedzic <gostrc@gmail.com>
# Contributor: Allan McRae <allan@archlinux.org>
# Contributor: John Proctor <jproctor@prium.net>
# Contributor: 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=ruby2.7
pkgver=2.7.3
_json_ver=2.3.0
pkgrel=1
pkgrel+=.parabola1
arch=(x86_64)
arch+=(i686 armv7h)
url='https://www.ruby-lang.org/en/'
license=(BSD custom)
license=(${license[*]/BSD/BSD2/})
depends=(libxcrypt gdbm openssl libffi libyaml gmp zlib)
optdepends=(
    'tk: for Ruby/TK'
)
makedepends=(tk)
provides=(ruby)
options=(!emptydirs)
source=(https://cache.ruby-lang.org/pub/ruby/${pkgver:0:3}/ruby-${pkgver}.tar.xz)
source+=(json-${_json_ver}.tar.gz::https://github.com/flori/json/archive/v${_json_ver}.tar.gz
        json-libre.patch
        ruby-remove-json-ext.patch
        ruby-add-json_pure.patch)
sha512sums=('b755d418b3bab2f9f6a8893afd13869269f17065643dde78b9e85ae3538a6d0617893db6e9c3908e00a40c7577a5c912a7c822d8f245cdcfb857be76dfb66c1e')
sha512sums+=('0cda44a77212748d9513e61314dfbc7c0cf82beebcc56eb8b043e4ada698bc475502100389b80fa5c9090341fd1c6d2841fc5c9332e520f1c8dd1084ca505379'
             '582017bd0f98878c0ac6f4625854422662d83ae6274a34762082a720052023780dceb17694b1f4e18d9a0dcbb525990341176eac31040aa63099a8b3a8d52071'
             'f952db3774f5eb5bc6c445b690262a883553b51f42dfdcc802c6de5bc2b2907d73472ede38d9d7a1b8efe8fc9cd00e64d986d78353f0a1c86497f71ebd522c48'
             '9ff8a2ec9fdecd05fd506ed6f977dcd483e47611dc6bbe6c4622100a2071c0f6c7b3f813a94b73bebb9f0ed7fe9f6257289cdf971030ad525a3c8a0d065b09c2')


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

  # Modify 'json' so that it doesn't try to load (non-free!) 'json/ext'
  echo "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}"
  echo "applying ruby-remove-json-ext.patch"
  rm -r ext/json test/json
  patch -p1 -i ../ruby-remove-json-ext.patch

  # and insert the json_pure lib instead
  echo "applying ruby-add-json_pure.patch"
  cp -r  ../json-${_json_ver}/lib/*             -t lib/
  cp     ../json-${_json_ver}/json_pure.gemspec -t lib/json/
  cp -rT ../json-${_json_ver}/tests                test/json
  patch -p1 -i ../ruby-add-json_pure.patch

  # Delete original json sources
  cd "$srcdir"
  rm -rf "json-${_json_ver}"
}

build() {
  cd ruby-${pkgver}

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

  make
}

package() {
  cd ruby-${pkgver}

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

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

  # remove files conflicting with 'ruby' package
  rm -r "$pkgdir"/usr/share/man/
}
