# Maintainer: Quinton Miller <nicetas.c@gmail.com>

_bootstrap=1
_realname=crystal
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.18.0
pkgrel=1
pkgdesc="Fast and statically typed, compiled language with Ruby-like syntax (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'mingw64' 'clang64' 'clangarm64')
url="https://crystal-lang.org/"
msys2_repository_url="https://github.com/crystal-lang/crystal"
msys2_documentation_url="https://crystal-lang.org/reference/${pkgver%.*}/"
msys2_changelog_url="https://github.com/crystal-lang/crystal/releases/tag/${pkgver}"
msys2_references=(
  "archlinux: crystal"
)
license=('spdx:Apache-2.0 WITH Swift-exception')
depends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-gc"
  "${MINGW_PACKAGE_PREFIX}-pcre2"
  "${MINGW_PACKAGE_PREFIX}-gmp"
  "${MINGW_PACKAGE_PREFIX}-libffi"
  "${MINGW_PACKAGE_PREFIX}-libiconv"
  "${MINGW_PACKAGE_PREFIX}-libxml2"
  "${MINGW_PACKAGE_PREFIX}-libyaml"
  "${MINGW_PACKAGE_PREFIX}-llvm-libs"
  "${MINGW_PACKAGE_PREFIX}-openssl"
  "${MINGW_PACKAGE_PREFIX}-zlib"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-llvm"
  $( (( _bootstrap )) || echo "${MINGW_PACKAGE_PREFIX}-crystal")
  "git"
)
checkdepends=(
  "${MINGW_PACKAGE_PREFIX}-lld" # needed for linking std_spec
)
source=("${_realname}::git+${msys2_repository_url}.git#tag=${pkgver}")
sha256sums=('bf3e91b56b438098faa2d0cb8adfe6fc79d9d241254aa09e287320a2d0037a2e')

if (( _bootstrap )); then
  # stage 0 compiler
  if [[ ${MSYSTEM} == CLANGARM64 ]]; then
    source+=("https://github.com/crystal-lang/crystal/releases/download/1.17.0/crystal-1.17.0-windows-aarch64-gnu-unsupported.zip")
    sha256sums+=('76064ce2e24085d568e57e8987c56c38bbbe52b0e29b0015bc9bcdd35807eb9f')
  else
    source+=("https://github.com/crystal-lang/crystal/releases/download/1.15.0/crystal-1.15.0-windows-x86_64-gnu-unsupported.zip")
    sha256sums+=('7d7df1b4a99cb3f938106ba45e0b55ce1d9913491999f1acaa57da168a18becb')
  fi
fi

prepare() {
  cd "${_realname}"

  if test true != "$(git config core.symlinks)"; then
    git config core.symlinks true &&
    MSYS='winsymlinks:nativestrict' git restore --source=HEAD :/
  fi
}

build() {
  cd "${_realname}"
  if (( _bootstrap )); then
    CRYSTAL="$srcdir/bin/crystal.exe" make interpreter=1 release=1
  else
    make interpreter=1 release=1
  fi
}

check() {
  cd "${_realname}"
  # the full suite is run on the crystal-lang/crystal repo's own CI
  CRYSTAL_SPEC_COMPILER_BIN="$(pwd)/.build/crystal.exe" make std_spec
}

package() {
  cd "${_realname}"
  make install DESTDIR="$pkgdir" PREFIX="${MINGW_PREFIX}" deref_symlinks=1
}
