# Previous maintainer: Sarah Ottinger <schalaalexiazeal@gmail.com>
# Update to 4.3-stable and maintained by: Felix Xing (Felixaszx) felixxing3009@outlook.com
# Base on Arch Linux: https://gitlab.archlinux.org/archlinux/packaging/packages/godot/

_realname=godot
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=4.5
pkgrel=1
pkgdesc='An advanced, feature packed, multi-platform 2D and 3D game engine (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://godotengine.org/'
msys2_repository_url="https://github.com/godotengine/godot"
msys2_references=(
  "cpe: cpe:/a:godotengine:godot"
  "cpe: cpe:/a:godotengine:godot_engine"
)
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-scons")
depends=("${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-embree"
         "${MINGW_PACKAGE_PREFIX}-enet"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-glslang"
         "${MINGW_PACKAGE_PREFIX}-graphite2"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz-icu"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libogg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtheora"
         "${MINGW_PACKAGE_PREFIX}-libvorbis"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-wslay"
         "${MINGW_PACKAGE_PREFIX}-mbedtls"
         "${MINGW_PACKAGE_PREFIX}-openxr-sdk"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
optdepends=()
source=("https://github.com/godotengine/godot/releases/download/${pkgver}-stable/${_realname}-${pkgver}-stable.tar.xz"
        "002-enforcing-pkgconfig-for-syslib.patch")
sha256sums=('2cdb383d68bfe646c832064b9063310745e8db03a2e4b59f5946ee934b95c67d'
            '0bc46520d2028839954be478c2975de6f05ae67429a1cf8d6fb39ef5deb60ccb')

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

prepare() {
  cd ${_realname}-${pkgver}-stable

  apply_patch_with_msg \
    002-enforcing-pkgconfig-for-syslib.patch
}

build() {
  cd ${_realname}-${pkgver}-stable

  declare -a extra_config
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    extra_config+=("use_llvm=yes")
  fi

  if [[ ${CARCH} == aarch64 ]]; then
    extra_config+=("arch=arm64")
  else
    extra_config+=("arch=x86_64")
  fi

  scons \
    platform=windows \
    target=editor \
    optimize=speed\
    production=yes\
    use_mingw=yes\
    use_static_cpp=no\
    builtin_brotli=no\
    builtin_embree=no\
    builtin_enet=no\
    builtin_freetype=no\
    builtin_glslang=no\
    builtin_graphite=no\
    builtin_harfbuzz=no\
    builtin_icu4c=no\
    builtin_libogg=no\
    builtin_libpng=no\
    builtin_libtheora=no\
    builtin_libvorbis=no\
    builtin_libwebp=no\
    builtin_wslay=no\
    builtin_mbedtls=no\
    builtin_openxr=no\
    builtin_zlib=no\
    builtin_zstd=no\
    "${extra_config[@]}"
}

package() {
  cd ${_realname}-${pkgver}-stable
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    if [[ ${CARCH} == aarch64 ]]; then
      install -Dm755 bin/godot.windows.editor.arm64.llvm.exe "${pkgdir}${MINGW_PREFIX}/bin/godot.exe"
    else
      install -Dm755 bin/godot.windows.editor.x86_64.llvm.exe "${pkgdir}${MINGW_PREFIX}/bin/godot.exe"
    fi
  else
    install -Dm755 bin/godot.windows.editor.x86_64.exe "${pkgdir}${MINGW_PREFIX}/bin/godot.exe"
  fi
  install -Dm644 icon.svg "${pkgdir}${MINGW_PREFIX}/share/pixmaps/godot.svg"
  install -D -m644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/godot/LICENSE.txt"
  install -D -m644 "misc/dist/linux/godot.6" "${pkgdir}${MINGW_PREFIX}/share/man/man6/godot.6"
}

