# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=edyn
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.3.1
pkgrel=1
pkgdesc='Edyn is a real-time physics engine organized as an ECS (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/xissburg/edyn'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-entt")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("${url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        '0001-edyn-1.3.1-meta-factory.patch')
sha256sums=('8b88224cd66092e4bf85fde9d0299dbba2b802f67784c45b3eff485564664f06'
            'd15deb6cd7ebb75fcfea675bc537ad1b91952137e8b880d1d2db152eaefc6e86')

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

  patch -p1 -i "${srcdir}"/0001-edyn-1.3.1-meta-factory.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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}-static"

  cmake --build "build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}-shared"

  cmake --build "build-${MSYSTEM}-shared"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}-shared"
}
