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

_realname=parmetis
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.0.3
pkgrel=4
pkgdesc="Parallel Graph Partitioning and Fill-reducing Matrix Ordering (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url='http://glaros.dtc.umn.edu/gkhome/metis/parmetis/overview'
msys2_references=(
  'gentoo: '
)
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-metis")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-msmpi")
source=("https://salsa.debian.org/science-team/parmetis/-/archive/upstream/${pkgver}/parmetis-upstream-${pkgver}.tar.bz2"
        "0001-mingw-w64-does-not-have-sys-resource-h.patch"
        "0002-mingw-w64-do-not-use-reserved-double-underscored-names.patch"
        "0003-WIN32-Install-RUNTIME-to-bin.patch"
        "0004-METIS-GKLIB-location.patch"
        "0005-find-MPI.patch")
sha256sums=('58d00e31fe8954ec5e6811112a4b757fa1b47767494d2c7367e77dfcd8c8d85f'
            '22e62ec7dfbaf01865b1e09e7b3f3bad20188354efbef6e64d102a03c7bf748b'
            '141026e4e39f0529a916dce718273875ba12a98b0ebe7c2730e4950866ffc845'
            'b384fa51e01b9d3384fd067236e34036b0520f375d16abe575005e518dd5b1fd'
            'f1e7a174460ac79732de10d0f076f87b2ed60d185436bda0698bd012e70ea53c'
            '642f801967edc196e94e28f7fa3a63759ab33c1a534a5924d404acdcbfea2eb2')

prepare() {
  cd "${srcdir}"/${_realname}-upstream-${pkgver}
  for p in "${srcdir}"/*.patch; do
    patch -p1 -i "$p"
  done
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  for _shared in OFF ON; do
    mkdir -p "${srcdir}"/build-shared-${_shared}-${MSYSTEM} && cd "${srcdir}"/build-shared-${_shared}-${MSYSTEM}
    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX="  \
      ${MINGW_PREFIX}/bin/cmake                    \
        -G'Ninja'                         \
        -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX}     \
        -DCMAKE_C_COMPILER=mpicc                   \
        -DCMAKE_CXX_COMPILER=mpicxx                \
        -DCMAKE_C_FLAGS="${CPPFLAGS} ${CFLAGS}"    \
        "${extra_config[@]}"                       \
        -DSHARED=${_shared}                        \
        ../${_realname}-upstream-${pkgver}
    ${MINGW_PREFIX}/bin/cmake --build .
  done
}

package() {
  for _shared in OFF ON; do
    cd "${srcdir}"/build-shared-${_shared}-${MSYSTEM}
    DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  done

  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig"
  echo "
      prefix=${MINGW_PREFIX}
      libdir=\${prefix}/lib
      includedir=\${prefix}/include
      Name: ${_realname}
      URL: ${url}
      Version: ${pkgver}
      Description: ${pkgdesc}
      Requires: metis msmpi
      Cflags: -I\${includedir}
      Libs: -L\${libdir} -l${_realname}
    " | sed '/^\s*$/d;s/^\s*//' > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}.pc"

  mv "${pkgdir}"${MINGW_PREFIX}/bin/ptest{,_parmetis}.exe
  mv "${pkgdir}"${MINGW_PREFIX}/bin/mtest{,_parmetis}.exe
}
