# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=opus
pkgbase=mingw-w64-jellyfin-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-jellyfin-${_realname}")
pkgver=1.6
pkgrel=1
pkgdesc="Codec designed for interactive speech and audio transmission over the Internet (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.opus-codec.org/"
msys2_repository_url="https://github.com/xiph/opus"
msys2_references=(
  "cpe: cpe:2.3:a:opus-codec:opus"
)
license=('spdx:BSD-3-Clause')
makedepends=(
    "${MINGW_PACKAGE_PREFIX}-cc"
    "${MINGW_PACKAGE_PREFIX}-doxygen"
    "${MINGW_PACKAGE_PREFIX}-meson"
    "${MINGW_PACKAGE_PREFIX}-ninja"
)
source=("https://downloads.xiph.org/releases/opus/opus-${pkgver}.tar.gz"
        001-aarch64-features.patch)
sha256sums=('b7637334527201fdfd6dd6a02e67aceffb0e5e60155bbd89175647a80301c92c'
            'a579c6d0e80632bfc144e4338a560f1a4f45b1a9b431aa71d0ea4c7259bdbe05')

export MINGW_TOOLCHAIN_PREFIX="${MINGW_PREFIX}"
export FF_MINGW_PREFIX="${MINGW_TOOLCHAIN_PREFIX}/ffbuild"

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  apply_patch_with_msg \
    001-aarch64-features.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  # -Dasm is for ARM fixed point, not neon or x86 avx/sse
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_TOOLCHAIN_PREFIX}/bin/meson.exe setup \
      --prefix="${FF_MINGW_PREFIX}" \
      --wrap-mode=nodownload \
      --auto-features=enabled \
      --buildtype=plain \
      --default-library=static \
      -Dasm=disabled \
      -Dcustom-modes=true \
      ../${_realname}-${pkgver}

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

check() {
  :
}

package_opus() {
  ${MINGW_TOOLCHAIN_PREFIX}/bin/meson.exe install -C "${srcdir}/build-${MSYSTEM}" --destdir "${pkgdir}"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/opus.m4" "${pkgdir}${FF_MINGW_PREFIX}/share/aclocal/opus.m4"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}"/COPYING "${pkgdir}${FF_MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-jellyfin-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
