# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=squirrel
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.2
pkgrel=2
pkgdesc="A light-weight scripting language that fits in the size, memory bandwidth, and real-time requirements of applications like video games (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='http://www.squirrel-lang.org'
msys2_repository_url="https://github.com/albertodemichelis/squirrel"
msys2_references=(
  "cpe: cpe:/a:squirrel-lang:squirrel"
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/albertodemichelis/squirrel/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "fix-version-number.patch::https://github.com/albertodemichelis/squirrel/commit/af69eb70844d5830c99d24e671352b2e5e0fe255.patch"
        "001-static-libraries-name.patch")
sha256sums=('02805414cfadd5bbb921891d3599b83375a40650abd6404a8ab407dc5e86a996'
            '97538b709ff71765e083a191c4f5eeccb38b603172a7917a94b1e58326173137'
            '751f372826078c782729314a03f0dca7d444ab45a7611c1a50499bce476e9b0c')

# Helper macros to help make tasks easier #
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() {
  cd "${srcdir}"/${_realname}-${pkgver}
  apply_patch_with_msg fix-version-number.patch
  apply_patch_with_msg 001-static-libraries-name.patch
}

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

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DLONG_OUTPUT_NAMES=OFF \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

#check() {
#  cd "${srcdir}"/build-${MSYSTEM}
#  ${MINGW_PREFIX}/bin/ctest
#}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYRIGHT \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
