# Maintainer: Rafal Brzegowy <rafal.brzegowy@yahoo.com>

_realname=moab
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.5.1
pkgrel=1
pkgdesc='The Mesh-Oriented datABase MOAB is a component for representing and evaluating mesh data (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64' )
url='https://sigma.mcs.anl.gov/moab-library/'
msys2_repository_url="https://bitbucket.org/fathomteam/moab"
license=('spdx:LGPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-openblas")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             $([[ "${CARCH}" != "x86_64" ]] || echo "${MINGW_PACKAGE_PREFIX}-msmpi"))
source=(https://web.cels.anl.gov/projects/sigma/downloads/${_realname}/${_realname}-${pkgver}.tar.gz
        "001-no-fortran.patch"
        "002-cmake-4.patch::https://api.bitbucket.org/2.0/repositories/fathomteam/moab/pullrequests/733/patch")
sha256sums=('67b6ed3a13c235cec16f60f8f46f9bf0371fd321cf36dea113d0e09f09d0d438'
            '2669507edebea057297ef7fb4e4ced4e5bc2ac350da78b24ac51a7bcb3559a94'
            'ec1ba96f395fd6e3eb4c34c2abca8e7e8aac2e989f459952b70a0194b3f5d14c')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  apply_patch_with_msg \
    001-no-fortran.patch \
    002-cmake-4.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    _extra_config+=("-DENABLE_FORTRAN=OFF")
  fi

  if [[ ${CARCH} != aarch64 ]]; then
    extra_config+=("-DENABLE_MPI=ON")
  fi 

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_SHARED_LIBS=OFF \
      -DENABLE_TESTING=OFF \
      "${_extra_config[@]}" \
      ../${_realname}-${pkgver}

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

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
