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

pkgbase=rubberband
pkgname=(rubberband rubberband-{ladspa,lv2,vamp})
pkgver=%version%
pkgrel=%release%
pkgdesc="Time-stretching and pitch-shifting audio library and utility"
arch=(x86_64)
url="https://www.breakfastquay.com/rubberband/"
license=(GPL2)
makedepends=(
  boost
  fftw
  java-environment
  ladspa
  lv2
  meson
  libsamplerate
  libsndfile
  vamp-plugin-sdk
)
source=($pkgbase-$pkgver.tar.gz::https://github.com/breakfastquay/$pkgbase/archive/v$pkgver.tar.gz)
sha512sums=('6d7ce80f47a5870920748d6e2ff9425f9d90e3fd2d62d7b937158ad2134829bc1d1e34ec4fd6327de5d6f1924b4bb793dc4c9d10574102e11338383c4522ba84')
b2sums=('09002ddb20dc34ea3bceb3d35468cd6b5398e41808c8e9ef9346e5fb3a3544b725c7da6c6bce80e6b9a2b58e1f55faea83d365b255852d3b953f5134dfc6445e')

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

build() {
  local java_major=$(java --version 2>/dev/null |grep 'openjdk'| cut -d ' ' -f2| cut -d '.' -f1)
  local meson_options=(
    -Dfft=fftw
    -Dresampler=libsamplerate
    -Dextra_include_dirs="/usr/lib/jvm/java-$java_major-openjdk/include,/usr/lib/jvm/java-$java_major-openjdk/include/linux"
  )
  export JAVA_HOME="/usr/lib/jvm/java-$java_major-openjdk"

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

check() {
  meson test -C build
}

package_rubberband() {
  depends=(
    fftw libfftw3.so
    gcc-libs
    glibc
    libsamplerate libsamplerate.so
    libsndfile libsndfile.so
  )
  provides=(
    librubberband.so
    librubberband-jni.so
  )

  meson install -C build --destdir "$pkgdir"
  # rubberband is unstripped: https://github.com/breakfastquay/rubberband/issues/72
  strip "$pkgdir/usr/bin/$pkgbase"
  install -vDm 644 $pkgbase-$pkgver/{CHANGELOG,README.md} -t "$pkgdir/usr/share/doc/$pkgbase/"

  (
    cd "$pkgdir"
    _pick $pkgbase-ladspa usr/lib/ladspa
    _pick $pkgbase-lv2 usr/lib/lv2
    _pick $pkgbase-vamp usr/lib/vamp
  )
}

package_rubberband-ladspa() {
  pkgdesc+=" - LADSPA plugin"
  groups=(
    pro-audio
    ladspa-plugins
  )
  depends=(
    fftw libfftw3.so
    gcc-libs
    glibc
    libsamplerate libsamplerate.so
    ladspa-host
  )

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

package_rubberband-lv2() {
  pkgdesc+=" - LV2 plugin"
  groups=(
    pro-audio
    lv2-plugins
  )
  depends=(
    fftw libfftw3.so
    gcc-libs
    glibc
    libsamplerate libsamplerate.so
    lv2-host
  )

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

package_rubberband-vamp() {
  pkgdesc+=" - VAMP plugin"
  groups=(
    pro-audio
    vamp-plugins
  )
  depends=(
    fftw libfftw3.so
    gcc-libs
    glibc
    libsamplerate libsamplerate.so
    vamp-host
    vamp-plugin-sdk
  )

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