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

# This package provides multiple library build flavors differentiated by
# the 3-character suffix XYZ as follows:
#
#  * X is the scalar type based on the Netlib's SDCZ notation:
#    - A for multiprecision or precision-neutral build
#    - S for single precision real
#    - D for double precision real
#    - C for single precision complex
#    - Z for double precision complex
#
#  * Y is the execution model:
#    - S for sequential code
#    - M for MPI parallel code
#    - T for multithreaded code, either bare threading or OpenMP, OpenACC etc.
#    - H for heterogeneous code with CUDA, OpenCL etc.
#
#  * Z is the build type:
#    - O for optimized build
#    - G for debugging build
#
# The suffix is used in the static and dynamic libraires as well as in PkgConfig .pc files.
# That is the ZMO suffix designates the optimized MPI parallel double precision complex library flavor.
# Consider the `pkg-config slepc-zmo --cflags` command to obtain the build-specific compilation flags.

_realname=slepc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.23.0
pkgrel=1
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
pkgdesc="Scalable library for eigenvalue problem computations (mingw-w64)"
url="https://slepc.upv.es/"
msys2_repository_url="https://gitlab.com/slepc/slepc"
msys2_references=(
  'aur: slepc'
)
license=('spdx:BSD-2-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-petsc"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"))
makedepends=("python"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             $([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-msmpi")
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-fc")
             "${MINGW_PACKAGE_PREFIX}-petsc-build")
optdepends=("${MINGW_PACKAGE_PREFIX}-tcl: build & run test suite")
source=("https://slepc.upv.es/download/distrib/${_realname}-${pkgver}.tar.gz"
        'slepc.tcl'
        'testme-0.tm'
        'buildme-0.tm'
        'xyz-0.tm')
noextract=("${_realname}-${pkgver}.tar.gz")
sha256sums=('78252f7b2f540c5fdadadee0fd21f3e6eff810f82cb45482f327b524c8db63d0'
            '1c341cd82f6bd8db447a21f88ba81378605ca6b0eabc443212d53fad6113c9fe'
            '8d8f5670ffdf34a10b0d027bff976131824e2e6b053f8a13d207ecddd4663d6d'
            'd0db2aa573c9aaf57b30cb3c2bd7c66e1677f3f468002f467da98b75943076b1'
            'd52709e3663d50ed793bfe991c571ec4ea5e6eaf72fcc920e1f8ba43d0ed7050')


prepare() {
  mkdir -p $srcdir/build-${MSYSTEM} && cd $srcdir/build-${MSYSTEM}
  tar xzf $srcdir/${_realname}-${pkgver}.tar.gz
}

build() {
  . ${MINGW_PREFIX}/src/petsc/petsc
  cd  "${srcdir}/build-${MSYSTEM}/${_realname}-${pkgver}"

  declare extra_libs=""

  if [[ ${CARCH} != aarch64 ]]; then
    extra_libs="$(pkg-config msmpi --libs)"
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    extra_libs+=" -lgfortran"
  fi

  for build in ${petsc_builds}; do
    export SLEPC_DIR=`pwd`
    export PETSC_ARCH=${build}
    export PETSC_DIR=${MINGW_PREFIX}/src/petsc
    /usr/bin/python configure
    make
    (
      cd ${build}/lib
      case ${build} in
        *o) strip -S *.a ;;
      esac
      ${CC} -shared -Wl,--enable-auto-import -Wl,--export-all-symbols \
        -o lib${_realname}-${build}.dll -Wl,--out-implib,lib${_realname}.dll.a \
        -Wl,--whole-archive lib${_realname}.a -Wl,--no-whole-archive \
        $(pkg-config petsc-${build} --libs) $(pkg-config openblas --libs) ${extra_libs}
    )
  done
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}/${_realname}-${pkgver}
  mkdir -p "${pkgdir}"${MINGW_PREFIX}/{bin,lib/pkgconfig,lib/${_realname},include/${_realname},share/test/${_realname}}
  (
    cd include
    cp *.h "${pkgdir}"${MINGW_PREFIX}/include/${_realname}
    cd ${_realname}
    cp -R finclude "${pkgdir}"${MINGW_PREFIX}/include/${_realname}
    cd "${pkgdir}"${MINGW_PREFIX}/include/${_realname}
    find . \( ! -name '*.h' -a -type f \) -delete
  )
  (
    cd "${srcdir}"
    cp *.tcl *.tm "${pkgdir}"${MINGW_PREFIX}/share/test/${_realname}
  )
  (
    cd src/eps/tutorials
    cp ex1.c ex1f.F90 "${pkgdir}"${MINGW_PREFIX}/share/test/${_realname}
  )
  for build in ${petsc_builds}; do
    (
      cd ${build}/lib
      mkdir -p "${pkgdir}"${MINGW_PREFIX}/lib/${_realname}/${build}
      cp *.a "${pkgdir}"${MINGW_PREFIX}/lib/${_realname}/${build}
      cp *.dll "${pkgdir}"${MINGW_PREFIX}/bin
    )
    (
      cd ${build}/include
      mkdir -p "${pkgdir}"${MINGW_PREFIX}/include/${_realname}/${build}
      cp *.h "${pkgdir}"${MINGW_PREFIX}/include/${_realname}/${build}
      if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
        cp *.mod "${pkgdir}"${MINGW_PREFIX}/include/${_realname}/${build}
      fi
    )
    case ${build} in
      ?m?) desc="MPI parallel";;
      ?t?) desc="OpenMP multithreaded";;
      ?s?) desc="Sequential";;
    esac
    case ${build} in
      z*|d*) desc="$desc double precision";;
      c*|s*) desc="$desc single precision";;
    esac
    case ${build} in
      z*|c*) desc="$desc complex";;
    esac
    echo "
      prefix=${MINGW_PREFIX}
      libdir=\${prefix}/lib/${_realname}
      includedir=\${prefix}/include/${_realname}
      Name: ${_realname}
      URL: ${url}
      Version: ${pkgver}
      Description: ${desc} SLEPc build
      Requires: petsc-${build}
      Cflags: -I\${includedir}/${build} -I\${includedir}
      Libs: -L\${libdir}/${build} -l${_realname}
    " | sed '/^\s*$/d;s/^\s*//' > "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/${_realname}-${build}.pc
  done
}
