# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: Kyle Keen <keenerd@gmail.com>

pkgname=zeromq
pkgver=4.3.5
pkgrel=3
pkgdesc='Fast messaging system built on sockets. C and C++ bindings. aka 0MQ, ZMQ.'
arch=(x86_64 aarch64)
url='http://www.zeromq.org'
license=(MPL-2.0)
depends=(
  glibc
  gnutls
  util-linux
  libsodium
  libpgm
  libgcc
  libstdc++
)
makedepends=(
  git
  cmake
  asciidoc
  xmlto
)
optdepends=('cppzmq: C++ binding for libzmq')
provides=(libzmq.so)
source=(
  "$pkgname::git+https://github.com/zeromq/libzmq#tag=v$pkgver"
  fix-doc-install.patch
)
sha512sums=('9e9df568ecffa2fd2d5e01860d3ac66e4826a15c1d32b9844382dd7a5ec03062d84480e1607d16a0b55a93f012bc479d16df211665b9b75e8b2bbefc52dab859'
            'fcaf3e6147b9a759f8ba3cc71365d78d0b16e93c025eae4b9ebcbf4c832d09db81b61840b6c9b69bb4fbffcb8a0c7221598d866158902ff878bf150e23aa73e6')
b2sums=('877078b6bf0a43a2394cb80201de24b9434be033e41a2e6523cda4c10dc3a09d0968e4b3bc889341d64d915659548fac1488e90ebd0dad00a7a37e7d27e5ad7e'
        '98aed4ff26fd1da40b7a1a4d5b101c7906a0f95bc0cfec936879418a857bfe7d4c8b6951de2247d1301ce9273dfe30442b3a03b9cc84f4d6ea737fdff46d43c6')

prepare() {
  cd "$pkgname"

  # install documentation to correct directory
  patch -p1 -i "$srcdir/fix-doc-install.patch"
}

build() {
  local cmake_options=(
    -S "$pkgname"
    -B build
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_INSTALL_PREFIX=/usr
    -D CMAKE_CURRENT_BINARY_DIR=/usr
    -D CMAKE_POLICY_VERSION_MINIMUM=3.5
    -D BUILD_SHARED=ON
    -D BUILD_STATIC=OFF
    -D ENABLE_CPACK=OFF
    -D ZMQ_BUILD_TESTS=ON
    -D WITH_OPENPGM=ON
    -D OPENPGM_PKGCONFIG_NAME=openpgm-5.3
    -D WITH_LIBBSD=ON
    -D WITH_LIBSODIUM=ON
    -D ENABLE_CURVE=ON
    -D WITH_DOC=ON
    -W no-dev
  )

  cmake "${cmake_options[@]}"

  cmake --build build

  # https://github.com/zeromq/libzmq/issues/4160
  # manpages aren't created when using cmake, so this script does the job
  cd "$pkgname/doc"
  for FILE in ./*.txt; do
    asciidoc \
      -d manpage \
      -b docbook \
      -f asciidoc.conf \
      -azmq_version="$pkgver" \
      "${FILE}"

    xmlto man "${FILE%.txt}.xml"
  done
}

check() {
  # -j1: due to segfaults when running simultaneous tests
  ctest \
    --test-dir build \
    --output-on-failure \
    -j1 \
    -E "test_(diffserv|zmq|ppoll_fd|zmq_poll_fd|connect_rid|heartbeats|radio_dish)"
}

package() {
  DESTDIR="$pkgdir" cmake --install build

  # manpages
  cd "$pkgname"
  install -vDm644 -t "$pkgdir/usr/share/man/man3" doc/*.3
  install -vDm644 -t "$pkgdir/usr/share/man/man7" doc/*.7
}
