# Maintainer (arch): Anatol Pomozov <anatol.pomozov@gmail.com>
# Maintainer (arch): Andreas 'Segaja' Schleifer <segaja at archlinux dot org>
# Maintainer (arch): Tim Meusel <tim@bastelfreak.de>
# Contributor: 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>


# parabola changes and rationale:
#  - replace non-free '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
#    https://github.com/flori/json/pull/567
#  - specify *which* BSD license


pkgname=(ruby ruby-docs ruby-stdlib ruby-bundledgems)
pkgver=3.0.6
pkgrel=1
arch=(x86_64)
url='https://www.ruby-lang.org/en/'
license=(BSD custom)
makedepends=(doxygen gdbm graphviz libffi libyaml openssl ttf-dejavu tk)
options=(!emptydirs !lto) # Disable LTO until fixes for https://bugs.ruby-lang.org/issues/18062 released
_osslver=3.0.0
source=(https://cache.ruby-lang.org/pub/ruby/${pkgver:0:3}/ruby-${pkgver}.tar.xz
        ruby-openssl-$_osslver.tar.xz::https://github.com/ruby/openssl/archive/refs/tags/v$_osslver.tar.gz)
sha512sums=('086516c563673692aa9cb63b82617397f8b845641b3eb09833989292d31f987af808ac1ae9aa6b0eee1688e5f3f7a0777017bfd4027142152ca939eba2b1a551'
            'SKIP'
            '076d2924b61c4f7704b47718a33732fb5285a3af39f328ed9e83add36589da99dfae73b3e893398fc3f2a74dcbead0b66edc682040fa1b48f9530894c7187f95')
b2sums=('461878ce72f6ec33365c21ab84db52225533f9967f005405b1675a49e77a11a737a8de7eed9c59fcddb2685ae2fb9ac879ad8252a112ccf51ca7bf1c8e9d118d'
        'SKIP'
        '2eeb64caf188d79137686a8c5f0aa9d27860b3a3da8e0d4082f749beaa600928d814fe8cd70a6921358066603ce453d92838c1bce32509574ea333b6ca567fc8')

# Parabola: Mundane packaging considerations.
license=( ${license[*]/BSD/custom:BSD-2-Clause} )
arch+=(armv7h i686)
pkgrel+=.parabola1
options+=(debug)

# Parabola: Replace the CVTUTF code with a free implementation.
# https://github.com/flori/json/pull/567
# We need to do this in mksource() (not of prepare()) so that the
# .src.pkg.tar file doesn't include nonfree sources.
_jsonver=6e75be64c896e093075ec99bf94a3f5fc576c283
_srcrel=1 # increment this to rebuild the sourceball (mksource, _jsonver) without bumping pkgver
mksource=("${source[0]}"
          "ruby-json-${_jsonver}.tar.gz::https://github.com/parabola-gnulinuxlibre/ruby-json/archive/${_jsonver}.tar.gz"
          0001-Prep-to-use-Parabola-s-patched-ruby-json.patch)
mksha512sums=('abbf883cd9f3ddbd171df8f8c3cd35d930623c4c01a5e01387de0aee9811cca7604b82163e18e04f809773bf1ca5a450f13f62f3db14f191f610e116ae4fa6f8'
              'ce0a1399f0974e3919e85af9aa50e982a5e1adff7312f716033f6a19ce7044b046e037f19fcf43adaeff402c882bd0a3b045e93c245287a7f947754125eb39c6'
              'e2df05dea2014edd025f1ebeb9bffe121bfb49e4b45dc906f74b947f0d9f869dec5fee385533a2cf26057fcd226a0d0905b94ee8ca8a3f38cf511c39fa22ab12')
mkb2sums=('e1fca86cdf267fc2289ef4d605fdf0023265c3a291ca4088361c0acd64310a7c41bf0750b4ac762157c20ceaa5df8bdc0a67374bcad9654bca5c3fc8630ff89c'
          'f1339558107ee1f4b9a54b63d601c9c4a78ed1a4cc148c3c57d963d5536a1b8ced66dd7cd4b7ea9bc2c5ee5fcb3801c028671884484e16ea7d57d0a49e16f317'
          'b0ec9e3d834c4260211838ce45211dde552b0f679b05fe523469e52416bc89ddc835746355b2b8bae9dcf1a26733d2968b134fa67712e5f60180134569470a73')
source=("https://repo.parabola.nu/other/ruby-libre/ruby-${pkgver}-libre${_srcrel}.tar.gz"{,.sig} "${source[@]:1}")
mkdepends=(ruby)
mksource() {
  mkdir "$srcdir/ruby"
  mv -T "$srcdir/ruby-$pkgver" "$srcdir/ruby/ruby"

  mkdir "$srcdir/flori"
  mv -T "$srcdir/ruby-json-$_jsonver" "$srcdir/flori/json"

  pushd "$srcdir/ruby/ruby"
  patch -p1 -i ../../0001-Prep-to-use-Parabola-s-patched-ruby-json.patch
  ruby tool/sync_default_gems.rb json
  popd

  mv -T "$srcdir/ruby/ruby" "$srcdir/ruby-${pkgver}"
  rm -rf -- "$srcdir/ruby" "$srcdir/flori"
}


prepare() {
  cd ruby-${pkgver}
  rm -rf ext/openssl test/openssl
  mv ../openssl-$_osslver/ext/openssl ext
  mv ../openssl-$_osslver/lib ext/openssl
  mv ../openssl-$_osslver/{History.md,openssl.gemspec} ext/openssl
  mv ../openssl-$_osslver/test/openssl test
}

build() {
  cd ruby-${pkgver}

  # this uses malloc_usable_size, which is incompatible with fortification level 3
  export CFLAGS="${CFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"
  export CXXFLAGS="${CXXFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"

  ./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}

  # this uses malloc_usable_size, which is incompatible with fortification level 3
  export CFLAGS="${CFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"
  export CXXFLAGS="${CXXFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"

  make test
}

