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

_realname=supertuxkart
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.5
pkgrel=1
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}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsquish"
         "${MINGW_PACKAGE_PREFIX}-libvorbis"
         "${MINGW_PACKAGE_PREFIX}-mcpp"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-shaderc"
         "${MINGW_PACKAGE_PREFIX}-sqlite"
         "${MINGW_PACKAGE_PREFIX}-wiiuse"
         "${MINGW_PACKAGE_PREFIX}-zlib")
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.gz"
        "mingw-build.patch")
sha256sums=('33cf8841e4ff4082d80b9248014295bbbea61d14683e86dff100e3ab8f7b27cb'
            'd7787e83b0cdf073a5179886d80c6deeae91f2009a6c5ff607c3334672fad63a')

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

  patch -p1 -i ../mingw-build.patch
}

build() {
  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=" \
    cmake \
      -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[@]}" \
      -S SuperTuxKart-${pkgver}-src \
      -B build-${MSYSTEM}

  cmake --build build-${MSYSTEM}
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install build-${MSYSTEM}

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