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

_realname=libsndfile
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.0.31
pkgrel=1
pkgdesc="A C library for reading and writing files containing sampled sound (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="http://www.mega-nerd.com/libsndfile"
license=('LGPL2.1')
depends=("${MINGW_PACKAGE_PREFIX}-flac"
         "${MINGW_PACKAGE_PREFIX}-libogg"
         "${MINGW_PACKAGE_PREFIX}-libvorbis"
         "${MINGW_PACKAGE_PREFIX}-opus")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
options=('staticlibs' 'strip')
source=("https://github.com/libsndfile/libsndfile/releases/download/${pkgver}/${_realname}-${pkgver}.tar.bz2"{,.sig})
sha256sums=('a8cfb1c09ea6e90eff4ca87322d4168cdbe5035cb48717b40bf77e751cc02163'
            'SKIP')
validpgpkeys=('31D95CAB6D80D262244A1750A47620E801E47E95')

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  msg2 "Build static for ${MINGW_CHOST}"
  [[ -d "${srcdir}/build-${MINGW_CHOST}-static" ]] && rm -rf "${srcdir}/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 "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DBUILD_SHARED_LIBS=OFF \
    ../${_realname}-${pkgver}

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

  msg2 "Build shared for ${MINGW_CHOST}"
  [[ -d "${srcdir}/build-${MINGW_CHOST}-shared" ]] && rm -rf "${srcdir}/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 "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DBUILD_SHARED_LIBS=ON \
    -DENABLE_COMPATIBLE_LIBSNDFILE_NAME=ON \
    ../${_realname}-${pkgver}

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

package() {
  msg2 "Install static for ${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build ./ --target install

  msg2 "Install shared for ${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build ./ --target install
}
