# Maintainer: Jeff Hubbard <musikernel@gmail.com>

_realname=rubberband
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.3.0
pkgrel=1
pkgdesc="High quality software library for audio time-stretching and pitch-shifting. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://breakfastquay.com/rubberband/"
license=('spdx:GPL-2.0-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-boost")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-libsamplerate"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-ladspa-sdk"
         "${MINGW_PACKAGE_PREFIX}-lv2"
         "${MINGW_PACKAGE_PREFIX}-vamp-plugin-sdk")
source=("https://breakfastquay.com/files/releases/${_realname}-${pkgver}.tar.bz2"
        001-use-system-memory-barrier-on-mingw.patch)
sha256sums=('d9ef89e2b8ef9f85b13ac3c2faec30e20acf2c9f3a9c8c45ce637f2bc95e576c'
            'c66b22f94ace3fd82c24be5ec87069f44410fa5dbd43546cd811816c324fcb8f')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ../001-use-system-memory-barrier-on-mingw.patch
}
build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("--buildtype=release")
  else
    extra_config+=("--buildtype=debug")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    --prefix="${MINGW_PREFIX}" \
    "${extra_config[@]}" \
    --wrap-mode=nodownload \
    --default-library=both \
    --auto-features=enabled \
    -Dfft=fftw \
    -Dresampler=libsamplerate \
    -Djni=disabled \
    -Dtests=disabled \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/meson.exe compile
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/meson.exe setup --reconfigure -Dtests=enabled "../${_realname}-${pkgver}"
  ${MINGW_PREFIX}/bin/meson.exe compile
  ${MINGW_PREFIX}/bin/meson.exe test || warning "Tests failed"
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson.exe install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
