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

_basename=chrono
_realname=project${_basename}
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=9.0.1
_mjver=2.7.9
pkgrel=6
pkgdesc='High-performance C++ library for multiphysics and multibody dynamics simulations (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url='https://projectchrono.org/'
msys2_repository_url='https://github.com/projectchrono/chrono'
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-eigen3"
         "${MINGW_PACKAGE_PREFIX}-irrlicht"
         "${MINGW_PACKAGE_PREFIX}-glm"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-glfw"
         "${MINGW_PACKAGE_PREFIX}-vulkanscenegraph"
         "${MINGW_PACKAGE_PREFIX}-vsgimgui"
         "${MINGW_PACKAGE_PREFIX}-vsgxchange"
         "${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-graphviz"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers")
source=("${msys2_repository_url}/archive/${pkgver}/${_basename}-${pkgver}.tar.gz"
        "https://github.com/mathjax/MathJax/archive/${_mjver}/MathJax-${_mjver}.tar.gz"
        '0001-chrono-9.0.1-fix-build.patch'
        '0002-chrono-9.0.1-not-install-irrlicht.patch'
        '0003-chrono-9.0.1-fix-python.patch'
        '0004-chrono-9.0.1-local-mathjax.patch'
        '0005-chrono-9.0.1-not-install-glew-and-glfw.patch'
        '0006-chrono-9.0.1-vsg-shadow-settings.patch')
sha256sums=('86da726ed3e3bacf682666b21d9c95dc87746b026dbafc722051a3202b822d39'
            'c9167279c87da044f2ff910ad573a02ce90354cb59440ae568eb86e1630f65df'
            '22e6a9521e790eee407551202418d4332068e95022c1bd8c8c5dd3eca2035e78'
            '5c5f70d2c134fc90f52484f36e84464f7e040e483a09cc81a425ff2589377477'
            '2874a51c3cdb3848440e47b37b8d94624a90f59e10a97ae96d3378707aa942cc'
            '230a216716facc7d0633b175b968a419816ca72b9225ac07613d6278216ae41f'
            '1a4a34516b1667eb219ee17f15ac8359348874956a3f2094d70668f0cc0d573c'
            '56c0aa5fa0e2fe7b43026288cfce5c92afb83181bcd57445482481cfd6c8ca81')

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

  patch -Np1 -i "${srcdir}"/0001-chrono-9.0.1-fix-build.patch
  patch -Np1 -i "${srcdir}"/0002-chrono-9.0.1-not-install-irrlicht.patch
  patch -Np1 -i "${srcdir}"/0003-chrono-9.0.1-fix-python.patch
  patch -Np1 -i "${srcdir}"/0004-chrono-9.0.1-local-mathjax.patch
  patch -Np1 -i "${srcdir}"/0005-chrono-9.0.1-not-install-glew-and-glfw.patch
  # https://github.com/msys2/MINGW-packages/pull/22581#discussion_r1852320907
  patch -Np1 -i "${srcdir}"/0006-chrono-9.0.1-vsg-shadow-settings.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,STATIC}_LIBS=ON \
      -DENABLE_MODULE_IRRLICHT=ON \
      -DENABLE_MODULE_OPENGL=ON \
      -DENABLE_MODULE_VSG=ON \
      -DENABLE_MODULE_PYTHON=ON \
      -DIRRLICHT_INSTALL_DIR="${MINGW_PREFIX}" \
      -DBUILD_TESTING=ON \
      -S "${_basename}-${pkgver}" \
      -B "build-${MSYSTEM}"

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

  cd "${srcdir}/${_basename}-${pkgver}/"doxygen
  doxygen Doxyfile
}

check() {
  cmake --build "build-${MSYSTEM}" --target test
}

package_projectchrono() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

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

package_projectchrono-docs() {
  pkgdesc+=" (Documentation)"
  depends=()

  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html/"
  cp -r -t "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html/" build-doxygen/html/*

  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/mathjax/"
  cp -r -t "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/mathjax/" MathJax-${_mjver}/*
}
# 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
