#!/usr/bin/bash
# JOBoRun		: Jwm OpenBox Obarun RUNit
# Maintainer	: Joe Bo Run <joborun@disroot.org>
# PkgSource		: url="https://gittea.disroot.org/joborun-pkg/jobextra/$pkgname"
# Website		: https://pozol.eu
#-----------------------------------------| DESCRIPTION |---------------------------------------

pkgbase=bullet
pkgname=('bullet' 'bullet-dp' 'bullet-docs' 'python-pybullet')
pkgver=3.25
_pkgver=3.25
pkgrel=01
pkgdesc="A 3D Collision Detection and Rigid Body Dynamics Library for games and animation"
url="http://www.bulletphysics.com/Bullet/"
makedepends=('cmake' 'doxygen' 'graphviz' 'ttf-dejavu' 'mesa' 'glu' 'python' 'python-numpy' 'python-setuptools' 'ninja')
#source=("$pkgname-$pkgver.tar.gz::https://github.com/bulletphysics/bullet3/archive/${_commit}.tar.gz"
source=("$pkgname-$pkgver.tar.gz::https://github.com/bulletphysics/bullet3/archive/refs/tags/${_pkgver}.tar.gz"
        bullet3_examplebrowser.sh)

prepare() {
  cd bullet3-${_pkgver}
  sed -i '/SET_TARGET_PROPERTIES(pybullet PROPERTIES PREFIX/d' examples/pybullet/CMakeLists.txt
}

build() {
  cp -r bullet3-${_pkgver} bullet3-${_pkgver}-dp

  export CXXFLAGS+=" -ffat-lto-objects"
  cd bullet3-${_pkgver}
  cmake \
      -Bbuild \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=/usr \
      -DBUILD_SHARED_LIBS=1 \
      -DINSTALL_LIBS=1 \
      -DINSTALL_EXTRA_LIBS=1 \
      -DBUILD_PYBULLET=ON \
      -DBUILD_PYBULLET_NUMPY=ON \
      -DBUILD_OPENGL3_DEMOS=ON \
      -DCMAKE_BUILD_TYPE=Release \
      -DBULLET2_MULTITHREADING=ON \
      -DCMAKE_SKIP_RPATH=YES
  ninja -C build

  # For Python and docs it doesn't matter whether we build multithreaded or not.
  python setup.py build
  doxygen

  cd ../bullet3-${_pkgver}-dp
  cmake \
      -Bbuild \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=/usr \
      -DBUILD_SHARED_LIBS=1 \
      -DINSTALL_LIBS=1 \
      -DINSTALL_EXTRA_LIBS=1 \
      -DBUILD_PYBULLET=ON \
      -DBUILD_PYBULLET_NUMPY=ON \
      -DBUILD_OPENGL3_DEMOS=ON \
      -DCMAKE_BUILD_TYPE=Release \
      -DBULLET2_MULTITHREADING=ON \
      -DUSE_DOUBLE_PRECISION=ON \
      -DCMAKE_SKIP_RPATH=YES
  ninja -C build
}

_package() {
  DESTDIR="${pkgdir}" ninja -C build install

  cd build

  install -Dm755 examples/ExampleBrowser/libBulletExampleBrowserLib.so.${pkgver} "${pkgdir}"/usr/lib/libBulletExampleBrowserLib.so.${pkgver}
  install -Dm755 examples/OpenGLWindow/libOpenGLWindow.so "${pkgdir}"/usr/lib/libOpenGLWindow.so
  install -Dm755 examples/ThirdPartyLibs/Gwen/libgwen.so "${pkgdir}"/usr/lib/libgwen.so
  install -Dm755 examples/ThirdPartyLibs/BussIK/libBussIK.so "${pkgdir}"/usr/lib/libBussIK.so
  install -Dm755 "${srcdir}"/bullet3_examplebrowser.sh "${pkgdir}"/usr/bin/bullet3_examplebrowser
  install -Dm755 examples/ExampleBrowser/App_ExampleBrowser "${pkgdir}"/opt/bullet/App_ExampleBrowser
  cp -r data "${pkgdir}"/opt/bullet/

  install -Dm644 ../LICENSE.txt "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}

package_bullet() {
  optdepends=('glu: for the example browser'
              'python: python bindings'
              'python-numpy: python bindings'
              'bullet-docs: documentation')

  cd bullet3-${_pkgver}

  _package
}

package_bullet-dp() {
  pkgdesc="A 3D Collision Detection and Rigid Body Dynamics Library for games and animation (double precision)"
  conflicts=("bullet")
  provides=("bullet")
  optdepends=('glu: for the example browser'
              'python: python bindings'
              'python-numpy: python bindings'
              'bullet-docs: documentation')

  cd bullet3-${_pkgver}-dp

  _package
}

package_python-pybullet() {
  pkgdesc="Bullet Python bindings"
  depends+=('bullet' 'gcc-libs')

  cd bullet3-${_pkgver}

  install -Dm755 build/examples/pybullet/libpybullet.so.${pkgver} "${pkgdir}"/usr/lib/libpybullet.so.${pkgver}
  python setup.py install --prefix=/usr --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE.txt "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}

package_bullet-docs() {
  pkgdesc="Documentation for bullet"

  cd bullet3-${_pkgver}

  # install docs
  install -Dm644 docs/GPU_rigidbody_using_OpenCL.pdf "${pkgdir}"/usr/share/doc/bullet/GPU_rigidbody_using_OpenCL.pdf
  install -Dm644 docs/Bullet_User_Manual.pdf "${pkgdir}"/usr/share/doc/bullet/Bullet_User_Manual.pdf
  install -Dm644 docs/BulletQuickstart.pdf "${pkgdir}"/usr/share/doc/bullet/BulletQuickstart.pdf
  cp -r html "${pkgdir}"/usr/share/doc/bullet/html
}

#---- arch license gpg-key & sha256sums ----

arch=(x86_64)

license=('custom:zlib')

sha256sums=(c45afb6399e3f68036ddb641c6bf6f552bf332d5ab6be62f7e6c54eda05ceb77  # bullet-3.25.tar.gz
	68ca940af489f7e9718fe9199cf0c3428c8e4d2b4ae1576847fffe56c0da50f7) # bullet3_examplebrowser.sh

sha512sums=('7086e5fcf69635801bb311261173cb8d173b712ca1bd78be03df48fad884674e85512861190e45a1a62d5627aaad65cde08c175c44a3be9afa410d3dfd5358d4'
             '8741ad94b6c46c226d89aebc8ab06d8a11bac3c04d3f0a2bf7a7524792a3375aa7bf7d295410b16fbeb4c348a31057b4570acdebe9bbaea251f44daca8d9fe81')

##  9ae6e93fc73cb04c71d96f49ad87ea0f461e4f23856960a3d32d6fcfd6c72c47  bullet-3.25-01-x86_64.pkg.tar.lz
##  1b9ef639a8bf054948544aafb649546859525f241d047f1330dd48d45a41c281  bullet-docs-3.25-01-x86_64.pkg.tar.lz
##  e76a299580309a015f130aa3e8729dc66605e3ca0d292d6491ea1b4036a10061  bullet-dp-3.25-01-x86_64.pkg.tar.lz
##  07692749619b6fd2f9df007fac5e7cdf163689a45bbe2736ecbda3ed8fc8cb40  python-pybullet-3.25-01-x86_64.pkg.tar.lz

