# Maintainer: ImperatorS79 <fevrier.simon@gmail.com>

_realname=jolt-physics
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.3.0
pkgrel=1
pkgdesc="A multi core friendly rigid body physics and collision detection library, written in C++, suitable for games and VR applications. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/jrouwe/JoltPhysics"
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("http://github.com/jrouwe/JoltPhysics/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('e7f9621e480646c434150e1fbe3a9410f4ec4b04ffe54791e0678326b741b918')

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" "-DGENERATE_DEBUG_SYMBOLS=OFF")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug" "-DGENERATE_DEBUG_SYMBOLS=ON")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    _extra_config+=("-DINTERPROCEDURAL_OPTIMIZATION=OFF")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DENABLE_ALL_WARNINGS=OFF \
      -DDOUBLE_PRECISION=OFF \
      -DCROSS_PLATFORM_DETERMINISTIC=ON \
      -DUSE_STATIC_MSVC_RUNTIME_LIBRARY=OFF \
      -DOBJECT_LAYER_BITS=16 \
      -DUSE_AVX512=OFF \
      -DBUILD_SHARED_LIBS=ON \
      -DTARGET_UNIT_TESTS=OFF \
      -DTARGET_HELLO_WORLD=ON \
      -DTARGET_PERFORMANCE_TEST=ON \
      -DTARGET_SAMPLES=ON \
      -DTARGET_VIEWER=ON \
      ../JoltPhysics-${pkgver}/Build

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

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  "${MINGW_PREFIX}"/bin/cmake.exe -DTARGET_UNIT_TESTS=ON ../JoltPhysics-${pkgver}/Build
  "${MINGW_PREFIX}"/bin/cmake.exe --build .

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

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

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

  install -Dm644 "${srcdir}/JoltPhysics-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  install -Dm755 "${srcdir}/build-${MSYSTEM}/JoltViewer.exe" "${pkgdir}${MINGW_PREFIX}/bin/JoltViewer.exe"
  install -Dm755 "${srcdir}/build-${MSYSTEM}/Samples.exe" "${pkgdir}${MINGW_PREFIX}/share/doc/jolt-physics/Samples.exe"
  cp -r "${srcdir}/JoltPhysics-${pkgver}/Assets/" "${pkgdir}${MINGW_PREFIX}/share/doc/jolt-physics/Assets/"
}
