# Maintainer: Dominic Sisneros <dsisnero@gmail.com>

_realname=mruby
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.3.0
pkgrel=1
pkgdesc="mruby is the lightweight implementation of the Ruby language (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/mruby/mruby"
msys2_references=(
  "cpe: cpe:/a:mruby:mruby"
)
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-ruby")
source=("https://github.com/${_realname}/${_realname}/archive/${pkgver}/${_realname}-${pkgver}.zip"
        "0001-use-setjmp-for-clang.patch")
sha256sums=('12a2837a35ce2a48fd3d8fe25e38bae864c46df6c3e3228e2f00c72c42b06572'
            'a24c5551c32132ad37839cba5cf03f6c9a26cc9ea8e30dc18747a236fb287c8f')

prepare() {
  cd  "${srcdir}/${_realname}-${pkgver}"

  # https://github.com/msys2/MINGW-packages/commit/538dc50fcb3d8730a12091c50671b0a668c526c2#commitcomment-133486249
  patch -Np1 -i "${srcdir}/0001-use-setjmp-for-clang.patch"
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -rf "${srcdir}/${_realname}-${pkgver}" "${srcdir}"/build-${MSYSTEM}
  cd "${srcdir}"/build-${MSYSTEM}
  # touch to ensure bison is regenerated, to ensure it's working
  touch mrbgems/mruby-compiler/core/parse.y

  ./minirake -v
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  install -d "${pkgdir}${MINGW_PREFIX}"/bin

  # In build_config\default.rb file, enable_debug option is disabled for performance
  # https://github.com/mruby/mruby/commit/3c55cb089514d40ff17e9f29f9195c85111e0c10
  # cp build/host-debug/bin/* "${pkgdir}${MINGW_PREFIX}"/bin/ || return 1

  cp build/host/bin/* "${pkgdir}${MINGW_PREFIX}"/bin/ || return 1

  install -d "${pkgdir}${MINGW_PREFIX}"/lib
  cp build/host/lib/*.a "${pkgdir}${MINGW_PREFIX}"/lib/

  install -d "${pkgdir}${MINGW_PREFIX}"/include
  cp -r include/* "${pkgdir}${MINGW_PREFIX}"/include/
}
