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

# Before building package need CD into directory with drive letter, like
# cd /c/build/mingw-w64-ruby

# be sure to run the updpkgprovs and rebuild.  This is to get the propper version
# of the ruby gems that were bundled.

_realname=ruby
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.0.2
pkgrel=1
pkgdesc="An object-oriented language for quick and easy programming (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.ruby-lang.org/en"
license=("BSD, custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || \
             echo "${MINGW_PACKAGE_PREFIX}-graphviz"))
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gdbm"
         "${MINGW_PACKAGE_PREFIX}-libyaml"
         "${MINGW_PACKAGE_PREFIX}-libffi"
         "${MINGW_PACKAGE_PREFIX}-pdcurses"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-tk")
options=('staticlibs' 'strip')
source=("https://cache.ruby-lang.org/pub/ruby/${pkgver%.*}/${_realname}-${pkgver}.tar.gz"
        0001-mingw-w64-time-functions.patch
        0003-fix-check-types.patch
        0004-rbinstall-destdir.patch
        0005-rbinstall-rake-bash.patch
        0006-autoconf-set-msvcrt-ver-for-ucrt.patch)
## Populated by the updpkgprovs script
provides=(
    "${MINGW_PACKAGE_PREFIX}-ruby-minitest=5.14.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-power_assert=1.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-rake=13.0.3"
    "${MINGW_PACKAGE_PREFIX}-ruby-rbs=1.0.4"
    "${MINGW_PACKAGE_PREFIX}-ruby-rexml=3.2.5"
    "${MINGW_PACKAGE_PREFIX}-ruby-rss=0.2.9"
    "${MINGW_PACKAGE_PREFIX}-ruby-test-unit=3.3.7"
    "${MINGW_PACKAGE_PREFIX}-ruby-typeprof=0.12.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-abbrev=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-base64=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-benchmark=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-bigdecimal=3.0.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-bundler=2.2.22"
    "${MINGW_PACKAGE_PREFIX}-ruby-cgi=0.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-csv=3.1.9"
    "${MINGW_PACKAGE_PREFIX}-ruby-date=3.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-dbm=1.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-debug=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-delegate=0.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-did_you_mean=1.5.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-digest=3.0.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-drb=2.0.4"
    "${MINGW_PACKAGE_PREFIX}-ruby-english=0.7.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-erb=2.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-etc=1.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-fcntl=1.0.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-fiddle=1.0.6"
    "${MINGW_PACKAGE_PREFIX}-ruby-fileutils=1.5.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-find=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-forwardable=1.3.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-gdbm=2.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-getoptlong=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-io-console=0.5.7"
    "${MINGW_PACKAGE_PREFIX}-ruby-io-nonblock=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-io-wait=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-ipaddr=1.2.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-irb=1.3.5"
    "${MINGW_PACKAGE_PREFIX}-ruby-json=2.5.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-logger=1.4.3"
    "${MINGW_PACKAGE_PREFIX}-ruby-matrix=0.3.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-mutex_m=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-net-ftp=0.1.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-net-http=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-net-imap=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-net-pop=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-net-protocol=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-net-smtp=0.2.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-nkf=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-observer=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-open-uri=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-open3=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-openssl=2.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-optparse=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-ostruct=0.3.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-pathname=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-pp=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-prettyprint=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-prime=0.1.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-pstore=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-psych=3.3.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-racc=1.5.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-rdoc=6.3.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-readline-ext=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-readline=0.0.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-reline=0.2.5"
    "${MINGW_PACKAGE_PREFIX}-ruby-resolv-replace=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-resolv=0.2.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-rinda=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-securerandom=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-set=1.0.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-shellwords=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-singleton=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-stringio=3.0.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-strscan=3.0.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-tempfile=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-time=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-timeout=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-tmpdir=0.1.2"
    "${MINGW_PACKAGE_PREFIX}-ruby-tracer=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-tsort=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-un=0.1.0"
    "${MINGW_PACKAGE_PREFIX}-ruby-uri=0.10.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-weakref=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-win32ole=1.8.8"
    "${MINGW_PACKAGE_PREFIX}-ruby-yaml=0.1.1"
    "${MINGW_PACKAGE_PREFIX}-ruby-zlib=1.1.0"
)
## End of pkgupdprovs modifications

sha256sums=('5085dee0ad9f06996a8acec7ebea4a8735e6fac22f22e2d98c3f2bc3bef7e6f1'
            '329994d3bf7e692e18c1faffbedfbd076e5d00257b2100387a773b59b81ccd4e'
            'e5d665cabac8b7fbb0dda9a556c2350190801aa07b3cb90a0d50097ab99272a0'
            '02382ec3b9e42d7dbb58edad3e41c361d98871711bb2f0320082c2acc6a82e2e'
            '85e10228375eea2d80c8e6ebb582eeaf625d8216e4d08a4af8136de1aa3f095e'
            '66fa809eae07facf2f9e13e92fcde72a4147992e23b399212cc323322419a1bb')
noextract=(${_realname}-${pkgver}.tar.gz)

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

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


prepare() {
  [[ -d ${srcdir}/${_realname}-${pkgver} ]] && rm -rf ${srcdir}/${_realname}-${pkgver}
  tar -xzf ${srcdir}/${_realname}-${pkgver}.tar.gz -C ${srcdir} || true

  cd ${srcdir}/${_realname}-${pkgver}
  # Pthread disabled in ruby configure for mingw
  # 0001-mingw-w64-time-functions.patch
  apply_patch_with_msg \
      0003-fix-check-types.patch \
      0005-rbinstall-rake-bash.patch
    #  patch -p1 -i ${srcdir}/0004-rbinstall-destdir.patch

  # Set msvcrt version to 140 which brings hidden ucrt I/O structs
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || \
  [[ ${MINGW_PACKAGE_PREFIX} == *-ucrt-* ]]; then
    patch -Nbp1 -i "${srcdir}/0006-autoconf-set-msvcrt-ver-for-ucrt.patch"
  fi

  autoreconf -fi
}

build() {

  local FFI_INC=$(pkg-config --cflags libffi)
  CPPFLAGS+=" -DFD_SETSIZE=2048 ${FFI_INC} -I${MINGW_PREFIX}/include/pdcurses"
  CFLAGS+=" -I${MINGW_PREFIX}/include/pdcurses ${FFI_INC}"
  CXXFLAGS+=" -I${MINGW_PREFIX}/include/pdcurses ${FFI_INC}"

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CFLAGS+=" -Wno-unknown-attributes"
    CXXFLAGS+=" -Wno-unknown-attributes"
  fi

  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-werror
  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make test || true
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install-nodoc
  for script in {erb,gem,irb,rdoc,ri}; do
    install ${srcdir}/ruby-${pkgver}/bin/${script} \
      ${pkgdir}${MINGW_PREFIX}/bin/
  done
  local PREFIX_WIN
# fixup rake.bat so that it can be invoked from "cmd"
  PREFIX_WIN=$(cygpath -wm ${pkgdir}${MINGW_PREFIX})
  sed -e "s|\\\|/|g" -e "s|${MINGW_PREFIX}/bin/ruby.exe|ruby.exe|g" \
      -e "s|${PREFIX_WIN}/bin/rake|rake|g" -i "${pkgdir}${MINGW_PREFIX}/bin/rake.bat"

  install -D -m644 ${srcdir}/${_realname}-${pkgver}/COPYING \
     "${pkgdir}${MINGW_PREFIX}/share/licenses/ruby/LICENSE"
  install -D -m644 ${srcdir}/${_realname}-${pkgver}/BSDL \
     "${pkgdir}${MINGW_PREFIX}/share/licenses/ruby/BSDL"
}