package_ruby() {
  pkgdesc='An object-oriented language for quick and easy programming'
  depends=(gdbm openssl libffi libyaml libxcrypt gmp zlib rubygems ruby-stdlib ruby-bundledgems)
  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 "${pkgdir}"/usr/bin/{bundle,bundler}

  # remove bundled rdoc gem
  rm "${pkgdir}"/usr/bin/{rdoc,ri}
  rm "${pkgdir}"/usr/share/man/man1/ri.1

  # remove irb as it is a separate package now
  rm "${pkgdir}"/usr/bin/irb
  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

  # remove already packaged stdlib gems (needs to be as dependency in ruby-stdlib)
  local stdlib_gems=(
    abbrev
    base64
    benchmark
    bigdecimal
    bundler
    cgi
    csv
    date
    delegate
    did_you_mean
    digest
    drb
    english
    erb
    etc
    fcntl
    fiddle
    fileutils
    find
    forwardable
    getoptlong
    io-console
    io-nonblock
    io-wait
    ipaddr
    irb
    json
    logger
    mutex_m
    net-http
    open-uri
    psych
    racc
    rdoc
    reline
    stringio
    time
    tmpdir
    uri
  )

  for stdlib_gem in "${stdlib_gems[@]}"; do
    # Parabola: We've replaced "x86_64-linux" with *-linux*".
    rm --force --recursive --verbose \
      "${pkgdir}"/usr/lib/ruby/${rubyver}/${stdlib_gem} \
      "${pkgdir}"/usr/lib/ruby/${rubyver}/${stdlib_gem}.rb \
      "${pkgdir}"/usr/lib/ruby/${rubyver}/*-linux*/${stdlib_gem}.so \
      "${pkgdir}"/usr/lib/ruby/gems/${rubyver}/specifications/default/${stdlib_gem}-*.gemspec
  done

  # Parabola: We've replaced "x86_64-linux" with *-linux*".
  rm --recursive --verbose \
    "${pkgdir}"/usr/lib/ruby/${rubyver}/English.rb \
    "${pkgdir}"/usr/lib/ruby/${rubyver}/io/console \
    "${pkgdir}"/usr/lib/ruby/${rubyver}/net/http \
    "${pkgdir}"/usr/lib/ruby/${rubyver}/net/http.rb \
    "${pkgdir}"/usr/lib/ruby/${rubyver}/net/https.rb \
    "${pkgdir}"/usr/lib/ruby/${rubyver}/*-linux*/cgi \
    "${pkgdir}"/usr/lib/ruby/${rubyver}/*-linux*/date_core.so \
    "${pkgdir}"/usr/lib/ruby/${rubyver}/*-linux*/digest \
    "${pkgdir}"/usr/lib/ruby/${rubyver}/*-linux*/io/console.so \
    "${pkgdir}"/usr/lib/ruby/${rubyver}/*-linux*/io/nonblock.so \
    "${pkgdir}"/usr/lib/ruby/${rubyver}/*-linux*/io/wait.so \
    "${pkgdir}"/usr/lib/ruby/${rubyver}/*-linux*/json \
    "${pkgdir}"/usr/lib/ruby/${rubyver}/*-linux*/racc
}

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

