# 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>


# Parabola changes and rationale:
#
# FSDG:
# - Disable default TPPM (rubygems.org, which hosts non-free packages).
#   https://wiki.parabola.nu/TPPM_Liberation_Project


pkgname=rubygems
pkgver=3.6.9
pkgrel=1
pkgdesc='Package management framework for Ruby'
arch=('any')
url='https://rubygems.org/'
license=('MIT')
depends=(
  ruby
)
makedepends=(
  rubygems
  ruby-rdoc
)
checkdepends=(
  cmake
  git
  ruby-rake
  ruby-test-unit
  ruby-webrick
)
provides=(
  ruby-rubygems
)
options=('!emptydirs')
source=(
  "https://github.com/rubygems/rubygems/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz"
  operating_system.rb)
sha512sums=('cf6251c86035838bf61831aa31033e4c80e1da12ad03c21627ef79bbbd0268a2ba26194ccef045bbeb2a0591736182fb0e77f388256a0158fa16e3f2b71723ed'
            '9d74b5528fc87c78316abdd5423a15a8859aec0cd230b20906c53e7e05d8ba8a156af42dc0f6cff270f0c75196079e622c45cc9f0b2f512fc5311fd54e37ab11'
            '490b947841884517ca9c331c0f286b8475d09206781e09f81cbe2874effff2958f6e13d8e73148b7bd13cb8e6eb6fccf78518d5c395b4683469ca65c76715aa1')
b2sums=('17fe697504270bfa1a2fe9489f01c86441ab61b286af26eac840d7fc0b733beb207a658edcb889627798bd78ca16f2f0e8d0187a05e1f0da6248ccf9643760de'
        '714ce47535ede25cad914002c45c908de7d4ff6eae5b25daf9ab1d3a75e09fffe6e17bc73422f9ba2fe16ae5e120352c536d2ab6152a36431a7f35e9c5466b47'
        '5e5d4ceb6f7688742ae396149430af308d73ac6744017847d67030cb40c78d2e5d6d156ecd377b2e3bcb7c6d65da8b50a92c17165be2d7a80762e313189fffa7')

pkgrel+=.parabola1
source+=(0001-FSDG-rubygems-Have-default_sources-be-empty.patch)
makedepends+=(ruby-default-gems-bootstrap) # for rubygems
checkdepends+=(ruby-bundled-gems-bootstrap) # for ruby-rake

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

  sed --in-place --regexp-extended 's|git ls-files|find -type f -not -path "*/\.git/*"|' Rakefile

  patch -Np1 -i ../0001-FSDG-rubygems-Have-default_sources-be-empty.patch # Parabola
}

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

  # Parabola: makepkg 7.1 sets `GIT_CONFIG_GLOBAL=/dev/null`; Arch
  # will need this too once they rebuild with pacman 7.1 instead of
  # 7.0.
  export GIT_CONFIG_GLOBAL="${srcdir}/check-gitconfig"
  touch "$GIT_CONFIG_GLOBAL"

  rake update_manifest test
}

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

  local _gemdir="$(ruby -e 'puts Gem.dir')"
  local _extension_api_version="$(ruby -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 --force --verbose --recursive \
    "${pkgdir}/usr/bin/"{bundle*,rake,rake-compiler,rspec,rdbg,rbs,typeprof,erb} \
    "${pkgdir}/usr/gems" \
    "${pkgdir}/usr/lib/bundler.rb" \
    "${pkgdir}/usr/lib/bundler" \
    "${pkgdir}/usr/specifications"

  install --verbose -D --mode=0644 ../operating_system.rb --target-directory "${pkgdir}/usr/lib/ruby/vendor_ruby/rubygems/defaults"
  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}"
}

# vim: tabstop=2 shiftwidth=2 expandtab:
