# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=supertuxkart
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.4
pkgrel=2
pkgdesc="Kart racing game featuring Tux and his friends (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://supertuxkart.net'
msys2_repository_url="https://github.com/supertuxkart/stk-code"
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-libvorbis"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-sqlite"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-mcpp"
         "${MINGW_PACKAGE_PREFIX}-wiiuse"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsquish"
         "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-shaderc"
         "${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-lld"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-egl-headers")
source=("https://github.com/supertuxkart/stk-code/releases/download/${pkgver}/SuperTuxKart-${pkgver}-src.tar.xz"
        "mingw-build.patch"
        "add-missing-header.patch::https://github.com/supertuxkart/stk-code/commit/0163e3fa88b72634c3ddff5304c9086b649f53b1.patch"
        "add-missing-header2.patch::https://github.com/supertuxkart/stk-code/commit/27eb0f3116921492e183ad3aa685ddb147ed7183.patch"
        "fix-missing-rotation.patch::https://github.com/supertuxkart/stk-code/commit/0c2b81ac1f9ff29f5012a98f530880b87f416337.patch"
        "${_realname}-${pkgver}-fix-crash.patch::https://github.com/supertuxkart/stk-code/commit/8544f19b59208ae93fc3db0cf41bd386c6aefbcb.patch"
        "require-cmake-3.6.patch::https://github.com/supertuxkart/stk-code/commit/7d4e8433c124c08c62f5335e2a884aeea71cf184.patch")
sha256sums=('9890392419baf4715313f14d5ad60746f276eed36eb580636caf44e2532c0f03'
            '8a88bf3e9fcfc2204e6ce33686edcaca99660e1a3b15ccaef8e7bbc1fcf003b3'
            '2b7d656c8daf34ea13bb65164e86619793076e410b4435bf2bd145f4d9df6617'
            '8e5054c6345af9baa7ae97483ce91d20a9095941b1a60d663ce2ef74a0c70343'
            '56a97314b89617eb8659a4164ec8ccc5a961c8a743bc99ba4af9a272f0e42202'
            'f27b4156d6052a0e23ae958788531458a713bb566fd2d563854f03034b8fbbe9'
            'c32425c14acea9b75fdc08bb1c58de237c95cfd1d154455297fbdda6424f7add')

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

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

  apply_patch_with_msg \
    "mingw-build.patch" \
    "add-missing-header.patch" \
    "add-missing-header2.patch" \
    "fix-missing-rotation.patch" \
    "${_realname}-${pkgver}-fix-crash.patch" \
    "require-cmake-3.6.patch"
}

build() {
  cd "${srcdir}/SuperTuxKart-${pkgver}-src"
  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

  export CFLAGS+=" -fuse-ld=lld"
  export CXXFLAGS+=" -fuse-ld=lld"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_C_COMPILER=clang \
      -DCMAKE_CXX_COMPILER=clang++ \
      -DUSE_SYSTEM_WIIUSE=ON \
      -DBUILD_RECORDER=OFF \
      -Wno-dev \
      "${extra_config[@]}" \
      ../SuperTuxKart-${pkgver}-src

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

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  # this shouldn't be installed
  rm -rf "${pkgdir}${MINGW_PREFIX}/lib"
  rm -rf "${pkgdir}${MINGW_PREFIX}/include"
  install -Dm644 "${srcdir}/SuperTuxKart-${pkgver}-src/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
