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

_realname=frontistr
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.6
pkgrel=3
pkgdesc='Open-Source Large-Scale Parallel FEM Program for Nonlinear Structural Analysis (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.frontistr.com"
msys2_repository_url="https://github.com/frontistr/frontistr"
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-metis"
         # "${MINGW_PACKAGE_PREFIX}-mumps" Couldn't be found by cmake!
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openblas")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-netcdf"
             $([[ ${CARCH} == aarch64 ]] || echo \
               "${MINGW_PACKAGE_PREFIX}-msmpi" \
               "${MINGW_PACKAGE_PREFIX}-parmetis" \
               "${MINGW_PACKAGE_PREFIX}-scalapack" \
               "${MINGW_PACKAGE_PREFIX}-trilinos")
             "${MINGW_PACKAGE_PREFIX}-seacas")
source=("https://github.com/${_realname}/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fix-build-with-flang.patch")
sha256sums=('e818698f61c4482151e7aa9d27cabfa14c8c1643a99511ec45659b883e78c742'
            'd13265d210a5263485f6100334a9faa95139c120a16ef4202cc65d2e0ffe23b4')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-fix-build-with-flang.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 [[ ${CARCH} != aarch64 ]]; then
    _extra_config+=("-DWITH_MPI=ON" "-DWITH_ML=ON")
  else
    _extra_config+=("-DWITH_MPI=OFF" "-DWITH_ML=OFF")
    CFLAGS+=" -Wno-implicit-function-declaration"
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    export FFLAGS="-fallow-invalid-boz"
  fi

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

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

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/License.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE 

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in $(find "${pkgdir}${MINGW_PREFIX}"/lib/cmake -type f); do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done
  
}
