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

_realname=sox-ng
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=14.6.0.4
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"
license=("GPL")
makedepends=(autoconf-archive git
             ${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}-id3lib
             ${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}-omp)
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=('faf376b7054eac6da3c6c15897a42a8fd22147d5d5ea05921ff1afa1f4e7a7ab'
            '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
}
