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

_realname=newton-dynamics
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.14c
pkgrel=1
pkgdesc='Newton dynamics is a realistic, cross-platform physics simulation library. '
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-tinyxml")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
license=('zlib')
url='http://newtondynamics.com/'
source=("https://github.com/MADEAPPS/newton-dynamics/archive/v${pkgver}.tar.gz"
        "0001-fix-arch-detection.patch"
        "0002-use-msys-tinyxml.patch")
sha256sums=('042342e021a429f4b689bf7aa2ed5b6d4b9b7abcde0eea57daa5873736073d22'
            'SKIP'
            'SKIP')
		 
prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/0001-fix-arch-detection.patch
  patch -p1 -i "${srcdir}"/0002-use-msys-tinyxml.patch
}

build() {
  #Static Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-static" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-static"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-static" && cd "${srcdir}/build-${MINGW_CHOST}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE="Release"\
    -G"MSYS Makefiles" \
    -DNEWTON_DOUBLE_PRECISION=ON \
    -DNEWTON_BUILD_SHARED_LIBS=OFF \
    -DNEWTON_WITH_AVX_PLUGIN=OFF \
    -DNEWTON_WITH_REFERENCE_GPU_PLUGIN=OFF \
    -DNEWTON_BUILD_SANDBOX_DEMOS=OFF \
    ../${_realname}-${pkgver}
	
    ${MINGW_PREFIX}/bin/cmake --build .

  #Shared Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-shared" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-shared"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-shared" && cd "${srcdir}/build-${MINGW_CHOST}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE="Release"\
    -G"MSYS Makefiles" \
    -DNEWTON_DOUBLE_PRECISION=ON \
    -DNEWTON_BUILD_SHARED_LIBS=ON \
    -DNEWTON_BUILD_SANDBOX_DEMOS=OFF \
    ../${_realname}-${pkgver}
	
    ${MINGW_PREFIX}/bin/cmake --build .
}

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