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

pkgname=('ruby' 'ruby-docs')
pkgver=2.7.2
pkgrel=1
arch=('i686' 'x86_64')
url='https://www.ruby-lang.org/en/'
license=('BSD' 'custom')
makedepends=('openssl-devel' 'libffi-devel' 'doxygen' 'libcrypt-devel'  'gmp-devel' 'libyaml-devel' 'libgdbm-devel' 'libiconv-devel' 'libreadline-devel' 'zlib-devel')
options=('!emptydirs' '!makeflags' 'staticlibs')
#libffi patch should be included because fiddle might contain the bundled version.
source=(https://cache.ruby-lang.org/pub/ruby/${pkgver%.*}/ruby-${pkgver//_/-}.tar.bz2
        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'
        'ruby-2.7.0-msys2.patch'
        'libffi-3.2.1-msysize.patch') 
sha256sums=('65a590313d244d48dc2ef9a9ad015dd8bc6faf821621bbb269aa7462829c75ed'
            '2f5eec5a7835b63fd1f0fb2af6a6a42e9bba1ee06fee35e4eafb0c5ad5fbfd03'
            '01a59cbfffa98eeca6b66f2f179474793dbb6ec32063d717a24e5ca03f28d767'
            '5fffe4fa469721bfe271650dae142c5a0274c22705e4be01541371d74a5de23d'
            'ee88cfec3610b21908a8c0725b43b4d1157ec394179489dd9c86309b1f663e4b'
            '4bb7eb2fe66e396ed16b589cdb656831407b39ad4e138d88536754c0448ac614'
            '0f9ad60795933258e4f653d54116083639f3c37ffc1339d8efaf7fae27c3ed71'
            'a700b3f7b77c39cb42a76581cdcb0d34b71daf20f9a222502b53f437c6f64800')
noextract=(ruby-${pkgver}.tar.bz2)

# 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() {
  [[ -d ${srcdir}/ruby-${pkgver} ]] && rm -rf ${srcdir}/ruby-${pkgver}
  tar -xjf ${srcdir}/ruby-${pkgver}.tar.bz2 -C ${srcdir} || true

  cd ${srcdir}/ruby-${pkgver//_/-}
  apply_patch_with_msg_p2 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 ruby-2.7.0-msys2.patch
  # This is just in case the build process the included libffi
  # It might not be ncessary but just in case.
  pushd ext/fiddle/libffi-3.2.1
    apply_patch_with_msg libffi-3.2.1-msysize.patch
    autoreconf -fiv
  popd
  
  #cp -f config.{sub,guess} tool/
  autoreconf -fi
}

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

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

check() {
  cd ruby-${pkgver//_/-}
  make test
}

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

  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}

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