# Contributor: Oleg A. Khlybov <fougas@mail.ru>

_realname=superlu_dist
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=9.1.0
pkgrel=1
pkgdesc="Distributed memory sparse direct linear solver (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url='https://portal.nersc.gov/project/sparse/superlu/'
msys2_repository_url="https://github.com/xiaoyeli/superlu_dist"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-parmetis"
         "${MINGW_PACKAGE_PREFIX}-omp")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-msmpi")
optdepends=("${MINGW_PACKAGE_PREFIX}-tcl: build & run test suite")
source=("https://github.com/xiaoyeli/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        'all.tcl'
        'tclbuildtest.tcl'
        'superlu_dist.test'
        '0001-pc-fix.patch'
        '0002-no-rpath-with-mingw.patch')
sha256sums=('1cb2c6dc7e8231b2ec30c1266e55e440ffca9f55527771d8df28f900dd179f9d'
            'ec5072630e1c0309fe383669e9187790cd135a393c67bc4bc35cf60b0ba396ff'
            '15c7af25b91406d5fe5f26cfe00963b6cfde1c3dd466eb25f1b6fae299934966'
            '65bdbd6de4a4b1c9dd40fb219ba3cb686f46f76b729a3819a2074356cbb9d15b'
            'd92946ba25c58e19eecc923bc41fc24e2faaeea4bce232f96b1fb92f40d59c2a'
            'c1e526a04f13d6dbe1c7e0ae50fb298f724ed47c1e14bb05226f1016d32858fa')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  for p in ${source[*]:4}; do
    patch -p1 -i "${srcdir}/$p"
  done
}

build() {
  mkdir -p build-${MSYSTEM} && cd 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

  MSYS2_ARG_CONV_EXCL='-DCMAKE_INSTALL_PREFIX=' \
    ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DBUILD_STATIC_LIBS=ON \
      -DBUILD_TESTING=OFF \
      -Denable_tests=OFF \
      -Denable_examples=OFF \
      -Denable_python=OFF \
      -DTPL_ENABLE_LAPACKLIB=ON \
      -DXSDK_ENABLE_Fortran=OFF \
      -DTPL_PARMETIS_LIBRARIES="-L${MINGW_PREFIX}/lib -lparmetis -lmetis -lmsmpi" \
      -DTPL_PARMETIS_INCLUDE_DIRS="${MINGW_PREFIX}/include" \
      ../${_realname}-${pkgver}

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

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

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

  cd "${pkgdir}${MINGW_PREFIX}/include"
  mkdir "${_realname}"
  find . -type f -exec mv {} "${_realname}" \;
  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/test/${_realname}"

  cd "${srcdir}"
  cp {all,tclbuildtest}.tcl ${_realname}.test "${pkgdir}${MINGW_PREFIX}/share/test/${_realname}"
  cd "${_realname}-${pkgver}/EXAMPLE"
  cp p?drive.c ?create_matrix.c g20.rua cg20.cua "${pkgdir}${MINGW_PREFIX}/share/test/${_realname}"
}
