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

_realname=msmpi
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=10.1.1
pkgrel=4
pkgdesc="Microsoft MPI SDK (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://docs.microsoft.com/en-us/message-passing-interface/microsoft-mpi"
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-binutils")
depends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-gcc-fortran")
optdepends=("${MINGW_PACKAGE_PREFIX}-tcl: build & run test suite")
options=('strip')
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'
  'all.tcl'
  'tclbuildtest.tcl'
  'msmpi.test'
)
sha256sums=('8284306d3d86a29904ce55b6a91a982470b0648821da0a563a34977a542e30e4'
            'baee3f18f38650e7182956baa0d3d8f8e5c26d8603ccee4871a4dbd160c13660'
            'f3384eeb848164e518edfd2b2b9ff1d0f33e267efd5811d5a7c5f79ef7a0a81e'
            '38e7be31ad555570f6122058317c08504472b8cfd0a198cdad827519b941ba2c'
            '031f910c6ce9a0f7bf66365dd4271fa36530f32c5d22755b10ef2d89dc605e1f'
            '4ff6e817fe7517f04f0ca04a038e4c136264384d36f520b79995604e3986fce4'
            'fd18184993872fc4eaea825e85f974dca4b020598d838c424c6c112c2328cf2a'
            '7b8ed9d3e257e439678cc648164164d6817f7fde68530055b392f67d97bf8ec8'
            'ea8203492942fa900c4946741ad88af8957c4d058e06eb618ffae0e5e967b71b'
            '9483053c0f3ce15d850f69565f2b4e53630cee75073c27432cd08ecc596a57d9'
            'c9f63c1ca20f8ebfe26ad455342f34c27bc1a1a83778240388e9f18b26c2e889'
            '8cb47bb77451dbe2703f7f28eef422e55e7a33a28977bf13bbd11d305aeef61e'
            'ec5072630e1c0309fe383669e9187790cd135a393c67bc4bc35cf60b0ba396ff'
            '15c7af25b91406d5fe5f26cfe00963b6cfde1c3dd466eb25f1b6fae299934966'
            '773d406e05340b3b96326ddd04f43fea3df3ca30912412824a976a18a86fc007')


build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${CARCH} && cd ${srcdir}/build-${CARCH}
  mkdir -p bin include lib
  # Import library
  ${MINGW_PREFIX}/bin/dlltool -k -d "$srcdir/msmpi.def.${CARCH}" -l lib/libmsmpi.a
  # Headers
  cp $srcdir/{mpi.h,mpif.h,mpi.f90} include
  cp $srcdir/mpifptr.h.${CARCH} include/mpifptr.h
  # Compiler wrappers
  mpic="$srcdir/mpi.c"
  cflags="-s -O2 -DNDEBUG"
  gcc ${cflags} -o bin/mpicc.exe -DCC "${mpic}"
  gcc ${cflags} -o bin/mpicxx.exe -DCXX "${mpic}"
  cp bin/mpicxx.exe bin/mpic++.exe
  gcc ${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 ${srcdir}/mpi.f90
  rm mpi.o
}

package() {
  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" "${pkgdir}${MINGW_PREFIX}/share/test/msmpi"
  cd ${srcdir}
  cp {all,tclbuildtest}.tcl hello_mpi.{c,cpp,F,f90} msmpi.test "${pkgdir}${MINGW_PREFIX}/share/test/msmpi"
  cd "${srcdir}/build-${CARCH}"
  cp -r * "${pkgdir}${MINGW_PREFIX}"
  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${_realname}
  " | sed '/^\s*$/d;s/^\s*//' > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}.pc"

}
