# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=('ruby' 'ruby-docs')
pkgver=3.4.6
pkgrel=1
arch=('i686' 'x86_64')
url='https://www.ruby-lang.org/en/'
msys2_references=(
  "anitya: 4223"
  "cpe: cpe:/a:ruby-lang:ruby"
  "cpe: cpe:/a:yukihiro_matsumoto:ruby"
)
license=('BSD' 'custom')
makedepends=('openssl-devel' 'libffi-devel' 'doxygen' 'libxcrypt-devel'  'gmp-devel' 'libyaml-devel' 'libgdbm-devel' 'libiconv-devel' 'libreadline-devel' 'zlib-devel' 'autotools' 'gcc')
source=(https://cache.ruby-lang.org/pub/ruby/${pkgver%.*}/ruby-${pkgver//_/-}.tar.xz
        2.5.0-cygwin-configure.patch
        2.4.0-cygwin-rubygems.patch
        2.4.0-cygwin-compile.patch
        2.5.0-pkgconfig-version.patch
        gemrc
        2.7.7-msys2.patch
        3.1.3-rmdir-for-force-ln.patch
        3.4.1-win32-resolv-iphlpapi-header.patch)
sha256sums=('804995bc22938aa475127000d3103cb133409ad3955edfc0e7412be66a4859b8'
            '14bc6cb41d610d820a327bb9eeffb8c320f90dda1fdee1449a1b999de09dd1c9'
            'f1a084325c16fa045769d4b2c792052e9d377cb4d98bacf08c45eacd835d7b13'
            '5fffe4fa469721bfe271650dae142c5a0274c22705e4be01541371d74a5de23d'
            'ee88cfec3610b21908a8c0725b43b4d1157ec394179489dd9c86309b1f663e4b'
            '4bb7eb2fe66e396ed16b589cdb656831407b39ad4e138d88536754c0448ac614'
            'a4ec80d3da58969068112a26cc2a40eee0c6052ac61d789a84b6e5d74ef9246a'
            'd37409413b40e560a9d0e8977e711a6f47e7e92ef8dea8b85dee7e7784a47524'
            'd9494f491b031992d65eee7cfd78635882d56da602516654b3a77cf3dc19fe96')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  msg2 "Applying $1"
  patch -Np1 -i ${srcdir}/$1
}

apply_patch_with_msg_p2() {
  msg2 "Applying $1"
  patch -Np2 -i ${srcdir}/$1
}


del_file_exists() {
  for _fname in $@
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #


prepare() {
  cd ${srcdir}/ruby-${pkgver//_/-}
  apply_patch_with_msg 2.5.0-cygwin-configure.patch
  apply_patch_with_msg_p2 2.4.0-cygwin-rubygems.patch
  apply_patch_with_msg_p2 2.4.0-cygwin-compile.patch
  apply_patch_with_msg 2.5.0-pkgconfig-version.patch
  apply_patch_with_msg 2.7.7-msys2.patch

  # the script assumes that symlinks are files, but we deepcopy
  apply_patch_with_msg 3.1.3-rmdir-for-force-ln.patch
  apply_patch_with_msg 3.4.1-win32-resolv-iphlpapi-header.patch

  autoreconf -fi
}

build() {
  mkdir -p ${srcdir}/build-${CHOST} && cd ${srcdir}/build-${CHOST}

  export MSYSTEM=CYGWIN
  CFLAGS+=" -fno-strict-aliasing "
  CXXFLAGS+=" -fno-strict-aliasing "
  ../ruby-${pkgver//_/-}/configure \
    --build=${CHOST} \
    --prefix=/usr \
    --exec_prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --sharedstatedir=/var/lib \
    --libexecdir=/usr/lib/ruby \
    --enable-shared \
    --disable-rpath \
    --with-dbm-type=gdbm_compat \
    LDSHARED="gcc -shared" \
    DLDFLAGS="-Wl,--export-all-symbols"
  # -j2 due to excessive paging file usage
  make -j2
}

check() {
  cd ${srcdir}/build-${CHOST}
  make test
}

package_ruby() {
  pkgdesc='An object-oriented language for quick and easy programming'
  depends=('gcc-libs' 'libopenssl' 'libffi' 'libxcrypt'  'gmp' 'libyaml' 'libgdbm' 'libiconv' 'libreadline' 'zlib')
  optdepends=(#'tk: for Ruby/TK'
              'ruby-docs: Ruby documentation')
  provides=('rubygems' 'rake')
  conflicts=('rake')
  backup=('etc/gemrc')

  cd ${srcdir}/build-${CHOST}

  make DESTDIR=${pkgdir} install-nodoc

  install -D -m644 ${srcdir}/gemrc ${pkgdir}/etc/gemrc

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

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

  cd ${srcdir}/build-${CHOST}

  # html docs are missing without -j1
  make -j1 DESTDIR=${pkgdir} install-doc install-capi

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