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

_realname=libmysofa
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.2
pkgrel=1
pkgdesc="Reader for AES SOFA files to get better HRTFs (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/hoene/libmysofa/"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cunit"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
license=('BSD')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/hoene/libmysofa/archive/v${pkgver}.tar.gz"
        001-mingw-dont-use-nuget.patch
        002-fix-exports.patch
        003-install-import-library.patch
        004-fix-dllexport-attribute.patch
        005-add-cflag-private-for-staticlib.patch)
sha256sums=('7638dec8ba2b5408cf32ab41d37cb29460095c4b2161a860480c122e71806ba9'
            'f7c1f72dac366fd11502cf7176aa5133075f39b0e375522d00fc1cb827189378'
            '5cac782837366aa0fabe5e87c0f27ef9ed7a476aed7649781994cfe108eb0894'
            'ba7a00f9cac4d9175480dcbe3d62ae661d9d876acad0e2b4ca56d0c104f8111f'
            '4b9b469cca4b5f22a1005eddb6db6124b6e3932eaa2638a54e09d8ab005dc084'
            '665bd53bfe3cdface288553ebd1b8d07e78762475add4971413a5703790ea045')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-mingw-dont-use-nuget.patch
  patch -p1 -i ${srcdir}/002-fix-exports.patch
  patch -p1 -i ${srcdir}/003-install-import-library.patch
  patch -p1 -i ${srcdir}/004-fix-dllexport-attribute.patch
  patch -p1 -i ${srcdir}/005-add-cflag-private-for-staticlib.patch
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    "../${_realname}-${pkgver}"

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

package() {
  cd "${srcdir}"/build-${CARCH}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}
  cp ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/
}
