# Maintainer: Wolfgang Stöggl <c72578@yahoo.de>

_realname=sox
_base_ver=14.4.2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
# https://sourceforge.net/p/sox/code/ci/f3094754a7c2a7e55c35621d20fa9945736e72df/
pkgver=14.4.2.r3239.f3094754
pkgrel=1
pkgdesc="SoX is the Swiss Army Knife of sound processing utilities (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://sourceforge.net/projects/sox/"
msys2_references=(
  "cpe: cpe:/a:sox_project:sox"
)
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)
source=("${_realname}"::"git+https://git.code.sf.net/p/sox/code#commit=f3094754a7c2a7e55c35621d20fa9945736e72df"
        "0001-ucrt-no-rewind-pipe.patch")
sha256sums=('affffe2c3901b4859beb41bfc1d663ab959c968a3635a448359d50b1142a3d6a'
            '693e7dcd737ea6c5137f2827518ff08549841d480cde00c4f3f505b2657c71f0')

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "%s.r%s.%s" "${_base_ver}" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

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

  patch -Np1 -i "${srcdir}/0001-ucrt-no-rewind-pipe.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" \
  ../${_realname}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-shared \
    --enable-static \
    --with-distro='MSYS2 MINGW-packages'

  make

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

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

  make
}

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

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