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

_realname=scotch
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-int64")
pkgdesc='Graph partitioning and sparse matrix ordering package (mingw-w64)'
pkgver=7.0.7
pkgrel=1
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.labri.fr/perso/pelegrin/scotch/"
msys2_repository_url="https://gitlab.inria.fr/scotch/scotch"
license=('spdx:CECILL-C')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-libsystre"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             $([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-msmpi"))
options=('!strip')
source=("https://gitlab.inria.fr/scotch/scotch/-/archive/v${pkgver}/${_realname}-v${pkgver}.tar.gz"
        "Makefile.idx32.inc"
        "Makefile.idx64.inc"
        "0002-pipe-fix.patch"
        "0004-dummysizes-regex.patch"
        "0005-gcc15.patch")
sha256sums=('02084471d2ca525f8a59b4bb8c607eb5cca452d6a38cf5c89f5f92f7edc1a5b5'
            '4221b69dcc53230ff4d6bc18e3982f6d39971a99fc17f4dab609fc19b99d8a30'
            'c52bf0598332d8139a37606e7ea9376b7f841059047c0c977b205a763fc7e97a'
            'b6e76b1d2f35b3fcc771b97a35a468d6314ee3a619defef18b82fc7654638a49'
            '15dd82d4c7b118e08316df4242d647bdaa1f180ff143ee33fe84633c97aac111'
            '5e9f80eccaca1619ffa279c544c9601731748d852c02f1873b0556bebf625804')

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

_build_scotch() {
  _idx_makefile_inc=$1
  _idx_suffix=$2

  cp -r "${srcdir}/${_realname}-v${pkgver}" "${srcdir}/build-${MSYSTEM}-idx${_idx_suffix}"
  cd "${srcdir}/build-${MSYSTEM}-idx${_idx_suffix}"

  cp -p "${srcdir}/${_idx_makefile_inc}" "${srcdir}/build-${MSYSTEM}-idx${_idx_suffix}/src/Makefile.inc"
  cd "${srcdir}/build-${MSYSTEM}-idx${_idx_suffix}/src"

  if [[ ${CARCH} != aarch64 ]]; then
    make scotch ptscotch esmumps ptesmumps
  else
    # No MPI so no PT-Scotch
    make scotch esmumps 
  fi

  cd ../lib
  ${CC} -shared -o lib${_realname}.dll -Wl,--enable-auto-import -Wl,--export-all-symbols,-no-undefined \
    -Wl,--out-implib,lib${_realname}.dll.a -Wl,--whole-archive lib${_realname}.a lib${_realname}err.a -Wl,--no-whole-archive \
    -pthread

  mv "${srcdir}/build-${MSYSTEM}-idx${_idx_suffix}/include/metis.h" "${srcdir}/build-${MSYSTEM}-idx${_idx_suffix}/include/scotchmetis.h"

  if [[ ${CARCH} != aarch64 ]]; then
    mpicc -shared -o libpt${_realname}.dll -Wl,--enable-auto-import -Wl,--export-all-symbols,-no-undefined \
      -Wl,--out-implib,libpt${_realname}.dll.a -Wl,--whole-archive libpt${_realname}.a libpt${_realname}err.a -Wl,--no-whole-archive \
      -L. -l${_realname} -pthread

    mv "${srcdir}/build-${MSYSTEM}-idx${_idx_suffix}/include/parmetis.h" "${srcdir}/build-${MSYSTEM}-idx${_idx_suffix}/include/ptscotchparmetis.h"
  fi
}

build()
{
  msg2 "Build Scotch with 32-bit indexing"
  _build_scotch "Makefile.idx32.inc" "32"

  msg2 "Build Scotch with 64-bit indexing"
  _build_scotch "Makefile.idx64.inc" "64"
}


_package_scotch() {
  _idx_suffix=$1

  cd "${srcdir}/${_realname}-v${pkgver}"
  mkdir -p "${pkgdir}"${MINGW_PREFIX}/{bin,lib,include}
  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig"
  echo "
	prefix=${MINGW_PREFIX}
	libdir=\${prefix}/lib
	includedir=\${prefix}/include
	Name: scotch
	URL: ${url}
	Version: ${pkgver}
	Description: Serial graph partitioning and sparse matrix ordering package
	Cflags: -I\${includedir}
	Libs.private: -lscotcherr
	Libs: -L\${libdir} -lscotch
  " | sed '/^\s*$/d;s/^\s*//' > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/scotch.pc"

  if [[ ${CARCH} != aarch64 ]]; then
    echo "
    prefix=${MINGW_PREFIX}
    libdir=\${prefix}/lib
    includedir=\${prefix}/include
    Name: ptscotch
    URL: ${url}
    Version: ${pkgver}
    Description: Parallel graph partitioning and sparse matrix ordering package
    Requires: scotch
    Requires.private: msmpi
    Cflags: -I\${includedir}
    Libs.private: -lptscotcherr
    Libs: -L\${libdir} -lptscotch
    " | sed '/^\s*$/d;s/^\s*//' > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/ptscotch.pc"
  fi  
  (
    cd "${srcdir}/build-${MSYSTEM}-idx${_idx_suffix}"/include
    install -m644 *.h "${pkgdir}${MINGW_PREFIX}/include"
  )
  (
    cd "${srcdir}/build-${MSYSTEM}-idx${_idx_suffix}"/lib
    install -m644 lib*.a "${pkgdir}${MINGW_PREFIX}/lib"
    install -m644 lib*.dll "${pkgdir}${MINGW_PREFIX}/bin"
  )
}

package_scotch()
{
  _package_scotch "32"
}

package_scotch-int64() {
  pkgdesc="Graph partitioning and sparse matrix ordering package with 64-bit indexing (mingw-w64)"
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  _package_scotch "64"
}


# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
