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

_realname=newton-dynamics
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.02
pkgrel=1
pkgdesc='Newton dynamics is a realistic, cross-platform physics simulation library (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='http://newtondynamics.com/'
msys2_repository_url='https://github.com/MADEAPPS/newton-dynamics'
msys2_references=(
  'aur: newton-dynamics'
)
license=('spdx:Zlib')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-tinyxml")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
source=("https://github.com/MADEAPPS/newton-dynamics/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fix-arch-detection.patch"
        "002-fix-headers.patch"
        "003-fix-mingw-debug-flags.patch"
        "004-missing-ngBigVector-aarch64-ctor.patch")
sha256sums=('13050bc4eac34303ad3ff3bca104cc0ebfacc8551c98d02d4f8505cf9ecd9532'
            '79eaa87af1d79a6f9e5578b8fb9d79d64f1d96429876abeb7bcafd8d7c3fe643'
            '0206e659a021a44fa138b4ba29639f7cb2f220b092556c464f77e7350f887e22'
            '28fa7a1aa14081332d67b48f2bbeabcb7e31e5a048d30f587a1f5a7f47103d77'
            'fa7642c5859572c6732b0a778a595bbe0bedde6bee78a484354ec6707c95570f')

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

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}/newton-4.00

  apply_patch_with_msg \
    001-fix-arch-detection.patch \
    002-fix-headers.patch \
    003-fix-mingw-debug-flags.patch \
    004-missing-ngBigVector-aarch64-ctor.patch
}

build() {

  #Static Build
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE="Release"\
    -DBUILD_64=ON \
    -DNEWTON_BUILD_SHARED_LIBS=OFF \
    -DNEWTON_EXCLUDE_TEST=ON \
    -DNEWTON_DOUBLE_PRECISION=ON \
    -DNEWTON_ENABLE_AVX2_SOLVER=OFF \
    -DNEWTON_BUILD_SANDBOX_DEMOS=OFF \
    ../${_realname}-${pkgver}/newton-4.00

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

  #Shared Build
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE="Release"\
    -DBUILD_64=ON \
    -DNEWTON_BUILD_SHARED_LIBS=ON \
    -DNEWTON_EXCLUDE_TEST=ON \
    -DNEWTON_DOUBLE_PRECISION=ON \
    -DNEWTON_ENABLE_AVX2_SOLVER=OFF \
    -DNEWTON_BUILD_SANDBOX_DEMOS=OFF \
    ../${_realname}-${pkgver}/newton-4.00

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

package() {
  #Static Install
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" cmake --install .

  #Shared Install
  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" cmake --install .

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