# Maintainer: Developer <dev@opalos.org>
# Maintainer: Developer <dev@opalos.org>

pkgbase=pulseaudio
pkgname=(
  pulseaudio
  libpulse
  pulseaudio-zeroconf
  pulseaudio-lirc
  pulseaudio-jack
  pulseaudio-bluetooth
  pulseaudio-equalizer
  pulseaudio-rtp
)
pkgver=%version%
pkgrel=%release%
pkgdesc="A featureful, general-purpose sound server"
url="https://www.freedesktop.org/wiki/Software/PulseAudio/"
arch=(x86_64)
license=(LGPL-2.1-or-later)
makedepends=(
  alsa-lib
  attr
  avahi
  bluez
  bluez-libs
  check
  dbus
  doxygen
  fftw
  git
  gst-plugins-base-libs
  gtk3
  jack2
  libasyncns
  libcap
  libsm
  libsndfile
  libsoxr
  libxtst
  lirc
  meson
  openssl
  orc
  rtkit
  sbc
  speexdsp
  systemd
  tdb
  valgrind
  webrtc-audio-processing-1
  xmltoman
)
_commit=1f020889c9aa44ea0f63d7222e8c2b62c3f45f68  # tags/v17.0^0
source=("git+https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git#commit=$_commit")
b2sums=('SKIP')

pkgver() {
  cd pulseaudio
  git describe --tags | sed 's/^v//;s/[^-]*-g/r&/;s/-/+/g'
}

prepare() {
  cd pulseaudio

  # Freeze version before patching
  ./git-version-gen doesnt-exist >.tarball-version

  # Fix crashes with some UCM devices
  # https://gitlab.archlinux.org/archlinux/packaging/packages/pulseaudio/-/issues/4
  git cherry-pick -n f5cacd94abcc47003bd88ad7ca1450de649ffb15
  git cherry-pick -n ed3d4f0837f670e5e5afb1afa5bcfc8ff05d3407
}

build() {
  local meson_options=(
    -D consolekit=disabled
    -D elogind=disabled
    -D pulsedsp-location='/usr/\$LIB/pulseaudio'
    -D stream-restore-clear-old-devices=true
    -D tcpwrap=disabled
    -D udevrulesdir=/usr/lib/udev/rules.d
  )

  arch-meson pulseaudio build "${meson_options[@]}"
  meson compile -C build
}

check() {
  meson test -C build --print-errorlogs
  ninja -C build test-daemon
}

_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
}

