# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Jakub Klinkovský <lahwaacz at archlinux dot org>
# Maintainer: David Runge <dvzrv@archlinux.org>

_name=pmix
pkgbase=openpmix
pkgname=(
  openpmix
  openpmix-docs
)
# NOTE: keep in sync with openmpi major versions
pkgver=5.0.1
pkgrel=1
pkgdesc="Extended version of the PMI standard"
arch=(x86_64 powerpc64le powerpc64 powerpc riscv64)
url="https://github.com/openpmix/openpmix"
license=(BSD-3-Clause)
makedepends=(
  glibc
  hwloc
  perl
  libevent
  libevent_{core,pthreads}-2.1.so
  python
  zlib
)
source=($url/releases/download/v$pkgver/$_name-$pkgver.tar.gz)
sha512sums=('db90eb716697282eb047363eb4695d9371d285fb3331cbe8f0a8d2c0e3418e7a05f6948ed5564b3a03893ae69193d0d784dee6340b9a9a8b6d7fa0e8c3816a5f')
b2sums=('a6ebe6937dba3463e1d066e02cd0079f1832f27ffe7d72f25f3fe71fdadf6faf8f00b6822e42e1b512a7a4003e207b811aa9f50d5b164df96fd96d9ceb933117')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

prepare() {
  cd $_name-$pkgver
  ./autogen.pl
}

build() {
  local configure_options=(
    --prefix=/usr
    --sysconfdir=/etc/$pkgname
  )

  cd $_name-$pkgver
  ./configure "${configure_options[@]}"
  # prevent excessive overlinking due to libtool
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make V=1
}

check() {
  make -k check -C $_name-$pkgver
}

package_openpmix() {
  depends=(
    glibc
    hwloc
    libevent libevent_{core,pthreads}-2.1.so
    zlib
  )
  optdepends=(
    'openpmix-docs: for documentation'
  )
  provides=(libpmix.so pmix)
  backup=(etc/$pkgname/pmix-mca-params.conf)

  make DESTDIR="$pkgdir" install -C $_name-$pkgver

  (
    cd "$pkgdir"
    _pick $pkgbase-docs usr/share/doc/
  )

  install -vDm 644 $_name-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
}

package_openpmix-docs() {
  pkgdesc+=" - documentation"

  mv -v $pkgname/* "$pkgdir/"
}
