# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: Ray Rashif <schiv@archlinux.org>
# Contributor: damir <damir@archlinux.org>

pkgname=fluidsynth
pkgver=2.5.4
pkgrel=1
pkgdesc="A real-time software synthesizer based on the SoundFont 2 specifications"
arch=(x86_64 aarch64)
url="https://www.fluidsynth.org/"
_url="https://github.com/fluidsynth/fluidsynth"
license=(LGPL-2.1-or-later)
groups=(pro-audio)
depends=(
  glibc
  libgcc
  libgomp
  libstdc++
  sdl3
)
makedepends=(
  alsa-lib
  cmake
  dbus
  doxygen
  glib2
  jack
  ladspa
  libpipewire
  libpulse
  libsndfile
  portaudio
  readline
  systemd-libs
)
provides=(
  libfluidsynth.so
  soundfont-synthesizer
)
backup=(etc/conf.d/$pkgname)
source=(
  $pkgname-$pkgver.tar.gz::$_url/archive/v$pkgver.tar.gz
)
sha512sums=('7539e32a56309ce61c4178dad38ac8aae82ebab37398e1a7b6d1e2d5abd0af73ae7af35358e655a935666c7e6fde30885a0d6e4b7ad8b129ab02f6aad8f18dd0')
b2sums=('a0c9d228d71479436b78ecd34c3e3df4fa58599189b929f1497afaa1add2ce757cdc957f3a2c9044545ca36d4886b080ea1f3aa2ec6596ea38a68af74fa6b1b7')

build() {
  local cmake_options=(
    -B build
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_INSTALL_PREFIX=/usr
    -D FLUID_DAEMON_ENV_FILE=/etc/conf.d/fluidsynth
    -D LIB_SUFFIX=""
    -D enable-ladspa=ON
    -D enable-portaudio=ON
    -S $pkgname-$pkgver
    -W no-dev
  )

  cmake "${cmake_options[@]}"
  cmake --build build --verbose
}

# The tests are flaky (or racy ?)
# it's also possible that the issue is not from fluidsynth
# but inside a dep... hard to say.
# They tend to block forever. When ran under strace,
# they will never block making things harder to debug.
# Ran overnight the following command without issue:
#  while /bin/true; do strace -fF -o /tmp/strace.log ./test_synth_process ; sleep 5s; done
# so disable the tests
#check() {
#  make check -k -C build
#}

package() {
  depends+=(
    alsa-lib libasound.so
    dbus libdbus-1.so
    glib2 libglib-2.0.so libgmodule-2.0.so
    jack libjack.so
    libpipewire libpipewire-0.3.so
    libpulse libpulse-simple.so
    libsndfile libsndfile.so
    portaudio libportaudio.so
    readline libreadline.so
    systemd-libs libsystemd.so
  )

  DESTDIR="$pkgdir" cmake --install build
  install -vDm 644 build/$pkgname.service -t "$pkgdir/usr/lib/systemd/user/"
  install -vDm 644 build/$pkgname.conf "$pkgdir/etc/conf.d/$pkgname"
  install -vDm 644 $pkgname-$pkgver/{AUTHORS,{CONTRIBUTING,README}.md,THANKS,TODO} -t "$pkgdir/usr/share/doc/$pkgname/"
}
