# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=avogadrolibs
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-qt6")
pkgver=1.102.1
pkgrel=2
pkgdesc='Libraries that provide 3D rendering, visualization, analysis and data processing useful in computational chemistry, molecular modeling, bioinformatics, materials science, and related areas (mingw-w64)'
arch=(any)
mingw_arch=(ucrt64 clang64 clangarm64)
url='https://www.openchemistry.org/'
msys2_repository_url="https://github.com/OpenChemistry/avogadrolibs"
msys2_references=(
  'archlinux: avogadrolibs'
)
license=(spdx:BSD-3-Clause)
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             #"${MINGW_PACKAGE_PREFIX}-genxrdpattern"
             "${MINGW_PACKAGE_PREFIX}-glew"
             "${MINGW_PACKAGE_PREFIX}-hdf5"
             "${MINGW_PACKAGE_PREFIX}-jkqtplotter"
             "${MINGW_PACKAGE_PREFIX}-libarchive"
             "${MINGW_PACKAGE_PREFIX}-libmsym"
             "${MINGW_PACKAGE_PREFIX}-mmtf-cpp"
             "${MINGW_PACKAGE_PREFIX}-nlohmann-json"
             "${MINGW_PACKAGE_PREFIX}-pybind11"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-qt6-base"
             "${MINGW_PACKAGE_PREFIX}-qt6-svg"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "${MINGW_PACKAGE_PREFIX}-spglib"
             "${MINGW_PACKAGE_PREFIX}-vtk"
             "${MINGW_PACKAGE_PREFIX}-fast_float"
             "${MINGW_PACKAGE_PREFIX}-utf8cpp")
source=(https://github.com/OpenChemistry/avogadrolibs/archive/${pkgver}/${_realname}-${pkgver}.tar.gz
        https://github.com/OpenChemistry/crystals/archive/${pkgver}/crystals-${pkgver}.tar.gz
        https://github.com/OpenChemistry/fragments/archive/${pkgver}/fragments-${pkgver}.tar.gz
        https://github.com/OpenChemistry/molecules/archive/${pkgver}/molecules-${pkgver}.tar.gz
        003-fix-install-python-modules.patch
        005-fix-cmake-config-file.patch)
sha256sums=('cceae6c36d20382685aa7ea286f0f068fd83d25f5ab4047adbc69c215470361f'
            'aaf911f4a27d604c924ecef675cc53d90277ddb421908f7090b5bdb615b77f1d'
            '079dfe8a546242875e33f25af93ea0b632dba79e182682452a55f21f7231967a'
            '2499fcb84ab66c0f70ea1cb47c765c6e81d7d37e7800dde971d4d4c8fe314369'
            'a47e551f39ea1cff22dcea5c6bf5b9c3aba5f69038351794c171e26638f314e7'
            '38f452b157ed6f89f0ffc584066abd14fe62c75ad452970d3cdcfdc4aba390a9')

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

prepare() {
  for _dir in crystals fragments molecules; do
    mv ${_dir}-${pkgver} ${_realname}-${pkgver}/${_dir}
  done

  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    003-fix-install-python-modules.patch \
    005-fix-cmake-config-file.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

  CXXFLAGS+=" -D_USE_MATH_DEFINES -Wno-ignored-attributes" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    "${_extra_config[@]}" \
    -DUSE_HDF5=ON \
    -DUSE_LIBARCHIVE=ON \
    -DUSE_LIBMSYM=ON \
    -DUSE_MMTF=ON \
    -DUSE_PYTHON=ON \
    -DUSE_QT=ON \
    -DUSE_SPGLIB=ON \
    -DUSE_VTK=ON \
    -DUSE_EXTERNAL_NLOHMANN=ON \
    -DUSE_EXTERNAL_PUGIXML=ON \
    -DBUILD_STATIC_PLUGINS=OFF \
    -DQT_VERSION=6 \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -B build-${MSYSTEM} \
    -S ${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build build-${MSYSTEM}
}

package_avogadrolibs() {
  depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
           "${MINGW_PACKAGE_PREFIX}-glew"
           "${MINGW_PACKAGE_PREFIX}-hdf5"
           "${MINGW_PACKAGE_PREFIX}-pugixml"
           "${MINGW_PACKAGE_PREFIX}-spglib")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python: Python bindings"
              "${MINGW_PACKAGE_PREFIX}-avogadrolibs-qt6: For the VTK and Qt plugins")

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}

  rm -r "${pkgdir}"${MINGW_PREFIX}/bin/libAvogadroQt* \
        "${pkgdir}"${MINGW_PREFIX}/lib/libAvogadroQt* \
        "${pkgdir}"${MINGW_PREFIX}/bin/libAvogadroMoleQueue* \
        "${pkgdir}"${MINGW_PREFIX}/lib/libAvogadroMoleQueue* \
        "${pkgdir}"${MINGW_PREFIX}/bin/libAvogadroVtk* \
        "${pkgdir}"${MINGW_PREFIX}/lib/libAvogadroVtk* \
        "${pkgdir}"${MINGW_PREFIX}/include/avogadro/{molequeue,qt*,vtk} \
        "${pkgdir}"${MINGW_PREFIX}/lib/avogadro2/{scripts,plugins}

  for _dir in crystals fragments molecules; do
    cp -r ${_realname}-${pkgver}/${_dir} "${pkgdir}"${MINGW_PREFIX}/share/avogadro2/
  done

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

package_avogadrolibs-qt6() {
  pkgdesc="Qt6 modules for Avogadro (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-avogadrolibs"
           "${MINGW_PACKAGE_PREFIX}-cc-libs"
           "${MINGW_PACKAGE_PREFIX}-glew"
           "${MINGW_PACKAGE_PREFIX}-jkqtplotter"
           "${MINGW_PACKAGE_PREFIX}-libarchive"
           "${MINGW_PACKAGE_PREFIX}-libmsym"
           "${MINGW_PACKAGE_PREFIX}-qt6-base"
           "${MINGW_PACKAGE_PREFIX}-qt6-svg"
           "${MINGW_PACKAGE_PREFIX}-vtk")

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/avogadro/molequeue
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/avogadro/qtgui
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/avogadro/qtopengl
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/avogadro/qtplugins
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/avogadro/vtk

  for _dir in crystals fragments molecules; do
    rm -rf "${pkgdir}"${MINGW_PREFIX}/share/avogadro2/${_dir}
  done

  install -Dm644 ${_realname}-${pkgver}/LICENSE -t "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}-qt6
}

# 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;
