# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=sox-ng
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=14.6.1
pkgrel=1
pkgdesc="SoX is the Swiss Army Knife of sound processing utilities (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://codeberg.org/sox_ng/sox_ng"
msys2_changelog_url='https://codeberg.org/sox_ng/sox_ng/src/branch/main/ChangeLog'
msys2_references=(
  'anitya: 373981'
  'aur: sox-ng-git'
  # Gentoo uses sox-ng despite the sox name without ng appendix.
  'gentoo: media-sound/sox'
)
license=("spdx:GPL-2.0-only", "spdx:LGPL-2.1-only")
makedepends=(autoconf-archive
             ${MINGW_PACKAGE_PREFIX}-autotools
             ${MINGW_PACKAGE_PREFIX}-cc
             ${MINGW_PACKAGE_PREFIX}-pkgconf)
depends=(${MINGW_PACKAGE_PREFIX}-cc-libs
             ${MINGW_PACKAGE_PREFIX}-flac
             ${MINGW_PACKAGE_PREFIX}-gsm
             ${MINGW_PACKAGE_PREFIX}-lame
             ${MINGW_PACKAGE_PREFIX}-libao
             ${MINGW_PACKAGE_PREFIX}-libid3tag
             ${MINGW_PACKAGE_PREFIX}-libmad
             ${MINGW_PACKAGE_PREFIX}-libpng
             ${MINGW_PACKAGE_PREFIX}-libsndfile
             ${MINGW_PACKAGE_PREFIX}-libltdl
             ${MINGW_PACKAGE_PREFIX}-libvorbis
             ${MINGW_PACKAGE_PREFIX}-opencore-amr
             ${MINGW_PACKAGE_PREFIX}-opusfile
             ${MINGW_PACKAGE_PREFIX}-twolame
             ${MINGW_PACKAGE_PREFIX}-vo-amrwbenc
             ${MINGW_PACKAGE_PREFIX}-wavpack
             ${MINGW_PACKAGE_PREFIX}-speex
             ${MINGW_PACKAGE_PREFIX}-speexdsp
             ${MINGW_PACKAGE_PREFIX}-fftw
             ${MINGW_PACKAGE_PREFIX}-omp)
optdepends=("${MINGW_PACKAGE_PREFIX}-ffmpeg: to decode otherwise unsupported formats")
source=("https://codeberg.org/sox_ng/sox_ng/releases/download/sox_ng-${pkgver}/sox_ng-${pkgver}.tar.gz"
        "0002-fix-missing-exports.patch"
        "0003-missing-include.patch")
sha256sums=('bb03126de6b3ce0049801466f067097eb73f8a2fc11e9239ed00744f42691145'
            '11f7ecd26f857cd4b2c84a849446bac1ec6b3a77f8e54fb13c593feca6fdd54f'
            'f9993ca00f531a4620162e5c138f632bf31f3aedb09a8658a1cec7b6dc706d1e')

prepare() {
  cd "sox_ng-${pkgver}"

  patch -p1 -i "${srcdir}/0002-fix-missing-exports.patch"
  patch -p1 -i "${srcdir}/0003-missing-include.patch"

  autoreconf -i
}

build() {
  # Silence format warnings originating from usage of PRIu64 and PRIuPTR within lsx_warn, lsx_debug and lsx_fail
  export CFLAGS="$CFLAGS -Wno-format"

  export lt_cv_deplibs_check_method='pass_all'

  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  CPPFLAGS+=" -DFLAC__NO_DLL" \
  ../sox_ng-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-shared \
    --enable-static \
    --with-distro='MSYS2 MINGW-packages'

  make -j1

  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  ../sox_ng-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --disable-static \
    --with-distro='MSYS2 MINGW-packages'

  make -j1
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  make DESTDIR="${pkgdir}" install

  cd "${srcdir}/build-${MSYSTEM}-shared"
  make DESTDIR="${pkgdir}" install
}
