# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=libsoxr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.1.3
pkgrel=2
pkgdesc="The SoX Resampler library that aims to give fast and high quality results for any constant resampling ratio (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://sourceforge.net/p/soxr/wiki/Home/"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "git")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
source=("git+https://git.code.sf.net/p/soxr/code#commit=945b592b70470e29f917f4de89b4281fbbd540c0"
        "0001-libsoxr-fix-pc-file-installation.patch"
        "0002-libsoxr-fix-documentation-installation.patch"
        "LICENSE-PFFFT")
sha256sums=('SKIP'
            'f6715ef431510e34da2644eb05893e3c25804dc3e9bab2819786987b9f2e2b1f'
            '7c16beb3898279db96b09b365c069071b371a1022f0baf12bb3da9d45d387fdf'
            'a9f3d236f3c14a41a1293f176d4c7c46d85404403417fcb8df6d6c9707104e42')

prepare() {
  cd "${srcdir}/code"
  patch -Np1 -i "${srcdir}/0001-libsoxr-fix-pc-file-installation.patch"
  patch -Np1 -i "${srcdir}/0002-libsoxr-fix-documentation-installation.patch"
}

build() {
  # Build shared
  [[ -d "build-${MINGW_CHOST}-shared" ]] && rm -rf "build-${MINGW_CHOST}-shared"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-shared"
  cd "${srcdir}/build-${MINGW_CHOST}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
  -G"MSYS Makefiles" \
  -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
  -DCMAKE_BUILD_TYPE=Release \
  -DBUILD_EXAMPLES=OFF \
  -DBUILD_SHARED_LIBS=ON \
  -DWITH_AVFFT=OFF \
  -DWITH_LSR_BINDINGS=ON \
  -DWITH_OPENMP=ON \
  -DWITH_PFFFT=ON \
  ../code
  
  make
  
  # Build static
  [[ -d "build-${MINGW_CHOST}-static" ]] && rm -rf "build-${MINGW_CHOST}-static"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-static"
  cd "${srcdir}/build-${MINGW_CHOST}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
  -G"MSYS Makefiles" \
  -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
  -DCMAKE_BUILD_TYPE=Release \
  -DBUILD_EXAMPLES=OFF \
  -DBUILD_SHARED_LIBS=OFF \
  -DWITH_AVFFT=OFF \
  -DWITH_LSR_BINDINGS=ON \
  -DWITH_OPENMP=ON \
  -DWITH_PFFFT=ON \
  ../code
  
  make
}

package() {
  # Install shared
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  make DESTDIR=${pkgdir} install

  # Install static
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  make DESTDIR=${pkgdir} install
  
  # License
  install -Dm644 ${srcdir}/code/LICENCE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  install -Dm644 ${srcdir}/code/COPYING.LGPL ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/
  install -Dm644 ${srcdir}/LICENSE-PFFFT ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/
}
