# Maintainer: Rafał Brzegowy <rafal.brzegowy@yahoo.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=precice
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.3.0
pkgrel=2
pkgdesc='A Coupling Library for Partitioned Multi-Physics Simulations on Massively Parallel Systems (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.precice.org'
msys2_repository_url='https://github.com/precice/precice'
license=('spdx:LGPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-boost-libs"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         #"${MINGW_PACKAGE_PREFIX}-petsc"
         "${MINGW_PACKAGE_PREFIX}-ginkgo"
         "${MINGW_PACKAGE_PREFIX}-kokkos"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-numpy")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             $([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-msmpi")
             #"${MINGW_PACKAGE_PREFIX}-ninja"
             #"${MINGW_PACKAGE_PREFIX}-petsc-build"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "git")
source=(https://github.com/precice/precice/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz
        001-fix-libprecice-dll-installation.patch)
sha256sums=('300df9dbaec066c1d0f93f2dbf055705110d297bca23fc0f20a99847a55a24f4'
            '49fe04f9e72d1034b8909a7b593ccf42eeaf5203d9188f71706260948a0fb566')

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

prepare() {
  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    001-fix-libprecice-dll-installation.patch
}

build() {
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # . ${MINGW_PREFIX}/src/petsc/petsc
  # for build in ${petsc_builds}; do
  #   #export PETSC_ARCH=${build}
  #   export PETSC_ARCH=dmo
  #   export PETSC_DIR=${MINGW_PREFIX}/src/petsc
  # done

  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 [[ ${CARCH} == aarch64 ]]; then
    extra_config+=("-DPRECICE_FEATURE_MPI_COMMUNICATION=OFF")
  else
    extra_config+=("-DPRECICE_FEATURE_MPI_COMMUNICATION=ON")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CXXFLAGS+=" -Wno-c++11-narrowing"
  else
    CXXFLAGS+=" -Wno-narrowing"
  fi

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G"Ninja" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_SHARED_LIBS=ON \
      -DBUILD_TESTING=OFF \
      ${extra_config[@]} \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      -DPRECICE_FEATURE_PETSC_MAPPING=OFF \
      -DPRECICE_FEATURE_GINKGO_MAPPING=ON \
      -DPRECICE_FEATURE_PYTHON_ACTIONS=ON \
      -DPRECICE_BUILD_TOOLS=ON \
      -Wno-dev \
      ../${_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
}
