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

_realname=elmerfem
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=9.0
pkgrel=1
pkgdesc="Finite element software for multiphysical problems (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64')
url='https://www.elmerfem.org/'
msys2_repository_url="https://github.com/ElmerCSC/elmerfem"
msys2_references=(
  'aur: elmerfem'
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-arpack"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-qt5-script"
         "${MINGW_PACKAGE_PREFIX}-qwt-qt5")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
source=("https://github.com/ElmerCSC/elmerfem/archive/release-${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fix-build-with-qwt-6.2.patch"
        "002-fix-build-with-newer-qt5.patch::https://github.com/ElmerCSC/elmerfem/commit/e057b0d4.patch"
        "003-Add-local-variable-i.patch::https://github.com/ElmerCSC/elmerfem/commit/96a33930.patch"
        "004-Fix-the-evaluation-of-tensor-components.patch::https://github.com/ElmerCSC/elmerfem/commit/a28b3521.patch"
        "005-add-i-to-local-scope.patch::https://github.com/ElmerCSC/elmerfem/commit/8f9f2c70.patch"
        "006-fix-some-compile-errors-in-DCRComplexSolve.patch::https://github.com/ElmerCSC/elmerfem/commit/54fd8705.patch")
sha256sums=('08c5bf261e87ff37456c1aa0372db3c83efabe4473ea3ea0b8ec66f5944d1aa0'
            '6d6f8847b572f08b85105ed9f816a483e24e4de8c2c3af6b2970de63a924cfa0'
            '777d8e4994e4acc75fac2d1db0aa36a6bb5d60b30273de8dd8226ffe04355f08'
            'd9feab19b4a10aa507ae3b95d20d8574f73337f8fb7cd113b0e6ee2a3b0135c1'
            '13e0a74ca35879286c3399c8f1c84e6e1c4037a78e2fd86d5d91ea1f9255ef5f'
            'ee42f42440eea666336426903d0b5d124b87934fe6ecb298e34fa50408501f22'
            '4a77bf1536ff2e2d17166e94492a1f94b840d6633ad3986f593f12a9bce24b37')

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

prepare() {
  cd ${_realname}-release-${pkgver}
  # https://github.com/ElmerCSC/elmerfem/commit/48e9430c
  _apply_patch_with_msg \
    001-fix-build-with-qwt-6.2.patch

  _apply_patch_with_msg \
    002-fix-build-with-newer-qt5.patch \
    003-Add-local-variable-i.patch \
    004-Fix-the-evaluation-of-tensor-components.patch \
    005-add-i-to-local-scope.patch \
    006-fix-some-compile-errors-in-DCRComplexSolve.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

  CFLAGS+=" -Wno-implicit-function-declaration" \
  CXXFLAGS+=" -Wno-deprecated-declarations" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -Wno-dev \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DWITH_ELMERGUI=ON \
      -DWIN32=ON \
      -DCPACK_BUNDLE_EXTRA_WINDOWS_DLLS=OFF \
      -DWITH_MPI=OFF \
      -DWITH_QT5=ON \
      -DQwt_INCLUDE_DIRS=${MINGW_PREFIX}/include/qwt-qt5/ \
      -DQWT_LIBRARY=${MINGW_PREFIX}/lib/libqwt-qt5.dll.a \
      ../${_realname}-release-${pkgver}

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

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

  "${MINGW_PREFIX}"/bin/ctest.exe ${MAKEFLAGS} || true
}

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

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  WIN_PREFIX=$(cygpath -wm ${MINGW_PREFIX})
  for _f in elmerf90 elmerld; do
    sed -e "s|${WIN_PREFIX}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${_f}
  done

  rm "${pkgdir}${MINGW_PREFIX}"/bin/libarpack*
  rm "${pkgdir}${MINGW_PREFIX}"/bin/*.bat

  install -Dm644 "${srcdir}/${_realname}-release-${pkgver}/license_texts/LICENSES" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  install -Dm644 "${srcdir}/${_realname}-release-${pkgver}/license_texts/LICENSES_GPL.txt" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  install -Dm644 "${srcdir}/${_realname}-release-${pkgver}/license_texts/GPL-2.txt" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  install -Dm644 "${srcdir}/${_realname}-release-${pkgver}/license_texts/LGPL-2.1.txt" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
}
