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

_realname=avogadroapp
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.102.1
pkgrel=1
pkgdesc='Advanced molecular editor designed for cross-platform use 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/avogadroapp"
license=(spdx:BSD-3-Clause)
depends=("${MINGW_PACKAGE_PREFIX}-avogadrolibs"
         "${MINGW_PACKAGE_PREFIX}-avogadrolibs-qt6"
         "${MINGW_PACKAGE_PREFIX}-molequeue"
         "${MINGW_PACKAGE_PREFIX}-qt6-5compat"
         "${MINGW_PACKAGE_PREFIX}-qt6-base")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-vtk"
             "${MINGW_PACKAGE_PREFIX}-fast_float"
             "${MINGW_PACKAGE_PREFIX}-nlohmann-json"
             "${MINGW_PACKAGE_PREFIX}-utf8cpp")
source=(https://github.com/OpenChemistry/avogadroapp/archive/${pkgver}/${_realname}-${pkgver}.tar.gz
        https://github.com/OpenChemistry/avogadro-i18n/archive/${pkgver}/avogadro-i18n-${pkgver}.tar.gz
        001-fix-build-on-mingw.patch)
sha256sums=('d5867376ee1a8cf06672178706615599c5968fe887871fb2001a2052b4b50882'
            'a842d258e47c8a9850e663cad4cc5c1b6a18565f0cc8a731d99588d4e5bc3e48'
            'dd4f67e8f80ca96f024a142831ef1ba5cd6c57dca6590230a44b9268e6302508')

prepare() {
  mv avogadro-i18n-${pkgver} avogadro-i18n

  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-fix-build-on-mingw.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=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    "${_extra_config[@]}" \
    -DAvogadro_ENABLE_RPC=OFF \
    -DUSE_VTK=ON \
    -DQT_VERSION=6 \
    -B build-${MSYSTEM} \
    -S ${_realname}-${pkgver}

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

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

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