# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=aubio
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.4.9
pkgrel=9
pkgdesc='A tool for extracting annotations from audio signals (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://aubio.org/'
msys2_references=(
  "cpe: cpe:/a:aubio:aubio"
)
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-ffmpeg7.1"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-libsamplerate"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-waf"
             "python")
source=(https://aubio.org/pub/aubio-${pkgver}.tar.bz2
        0001-fix-AUBIO_STRERROR-definition.patch
        0002-wscript-disable-tests.patch
        0003-waf-disable-def-install.patch
        0004-wscript-fix-implib-name.patch
        https://github.com/aubio/aubio/commit/8a05420e5dd8c7b8b2447f82dc919765876511b3.patch
        https://github.com/aubio/aubio/commit/cdfe9cef.patch
        https://github.com/aubio/aubio/commit/245deead.patch
        https://github.com/aubio/aubio/commit/0b947f96.patch
        https://github.com/aubio/aubio/commit/53bc55cd.patch
        ffmpeg7.patch
)
sha256sums=('d48282ae4dab83b3dc94c16cf011bcb63835c1c02b515490e1883049c3d1f3da'
            'c504cc0f6a80588cec94941fa9a1bb6338aea616f70a1ee1074ef19cb16c82da'
            '3c9da6fcaab3a96afeff50db8979920d8fcb7749cdb5d34400e2a6275fff614a'
            '09df1bdba2e3b34251a746380e65967bdf38a70182315b077b27aacdeab7672e'
            'f53a1b4ae636d4ec03a038535479bd83f8a37ceb3d3b0f8f37697efb7b0b1329'
            '33b730d1aa0562d5cb96341fce268acb8a42a0381e9dcd52caef0fc2737b8ad0'
            '025b6d0668601a17ce8312ad4930d7ba99715b754191208347df46b9a07b0a91'
            'ed8b8c76867884ff97e7df6d6992c9df6fd6396f002e103f7718ab978daff417'
            '1eb42cc2c51ca67ca6d54d2f223c3c3775041bf2743ccdddddd325e9365dc301'
            'e0d4ef1d61143130f919682b5288ad3030d9472f2c3b91d5c2703f9ec71e962d'
            'faf721a3bfb6785f8854d71cc62f5ad1807dd433ace8bd9553c27d5701cc5d7c')

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

  # https://github.com/aubio/aubio/pull/349
  patch -p1 -i "${srcdir}/0001-fix-AUBIO_STRERROR-definition.patch"

  patch -p1 -i "${srcdir}/0002-wscript-disable-tests.patch"
  patch -p1 -i "${srcdir}/0003-waf-disable-def-install.patch"
  patch -p1 -i "${srcdir}/0004-wscript-fix-implib-name.patch"

  # https://github.com/aubio/aubio/commit/8a05420e5dd8c7b8b2447f82dc919765876511b3
  patch -p1 -i "${srcdir}/8a05420e5dd8c7b8b2447f82dc919765876511b3.patch"

  # ffmpeg 7
  patch -p1 -i "${srcdir}/cdfe9cef.patch"
  patch -p1 -i "${srcdir}/245deead.patch"
  patch -p1 -i "${srcdir}/0b947f96.patch"
  patch -p1 -i "${srcdir}/53bc55cd.patch"
  # from Arch
  patch -p1 -i "${srcdir}/ffmpeg7.patch"

  rm -rv waflib
}

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

  export WAF_NO_PREFORK=1

  # Build against ffmpeg 7.1
  export PKG_CONFIG_PATH="${MINGW_PREFIX}/lib/ffmpeg7.1/pkgconfig:${PKG_CONFIG_PATH}"

  # https://github.com/aubio/aubio/issues/402
  CFLAGS+=" -Wno-incompatible-pointer-types"

  LDFLAGS+=" -pthread" \
  DEST_OS=win32 \
  TARGET=${MINGW_CHOST} \
  PKG_CONFIG=${MINGW_PREFIX}/bin/pkgconf \
  AR=${MINGW_PREFIX}/bin/ar \
  WINDRES=${MINGW_PREFIX}/bin/windres \
  /usr/bin/python "${MINGW_PREFIX}/bin/waf" configure \
    --prefix=${MINGW_PREFIX} \
    --check-c-compiler=${CC} \
    --with-target-platform=win32 \
    --enable-fftw3 \
    --disable-docs \
    --disable-tests \
    --notests \
    --out="${srcdir}/build-${MSYSTEM}"

  /usr/bin/python "${MINGW_PREFIX}/bin/waf" build
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"
  /usr/bin/python "${MINGW_PREFIX}/bin/waf" install --destdir="${pkgdir}"
  mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}/bin/"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