package_ruby-stdlib() {
  # upstream list of gems contained in stdlib ( https://github.com/ruby/ruby/tree/master/{ext,lib} )
  pkgdesc='A vast collection of classes and modules that you can require in your code for additional features'

  depends=(
    ruby-abbrev
    ruby-base64
    ruby-benchmark
    ruby-bigdecimal
    ruby-bundler
    ruby-cgi
    ruby-csv
    ruby-date
    #ruby-dbm   # removed in 3.1.2
    #ruby-debug   # removed in 3.1.2
    ruby-delegate
    ruby-did_you_mean
    ruby-digest
    ruby-drb
    ruby-english
    ruby-erb
    ruby-etc
    ruby-fcntl
    ruby-fiddle
    ruby-fileutils
    ruby-find
    ruby-forwardable
    #ruby-gdbm   # removed in 3.1.2
    ruby-getoptlong
    ruby-io-console
    ruby-io-nonblock
    ruby-io-wait
    ruby-ipaddr
    ruby-irb
    ruby-json
    ruby-logger
    #ruby-matrix   # removed in 3.1.2
    ruby-mutex_m
    #ruby-net-ftp   # removed in 3.1.2
    ruby-net-http
    #ruby-net-imap   # removed in 3.1.2
    #ruby-net-pop   # removed in 3.1.2
    #ruby-net-protocol
    #ruby-net-smtp   # removed in 3.1.2
    #ruby-nkf
    #ruby-observer
    ruby-open-uri
    #ruby-open3
    #ruby-openssl
    #ruby-optparse
    #ruby-ostruct
    #ruby-pathname
    #ruby-pp
    #ruby-prettyprint
    #ruby-prime   # removed in 3.1.2
    #ruby-pstore
    ruby-psych
    ruby-racc
    ruby-rdoc
    #ruby-readline
    #ruby-readline-ext
    ruby-reline
    #ruby-resolv
    #ruby-resolv-replace
    #ruby-rinda
    #ruby-securerandom
    #ruby-set
    #ruby-shellwords
    #ruby-singleton
    ruby-stringio
    #ruby-strscan
    #ruby-syslog
    #ruby-tempfile
    ruby-time
    #ruby-timeout
    ruby-tmpdir
    #ruby-tracer   # removed in 3.1.2
    #ruby-tsort
    #ruby-un
    ruby-uri
    #ruby-weakref
    #ruby-yaml
    #ruby-zlib

    #ruby-error_highlight   # new in 3.2.1
    #ruby2_keywords   # new in 3.1.2 - already exists in [community]
  )
}

package_ruby-bundledgems() {
  # upstream list of bundled gems ( https://github.com/ruby/ruby/blob/master/gems/bundled_gems )
  pkgdesc='Ruby Gems (third-party libraries) that are installed by default when Ruby is installed'

  depends=(
    ruby-minitest
    ruby-power_assert
    ruby-rake
    #ruby-rbs
    ruby-rexml
    #ruby-rss
    ruby-test-unit
    #ruby-typeprof

    # --- new in 3.1.2
    #ruby-debug
    #ruby-matrix
    #ruby-net-ftp
    #ruby-net-imap
    #ruby-net-pop
    #ruby-net-smtp
    #ruby-prime
  )
}

# vim: tabstop=2 shiftwidth=2 expandtab:
