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

_realname=audaspace
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_base_ver=1.3.0
_commit='44b57af54686dc1f860ac75c9352b6c77646a14f'
pkgver=1.3.0.r638
pkgrel=1
pkgdesc="A high level audio library written in C++ with language bindings for Python (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=('Apache')
depends=("${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-SDL2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
url="https://audaspace.github.io/"
source=(#${_realname}-${pkgver}.tar.gz::https://github.com/audaspace/audaspace/archive/v${pkgver}.tar.gz
        ${_realname}-${_base_ver}::git+https://github.com/neXyon/audaspace.git#commit=${_commit}
        0100-mingw-relocate.patch
        0101-mingw-exports.patch
        0102-mingw-python.patch)
options=('!strip' 'staticlibs')
sha256sums=('SKIP'
            'cd6d30da54ad32812d52713f55bd3c1a8591397aeb33c92809de56fa731506d0'
            '8cf2aefb1973177ec3d75443e47ba95c66ca113076e615af0a8f6c8de1398da0'
            'fdca455afd19939c6105370a5eccb9e6c6eb09b27b37b5039dbd2b75016a1d0e')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

pkgver() {
  cd "${srcdir}/${_realname}-${_base_ver}"
  local _rev=0
  printf "%s.r%s" ${_base_ver} "$(git rev-list --count $_commit)"
}

prepare() {
  cd ${_realname}-${_base_ver}
  
  apply_patch_with_msg \
    0100-mingw-relocate.patch \
    0101-mingw-exports.patch \
    0102-mingw-python.patch
}

build() {
  declare -a _btype
  if check_option "debug" "y"; then
    _btype=Debug
  else
    _btype=Release
  fi

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=${_btype} \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    -DPython3_ROOT_DIR=${MINGW_PREFIX} \
    -DPython3_FIND_REGISTRY=NEVER \
    ../${_realname}-${_base_ver}
  make
}

package() {
  cd "${srcdir}//build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} -j1 install
  install -Dm644 "${srcdir}/${_realname}-${_base_ver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
