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

# There is a Tcl integration test suite for both package maintainers and package end users
# to ensure the installed package is intact. The test suite performs a series of compile/run
# tests for multiple configurations from the build matrix.
# Basic usage: tclsh $MINGW_PREFIX/share/test/msmpi/msmpi.tcl

_realname=msmpi
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=10.1.1
pkgrel=17
pkgdesc="Microsoft MPI SDK (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url="https://docs.microsoft.com/en-us/message-passing-interface/microsoft-mpi"
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-cc")
makedepends=("${MINGW_PACKAGE_PREFIX}-fc")
optdepends=("${MINGW_PACKAGE_PREFIX}-fc: build FORTRAN code"
			"${MINGW_PACKAGE_PREFIX}-tcl: test suite")
source=(
  'mpi.c'
  'mpi.h'
  'mpif.h'
  'mpi.f90'
  'mpifptr.h.x86_64'
  'mpifptr.h.i686'
  'msmpi.def.x86_64'
  'msmpi.def.i686'
  'hello_mpi.c'
  'hello_mpi.cpp'
  'hello_mpi.F'
  'hello_mpi.f90'
  'msmpi.tcl'
  'testme-0.tm'
  'buildme-0.tm'
)
sha256sums=('17086fb1cf949251e4ae1549a06d292c58e468c822cca3ac1851e57e79d8ab20'
            'baee3f18f38650e7182956baa0d3d8f8e5c26d8603ccee4871a4dbd160c13660'
            '66185a4d47ca1a27df2340d3d8d28787e72db54b184f20430ed1e1d7c9244371'
            '5fbdb4109bb5d0badf24c4aaeeaa98009d4a7eaf4e595ecfa4bc3e750176299f'
            'edd76b5096bd31052be13e2d3306d931277d9c983b147ecf8aef0761d4ae8fb3'
            '126bb8230844ad1de5f7e368c12008d2eea9831cb149b4a6952c775c08b294a2'
            'fd18184993872fc4eaea825e85f974dca4b020598d838c424c6c112c2328cf2a'
            '7b8ed9d3e257e439678cc648164164d6817f7fde68530055b392f67d97bf8ec8'
            'ea8203492942fa900c4946741ad88af8957c4d058e06eb618ffae0e5e967b71b'
            '9483053c0f3ce15d850f69565f2b4e53630cee75073c27432cd08ecc596a57d9'
            'c9f63c1ca20f8ebfe26ad455342f34c27bc1a1a83778240388e9f18b26c2e889'
            '8cb47bb77451dbe2703f7f28eef422e55e7a33a28977bf13bbd11d305aeef61e'
            'cef6074632450dd847c051c785a12826a8dbe5c33f5771ecd3af8fdcee1c2f39'
            '8d8f5670ffdf34a10b0d027bff976131824e2e6b053f8a13d207ecddd4663d6d'
            'd0db2aa573c9aaf57b30cb3c2bd7c66e1677f3f468002f467da98b75943076b1')

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  # Import library (.dll.a)
  mkdir -p lib
  ${MINGW_PREFIX}/bin/dlltool -k -d "$srcdir/msmpi.def.${CARCH}" -l lib/libmsmpi.dll.a

  # Headers
  mkdir -p include
  cp $srcdir/mpi.h include
  cp $srcdir/{mpif.h,mpi.f90} include
  cp $srcdir/mpifptr.h.${CARCH} include/mpifptr.h

  # C/C++ Compiler wrappers
  mkdir -p bin
  mpic="$srcdir/mpi.c"
  if [[ ${MSYSTEM} == CLANG64 ]]; then
    sed -e "s|gfortran|flang|g" -i ${mpic}
  fi
  cflags="-s -O2 -DNDEBUG"
  $CC ${cflags} -o bin/mpicc.exe -DCC "${mpic}"
  $CC ${cflags} -o bin/mpicxx.exe -DCXX "${mpic}"
  cp bin/mpicxx.exe bin/mpic++.exe

  # Fortran Compiler wrappers
  $CC ${cflags} -o bin/mpifort.exe -DFC "${mpic}"
  cp bin/mpifort.exe bin/mpif77.exe
  cp bin/mpifort.exe bin/mpif90.exe

  # FORTRAN90 MPI modules
  bin/mpifort -c -Jinclude -cpp "${srcdir}"/mpi.f90
  rm mpi.o
}

package() {
  cd "${srcdir}"

  cp -r "build-${MSYSTEM}" "${pkgdir}${MINGW_PREFIX}"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/test/msmpi"
  cp msmpi.tcl {testme-0,buildme-0}.tm hello_mpi.{c,cpp,F,f90} "${pkgdir}${MINGW_PREFIX}/share/test/msmpi"

  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}
    Cflags: -I\${includedir}
    Libs: -L\${libdir} -l:libmsmpi.dll.a
  " | sed '/^\s*$/d;s/^\s*//' > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}.pc"

}
