# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=blosc
_srcname=c-blosc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.21.0
pkgrel=1
pkgdesc="A blocking, shuffling and loss-less compression library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='http://blosc.org/'
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-zstd"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-lz4")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
options=('strip' 'staticlibs')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/Blosc/c-blosc/archive/v${pkgver}.tar.gz"
        01-build-fix.patch)
sha256sums=('b0ef4fda82a1d9cbd11e0f4b9685abf14372db51703c595ecd4d76001a8b342d'
            '2f4b73059021961876074c73279b4f42a07a135ea83e1b3b0ce8169491459bb2')

prepare() {
  cd "${srcdir}/${_srcname}-${pkgver}"
  patch -p1 -i ${srcdir}/01-build-fix.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 \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE="Release" \
      -DBUILD_SHARED_LIBS="ON" \
      -DPREFER_EXTERNAL_LZ4="ON" \
      -DPREFER_EXTERNAL_ZLIB="ON" \
      -DPREFER_EXTERNAL_SNAPPY="ON" \
      -DPREFER_EXTERNAL_ZSTD="ON" \
      -DBUILD_TESTS="OFF" \
      ../${_srcname}-${pkgver}

  make
}

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

  # License
  install -Dm644 ${srcdir}/${_srcname}-${pkgver}/LICENSES/BLOSC.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
