# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Antonio Rojas <arojas@archlinux.org>

pkgbase=avogadrolibs
pkgname=(avogadrolibs
         avogadrolibs-qt5)
pkgver=1.99.0
pkgrel=6
pkgdesc='Libraries that provide 3D rendering, visualization, analysis and data processing useful in computational chemistry, molecular modeling, bioinformatics, materials science, and related areas'
arch=(x86_64 powerpc64le powerpc64 powerpc riscv64)
url='https://www.openchemistry.org/'
license=(custom)
makedepends=(boost
             cmake
             eigen
             fast_float
             fmt
             git
             hdf5
             genxrdpattern
             glew
             libmsym
             mmtf-cpp
             nlohmann-json
             openmpi
             pybind11
             python
             qt5-svg
             qt5-tools
             qt6-5compat
             qt6-declarative
             qt6-svg
             qt6-tools
             spglib
             tbb
             utf8cpp
             verdict
             vtk)
source=(git+https://github.com/OpenChemistry/avogadrolibs#tag=$pkgver)
sha256sums=('ad26ef7d887924a867c5aba68ecc3803b2684652d65a442516903c9343a332ff')

prepare() {
  mkdir crystals fragments molecules # Dummy dirs to trick cmake, actually provided by avogadro-{crystals,fragments,molecules}
  cd $pkgname
  git cherry-pick -n 6e2e84dbb088a40d69117c1836f4306792f57acd # Fix wrong cmake variable name
}

build() {
  case "${CARCH}" in
    powerpc64)
      export CFLAGS='-O2 -pipe'
      export CXXFLAGS='-O2 -pipe'
    ;;
  esac

  cmake -B build -S $pkgname \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_C_FLAGS="$CFLAGS -ffat-lto-objects" \
    -DCMAKE_CXX_FLAGS="$CXXFLAGS -ffat-lto-objects" \
    -DENABLE_TRANSLATIONS=ON \
    -DUSE_HDF5=ON \
    -DUSE_VTK=ON \
    -DUSE_PYTHON=ON \
    -DQT_VERSION=6 \
    -DUSE_SYSTEM_GENXRDPATTERN=ON \
    -DUSE_EXTERNAL_NLOHMANN=ON \
    -DUSE_EXTERNAL_PUGIXML=ON
  cmake --build build
}

package_avogadrolibs() {
  depends=(avogadro-crystals
           avogadro-fragments
           avogadro-molecules
           gcc-libs
           glew
           glibc
           hdf5
           libglvnd
           molequeue
           pugixml
           spglib
           verdict)
  optdepends=('avogadrolibs-qt5: For the VTK and Qt plugins')

  DESTDIR="$pkgdir" cmake --install build
  rm -r "$pkgdir"/usr/lib/libAvogadroQt* \
        "$pkgdir"/usr/lib/libAvogadroMoleQueue* \
        "$pkgdir"/usr/lib/libAvogadroVtk* \
        "$pkgdir"/usr/include/avogadro/{molequeue,qt*,vtk} \
        "$pkgdir"/usr/lib/avogadro2/{scripts,staticplugins}

  install -Dm644 $pkgname/LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname
}

package_avogadrolibs-qt5() {
  pkgdesc="Qt 5 modules for Avogadro"
  depends=(avogadrolibs
           fmt # needed by VTK dependencies
           gcc-libs
           glew
           glibc
           libarchive
           libglvnd
           libmsym
           libxcursor # needed by VTK dependencies
           openmpi # needed by VTK dependencies
           qt5-base
           qt5-svg
           vtk)

  DESTDIR="$pkgdir" cmake --install build/avogadro/molequeue
  DESTDIR="$pkgdir" cmake --install build/avogadro/qtgui
  DESTDIR="$pkgdir" cmake --install build/avogadro/qtopengl
  DESTDIR="$pkgdir" cmake --install build/avogadro/qtplugins
  DESTDIR="$pkgdir" cmake --install build/avogadro/vtk

  install -Dm644 $pkgbase/LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname
}