package_pulseaudio() {
  depends=(
    alsa-lib
    fftw
    libcap
    libltdl
    "libpulse=$pkgver-$pkgrel"
    libsm
    libsoxr
    libxtst
    orc
    rtkit
    speexdsp
    tdb
    webrtc-audio-processing-1
  )
  optdepends=(
    'pulseaudio-alsa: ALSA configuration (recommended)'
    'pulseaudio-bluetooth: Bluetooth support'
    'pulseaudio-equalizer: Graphical equalizer'
    'pulseaudio-jack: Jack support'
    'pulseaudio-lirc: IR (lirc) support'
    'pulseaudio-rtp: RTP and RAOP support'
    'pulseaudio-zeroconf: Zeroconf support'
  )
  provides=(pulse-native-provider)
  conflicts=(pipewire-pulse)
  backup=(
    etc/pulse/daemon.conf
    etc/pulse/default.pa
    etc/pulse/system.pa
  )
  install=pulseaudio.install
  replaces=(
    'pulseaudio-gconf<=11.1'
    'pulseaudio-xen<=9.0'
  )

  meson install -C build --destdir "$pkgdir"

  cd "$pkgdir"

  # Superseded by socket activation
  sed -e '/autospawn/iautospawn = no' \
      -i etc/pulse/client.conf

  # Disable cork-request module, can result in e.g. media players unpausing
  # when there's a Skype call incoming
  sed -e 's|/usr/bin/pactl load-module module-x11-cork-request|#&|' \
      -i usr/bin/start-pulseaudio-x11

  # Required by qpaeq
  sed -e '/Load several protocols/aload-module module-dbus-protocol' \
      -i etc/pulse/default.pa

  # Avoid warning about nonexistent dir
  # https://gitlab.archlinux.org/archlinux/packaging/packages/pulseaudio/-/issues/3
  mkdir -p etc/pulse/{default,system}.pa.d

  # Split packages
  _pick libpulse etc/pulse/client.conf
  _pick libpulse usr/bin/pa{cat,ctl,dsp,mon,play,rec,record}
  _pick libpulse usr/lib/libpulse{,-simple,-mainloop-glib}.so*
  _pick libpulse usr/lib/{cmake,pkgconfig}
  _pick libpulse usr/lib/pulseaudio/libpulse{dsp,common-*}.so
  _pick libpulse usr/include
  _pick libpulse usr/share/man/man1/pa{cat,ctl,dsp,mon,play,rec,record}.1
  _pick libpulse usr/share/man/man5/pulse-client.conf.5
  _pick libpulse usr/share/bash-completion/completions/pa{cat,ctl,dsp,play,rec,record}
  _pick libpulse usr/share/bash-completion/completions/pulseaudio
  _pick libpulse usr/share/vala
  _pick libpulse usr/share/zsh

  local moddir=usr/lib/pulseaudio/modules

  _pick zeroconf $moddir/libavahi-wrap.so
  _pick zeroconf $moddir/module-zeroconf-{publish,discover}.so
  _pick zeroconf $moddir/module-raop-discover.so

  _pick lirc $moddir/module-lirc.so

  _pick jack $moddir/module-jack-{sink,source}.so
  _pick jack $moddir/module-jackdbus-detect.so

  _pick bluetooth $moddir/libbluez5-util.so
  _pick bluetooth $moddir/module-bluetooth-{discover,policy}.so
  _pick bluetooth $moddir/module-bluez5-{discover,device}.so

  _pick equalizer $moddir/module-equalizer-sink.so
  _pick equalizer usr/bin/qpaeq

  _pick rtp $moddir/lib{rtp,raop}.so
  _pick rtp $moddir/module-rtp-{send,recv}.so
  _pick rtp $moddir/module-raop-sink.so
}

package_libpulse() {
  pkgdesc="$pkgdesc (client library)"
  depends=(
    dbus
    libasyncns
    libsndfile
    libxcb
    systemd
  )
  optdepends=(
    'glib2: mainloop integration'
    'pulse-native-provider: PulseAudio backend'
  )
  provides=(
    libpulse-mainloop-glib.so
    libpulse-simple.so
    libpulse.so
  )
  license=(LGPL)
  backup=(etc/pulse/client.conf)

  mv libpulse/* "$pkgdir"
}

package_pulseaudio-zeroconf() {
  pkgdesc="Zeroconf support for PulseAudio"
  depends=(
    avahi
    "pulseaudio=$pkgver-$pkgrel"
  )

  mv zeroconf/* "$pkgdir"
}

package_pulseaudio-lirc() {
  pkgdesc="IR (lirc) support for PulseAudio"
  depends=(
    lirc
    "pulseaudio=$pkgver-$pkgrel"
  )

  mv lirc/* "$pkgdir"
}

package_pulseaudio-jack() {
  pkgdesc="Jack support for PulseAudio"
  depends=(
    jack
    "pulseaudio=$pkgver-$pkgrel"
  )

  mv jack/* "$pkgdir"
}

package_pulseaudio-bluetooth() {
  pkgdesc="Bluetooth support for PulseAudio"
  depends=(
    bluez
    bluez-libs
    gst-plugins-base-libs
    "pulseaudio=$pkgver-$pkgrel"
    sbc
  )
  optdepends=('gst-plugins-bad: AptX and LDAC support')

  mv bluetooth/* "$pkgdir"
}

package_pulseaudio-equalizer() {
  pkgdesc="Graphical equalizer for PulseAudio"
  depends=(
    "pulseaudio=$pkgver-$pkgrel"
    python-dbus
    python-pyqt5
  )

  mv equalizer/* "$pkgdir"
}

package_pulseaudio-rtp() {
  pkgdesc="RTP and RAOP support for PulseAudio"
  depends=(
    gst-plugins-base
    gst-plugins-base-libs
    gst-plugins-good
    openssl
    "pulseaudio=$pkgver-$pkgrel"
  )

  mv rtp/* "$pkgdir"
}

# vim:set sw=2 et:
