# Maintainer: ImperatorS79 <fevrier.simon@gmail.com>

_realname=amgcl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.4.4
pkgrel=2
pkgdesc="Header-only C++ library for solving large sparse linear systems with algebraic multigrid (AMG) method. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='http://amgcl.readthedocs.io/'
msys2_repository_url="https://github.com/ddemidov/amgcl"
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-fc")
             "${MINGW_PACKAGE_PREFIX}-scotch")
depends=()
source=("https://github.com/ddemidov/amgcl/archive/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('02fd5418e14d669422f65fc739ce72bf9516ced2d8942574d4b8caa05dda9d8c')

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+=("-DAMGCL_HAVE_FORTRAN=ON")
  fi

  #Version of pastix too old in amgcl
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DAMGCL_BUILD_TESTS=OFF \
      -DAMGCL_BUILD_EXAMPLES=OFF \
      "${_extra_config[@]}" \
      -DAMGCL_HAVE_PYTHON=ON \
      -DBoost_USE_STATIC_LIBS=OFF \
      ../${_realname}-${pkgver}

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

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -DAMGCL_BUILD_TESTS=ON \
    -DAMGCL_BUILD_EXAMPLES=ON \
    ../${_realname}-${pkgver}
  "${MINGW_PREFIX}"/bin/cmake.exe --build .

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

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

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

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/share/amgcl/cmake/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

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