# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=bullet
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-dp"
         "${MINGW_PACKAGE_PREFIX}-python-pybullet")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
pkgver=3.25
pkgrel=7
pkgdesc="A 3D Collision Detection and Rigid Body Dynamics Library for games and animation (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.bulletphysics.com/Bullet/"
msys2_repository_url="https://github.com/bulletphysics/bullet3"
license=('spdx:Zlib')
install=${_realname}-${MSYSTEM}.install
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-freeglut"
             "${MINGW_PACKAGE_PREFIX}-openvr"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-numpy"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=(!emptydirs)
source=("https://github.com/bulletphysics/bullet3/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        mingw-allow-shared-builds.patch
        fix-runtime-install.patch
        install-openvr-only-msvc.patch
        0004-no-glad.patch
        0005-robotsimulator.patch
        0006-pybullet.patch
        0007-twojoint.patch
        0008-bulletrobotics.patch
        0009-bulletroboticsgui.patch
        )
sha256sums=('c45afb6399e3f68036ddb641c6bf6f552bf332d5ab6be62f7e6c54eda05ceb77'
            '10b99560cc5e36a68dfff2d06d962fd7bbc02c728046e51f2a839ab9e1c4172b'
            '942fb30ef816428f009fbde7ef955d6f23fb5d69ccb267d043f3efff8f43290c'
            'bc089e5ffac4b0d902a0b2fea9db8645ab385ee62ba82a3d739b00c272ed45fb'
            '6925e0cea09588910daa066ad25da767158751e893f96f1f9d99bdca7c826ce1'
            'e55cb628baa0929c2557af8c8da8e4857ecc1d584b8b30edcf2057af32342d5b'
            'bf127f379a01f5300885303e5c6a70de5f9476d25b6ecf118f7b85c830418d2d'
            '49c66a34165e1fc984ed5c3588e01493fa8d6b50061e1dbf907a826ab64dfa8b'
            'e9e00bb15300d15df398a8951cc6a32963722b31114ffdb32c41e879ffeee22d'
            'ae0949043cdeee9156e583b72575345b098da7ac0679ce43ae77d95f5c33e37f')

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

prepare () {
  cd "${srcdir}"/bullet3-${pkgver}

  apply_patch_with_msg \
    mingw-allow-shared-builds.patch \
    fix-runtime-install.patch \
    install-openvr-only-msvc.patch \
    0004-no-glad.patch \
    0005-robotsimulator.patch \
    0006-pybullet.patch \
    0007-twojoint.patch \
    0008-bulletrobotics.patch \
    0009-bulletroboticsgui.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

  CFLAGS+=" -Wno-incompatible-pointer-types"
  CXXFLAGS+=" -Wno-int-to-pointer-cast"

  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_EXTRAS=ON \
    -DINSTALL_LIBS=ON \
    -DINSTALL_EXTRA_LIBS=ON \
    -DUSE_DOUBLE_PRECISION=OFF \
    -DUSE_GLUT=ON \
    -DCMAKE_CXX_FLAGS_RELEASE="-fpermissive" \
    -DBUILD_PYBULLET=ON \
    -DBUILD_PYBULLET_NUMPY=ON \
    -DBUILD_UNIT_TESTS=OFF \
    -DBUILD_CPU_DEMOS=OFF \
    "${_extra_config}" \
    ../bullet3-${pkgver}

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

  mkdir -p "${srcdir}/build-${MSYSTEM}-dp" && cd "${srcdir}/build-${MSYSTEM}-dp"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_EXTRAS=ON \
    -DINSTALL_LIBS=ON \
    -DINSTALL_EXTRA_LIBS=ON \
    -DUSE_DOUBLE_PRECISION=ON \
    -DUSE_GLUT=ON \
    -DCMAKE_CXX_FLAGS_RELEASE="-fpermissive" \
    -DBUILD_UNIT_TESTS=OFF \
    -DBUILD_CPU_DEMOS=OFF \
    "${_extra_config}" \
    ../bullet3-${pkgver}

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

package_bullet() {
  depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
           "${MINGW_PACKAGE_PREFIX}-freeglut"
           "${MINGW_PACKAGE_PREFIX}-openvr")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python-pybullet=${pkgver}-${pkgrel}: python binding")

  cd "${srcdir}/build-${MSYSTEM}"

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

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/bullet
  for f in $(find examples -type f -iname *.dll -o -iname *.exe); do
    cp ${f} "${pkgdir}"${MINGW_PREFIX}/share/bullet/
  done
  cp -rf "${srcdir}"/bullet3-${pkgver}/data/* "${pkgdir}"${MINGW_PREFIX}/share/bullet/
}

package_bullet-dp() {
  pkgdesc="A 3D Collision Detection and Rigid Body Dynamics Library for games and animation (double-precision) (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
           "${MINGW_PACKAGE_PREFIX}-freeglut"
           "${MINGW_PACKAGE_PREFIX}-openvr")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  cd "${srcdir}/build-${MSYSTEM}-dp"

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

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/bullet
  for f in $(find examples -type f -iname *.dll -o -iname *.exe); do
    cp ${f} "${pkgdir}"${MINGW_PREFIX}/share/bullet/
  done
  cp -rf "${srcdir}"/bullet3-${pkgver}/data/* "${pkgdir}"${MINGW_PREFIX}/share/bullet/
}

package_python-pybullet() {
  pkgdesc="Bullet Python bindings (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-python-numpy")

  cd "${srcdir}/build-${MSYSTEM}"

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

  MSYS2_ARG_CONV_EXCL="-p" python -m compileall \
    -o 0 -o 1 -q -s"${pkgdir}" -p"/" "${pkgdir}${MINGW_PREFIX}/lib/python"*
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
