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

_realname=superlu_dist
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=6.4.0
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
pkgdesc="Distributed memory sparse direct linear solver (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-parmetis"
         "${MINGW_PACKAGE_PREFIX}-msmpi")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc")
optdepends=("${MINGW_PACKAGE_PREFIX}-tcl: build & run test suite")
options=('strip' 'staticlibs')
license=('3-clause BSD')
url='https://portal.nersc.gov/project/sparse/superlu/'
source=("https://github.com/xiaoyeli/${_realname}/archive/v${pkgver}.tar.gz"
        'all.tcl'
        'tclbuildtest.tcl'
        'superlu_dist.test'
        '0001-pc-fix.patch'
        '0002-aligned-malloc-fix.patch')
sha256sums=('cb9c0b2ba4c28e5ed5817718ba19ae1dd63ccd30bc44c8b8252b54f5f04a44cc'
            'ec5072630e1c0309fe383669e9187790cd135a393c67bc4bc35cf60b0ba396ff'
            '15c7af25b91406d5fe5f26cfe00963b6cfde1c3dd466eb25f1b6fae299934966'
            '65bdbd6de4a4b1c9dd40fb219ba3cb686f46f76b729a3819a2074356cbb9d15b'
            '3f5ef6bcb3501f0b1d65596b6c0c4b76091f3ef4aaf77b41d522cc7f0d5ce699'
            'eb9a900004828d2d1721aeef53fe02d79bf8bc35b27a24275464caa344daee24')

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

build() {
  declare -a _btype
  if check_option "debug" "y"; then
    _btype=Debug
  else
    _btype=Release
  fi
  cd "${srcdir}"
  mkdir -p build-${CARCH}s && cd build-${CARCH}s
  MSYS2_ARG_CONV_EXCL='-DCMAKE_INSTALL_PREFIX=' \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=${_btype} \
      -DBUILD_SHARED_LIBS=OFF \
      -DBUILD_TESTING=OFF \
      -Denable_tests=OFF \
      -Denable_examples=OFF \
      -DTPL_PARMETIS_LIBRARIES="-L${MINGW_PREFIX}/lib -lparmetis -lmetis -lmsmpi" \
      -DTPL_PARMETIS_INCLUDE_DIRS="${MINGW_PREFIX}/include" \
      ../${_realname}-${pkgver}
  make
  cd "${srcdir}"
  mkdir -p build-${CARCH} && cd build-${CARCH}
  MSYS2_ARG_CONV_EXCL='-DCMAKE_INSTALL_PREFIX=' \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=${_btype} \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_NEED_RESPONSE=ON \
      -DBUILD_TESTING=OFF \
      -Denable_tests=OFF \
      -Denable_examples=OFF \
      -DTPL_PARMETIS_LIBRARIES="-L${MINGW_PREFIX}/lib -lparmetis -lmetis -lmsmpi" \
      -DTPL_PARMETIS_INCLUDE_DIRS="${MINGW_PREFIX}/include" \
      ../${_realname}-${pkgver}
  make
}

package() {
  cd "${srcdir}/build-${CARCH}s"
  make DESTDIR="${pkgdir}" install
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" 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}"
}
