# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=portaudio
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=190600_20161030
pkgrel=3
pkgdesc="A free, cross-platform, open source, audio I/O library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="http://www.portaudio.com"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=(!libtool strip staticlibs)
source=("http://www.portaudio.com/archives/pa_stable_v${pkgver}.tgz"
        "fix-build.patch"
        "wdmks-wasapi-dsound.patch")
sha256sums=('f5a21d7dcd6ee84397446fa1fa1a0675bb2e8a4a6dceb4305a8404698d8d1513'
            '77e8438d482ad03baa5c23afbe293e8cbdb95287ff6a489515bde61231e78525'
            'fd62577c6fcf2811618701d49c68d6f0de515fad26f644ccacce57a16cf11bbc'
            )

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

  patch -p0 -i ${srcdir}/fix-build.patch
  patch -p0 -i ${srcdir}/wdmks-wasapi-dsound.patch

  autoconf
}

# In order to build with ASIO-support, download the ASIOSDK2.3 from Steinberg, add 
# the path below and add "asio" to --with-winapi. Both for shared and static, e.g:
#    --with-asiodir=${HOME}/ASIOSDK2.3 \
#    --with-winapi=wmme,directx,wasapi,wdmks,asio
# Unfortunately each build creates artefacts in the ASIO-SDK (.libs) that conflict 
# with the subsequent build (shared/static, 32-/64-bit. You may need to clean out
# manually or replace the SDK with a fresh copy. You may also need to provice two
# separate copies for asiodir in the shared and static build.

build() {
  export lt_cv_deplibs_check_method='pass_all'

  [[ -d "build-static-${MINGW_CHOST}" ]] && rm -rf "build-static-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-static-${MINGW_CHOST}"
  cd "${srcdir}/build-static-${MINGW_CHOST}"

  ../${_realname}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-shared \
    --enable-static \
    --with-dxdir=${MINGW_PREFIX}/${MINGW_CHOST} \
    --with-winapi=wmme,directx,wasapi,wdmks

  make

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

  ../${_realname}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-shared \
    --with-dxdir=${MINGW_PREFIX}/${MINGW_CHOST} \
    --with-winapi=wmme,directx,wasapi,wdmks

  make
}

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

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