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

_realname=slepc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.14.2
petsc_pkgver=3.14
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
pkgdesc="Scalable library for eigenvalue problem computations (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-petsc>=${petsc_pkgver}")
makedepends=("python"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-gcc-fortran"
             "${MINGW_PACKAGE_PREFIX}-petsc-build>=${petsc_pkgver}")
optdepends=("${MINGW_PACKAGE_PREFIX}-tcl: build & run test suite")
options=('strip' 'staticlibs')
license=('2-clause BSD')
url="https://slepc.upv.es/"
source=("https://slepc.upv.es/download/distrib/${_realname}-${pkgver}.tar.gz"
        'all.tcl'
        'tclbuildtest.tcl'
        'slepc.test')
noextract=("${_realname}-${pkgver}.tar.gz")
sha256sums=('3e54578dda1f4c54d35ac27d02f70a43f6837906cb7604dbcec0e033cfb264c8'
            'ec5072630e1c0309fe383669e9187790cd135a393c67bc4bc35cf60b0ba396ff'
            '15c7af25b91406d5fe5f26cfe00963b6cfde1c3dd466eb25f1b6fae299934966'
            '47237bd53f15e9d204b7c871ef887d9b6d11e0add1409a6eeb1a122f4b302265')

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

build() {
  . ${MINGW_PREFIX}/src/petsc-*/petsc
  cd  "${srcdir}/build-${MINGW_CHOST}/${_realname}-${pkgver}"
  for build in ${petsc_builds}; do
    export SLEPC_DIR=`pwd`
    export PETSC_ARCH=${build}
    export PETSC_DIR=${MINGW_PREFIX}/src/petsc-${petsc_pkgver}
    /usr/bin/python configure 
    make
  	(
  		cd ${build}/lib
  		lib=libslepc-${build}
  		rm -rf ${lib}.a
  	  case ${build} in
    		*o) strip -S *.a ;;
  	  esac
  		ar crsT ${lib}.a libslepcnep.a libslepcpep.a libslepcsvd.a libslepceps.a libslepcmfn.a libslepclme.a libslepcsys.a
  		gcc -shared -Wl,--enable-auto-import -Wl,--export-all-symbols -o ${lib}.dll -Wl,--out-implib,${lib}.dll.a -Wl,--whole-archive ${lib}.a -Wl,--no-whole-archive $(pkg-config msmpi openblas petsc-${build} --libs)
  	)
  done
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}/${_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 {all,tclbuildtest}.tcl slepc.test ${pkgdir}${MINGW_PREFIX}/share/test/${_realname}
  )
  (
    cd src/eps/tutorials
    cp ex1.c ex1f.F ${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,mod} ${pkgdir}${MINGW_PREFIX}/include/${_realname}/${build}
  	)
    lib=${_realname}-${build}
    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${lib}
    " | sed '/^\s*$/d;s/^\s*//' > ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${lib}.pc
  done
}
