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

_realname=audaspace
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.7.0
pkgrel=1
pkgdesc="A high level audio library written in C++ with language bindings for Python (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://audaspace.github.io/"
license=('spdx:Apache-2.0')
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}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/audaspace/audaspace/archive/v${pkgver}.tar.gz"
        0100-mingw-relocate.patch
        0101-mingw-exports.patch
        0102-mingw-python.patch
        https://github.com/audaspace/audaspace/commit/fa006ac74c505c7c4e969cc3e5537c979b581cc9.patch)
sha256sums=('e1c1283276da7b484863c9b191140b4508fe0b79557234bf88419b793eb743a8'
            '2f007f5a2ea5cc1d186608e9e65e927e6659622ef0e652a8fda75a8327a0800c'
            '8cf2aefb1973177ec3d75443e47ba95c66ca113076e615af0a8f6c8de1398da0'
            '16f8833e51b6e97019842297286c713e270bc9fe836950b93d10842d99d8c89c'
            'd033bfdf24c3de5ce69737a9d05aa9fe8cf83d2d5cc2e69d273546aaf5357812')

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

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

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

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -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}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}//build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
