# Maintainer: Luke T. Shumaker <lukeshu@parabola.nu>
# Maintainer (arch): Anatol Pomozov <anatol.pomozov@gmail.com>
# Maintainer (arch): Andreas 'Segaja' Schleifer <segaja at archlinux dot org>

# Changes from Arch:
# - Don't fetch packages from the third-party package repository
#   rubygems.org (which hosts non-free packages) by default.
#   https://wiki.parabola.nu/TPPM_Liberation_Project

pkgname='rubygems'
pkgver=3.3.25
pkgrel=1
pkgrel+=.parabola1
pkgdesc='Package management framework for Ruby'
arch=('any')
url='https://rubygems.org/'
license=('MIT')
depends=('ruby')
#checkdepends=('cmake' 'git' 'ruby-rake' 'ruby-webrick')
backup=('etc/gemrc')
options=('!emptydirs')
source=("https://github.com/rubygems/rubygems/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz"
        "${pkgname}_stop_so_duplication.patch"
        '0001-FSDG-Have-default_sources-be-empty.patch'
        'gemrc')
sha512sums=('7acfcd0b34ee522b3be22b59b2ae9b7f8ea31f850dc415365d93ef7fd80b751fdc504c3d539d5ce351e3d58802adf277577a134caac20b987d629a0b3864d395'
            '9919490bbf7dba979a1df7543e62eb3fca48e8a516e6b6ab0a73080952e1b58599b7f233259d122dc66bf93f032b434d70e0dd448a1cb86513f01acb51b2120e'
            'e59e2b3961deb06742a0a122c93dba58f3668faa27da14b3a11bffd4e9c74b8ec5ee12b77e48effdd525145b3f0b7c1b4538f26075b9ed904c43b1c2be231a7b'
            '8cafd14d414ee3c16aa94f79072bc6c100262f925dc1300e785846c3fabbbbffc1356b8e2223af5684e3340c55032d41231179ffa948bb12e01dbae0f4131911')

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

  patch --verbose --strip=1 --input="../${pkgname}_stop_so_duplication.patch"
  patch -p1 -i ../0001-FSDG-Have-default_sources-be-empty.patch
}

# https://github.com/rubygems/rubygems/issues/5951
#check() {
#  cd "rubygems-${pkgver}"
#
#  sed --in-place --regexp-extended 's|git ls-files|find -type f -not -path "*/\.git/*"|' Rakefile
#
#  export GEM_HOME="$(pwd)/.gem"
#  mkdir --parents --verbose "${GEM_HOME}"
#
#  rake update_manifest
#  rake test
#}

package() {
  cd "rubygems-${pkgver}"

  # Parabola: For bootstrapping purposes, we've inserted `-r ./lib/rubygems.rb`.
  local _gemdir="$(ruby -r ./lib/rubygems.rb -e 'puts Gem.dir')"
  local _extension_api_version="$(ruby -r ./lib/rubygems.rb -e 'puts Gem.extension_api_version')"

  export GEM_HOME="${pkgdir}${_gemdir}"

  install --verbose -d --mode=0755 \
      "${GEM_HOME}" \
      "${pkgdir}/usr/lib/ruby/${_extension_api_version}"

  # define the current version as previous version to avoid printing the changelog
  ruby setup.rb \
    --destdir="${pkgdir}" \
    --prefix="/usr" \
    --previous-version="${pkgver}" \
    --verbose

  # fix package structure
  mv --verbose "${pkgdir}/usr/lib/${pkgname}.rb" "${pkgdir}/usr/lib/ruby/${_extension_api_version}"
  mv --verbose "${pkgdir}/usr/lib/${pkgname}" "${pkgdir}/usr/lib/ruby/${_extension_api_version}"

  # cleanup - remove any bundler references as they are provided by https://archlinux.org/packages/community/any/ruby-bundler/
  rm --verbose --recursive \
    "${pkgdir}/usr/bin/"{bundle*,rake} \
    "${pkgdir}/usr/gems" \
    "${pkgdir}/usr/lib/bundler.rb" \
    "${pkgdir}/usr/lib/bundler" \
    "${pkgdir}/usr/specifications"

  install --verbose -D --mode=0644 ../gemrc "${pkgdir}/etc/gemrc"
  install --verbose -D --mode=0644 LICENSE.txt MIT.txt --target-directory "${pkgdir}/usr/share/licenses/${pkgname}"
  install --verbose -D --mode=0644 *.md --target-directory "${pkgdir}/usr/share/doc/${pkgname}"
}
