# Maintainer: Diego Sogari <diego.sogari@gmail.com>

_realname=libavro
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.10.0
pkgrel=2
pkgdesc="The Apache Avro data serialization system (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://github.com/apache/avro"
license=('Apache')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-asciidoc"
             "${MINGW_PACKAGE_PREFIX}-source-highlight")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-jansson"
         "${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/apache/avro/archive/release-${pkgver}.tar.gz"
        "001-libavro-1.10.0-fix-mingw-build.patch"
        "002-libavro-1.10.0-add-threadsafe-option.patch")
sha256sums=('50ceefe582193ef8a92a8891bac1520df778f5c260f2ba68f4c9ae41417e3777'
            '0914fa866147664df53daf8217c13fe34de477ddf5081d7aba7eca8575155606'
            '346e37dc9adb52739e672483ee895299da6e00121ae0061beb2df3063234d62b')

prepare() {
  cd "${srcdir}/avro-release-${pkgver}"
  patch -Np1 -i "${srcdir}/001-libavro-1.10.0-fix-mingw-build.patch"
  patch -Np1 -i "${srcdir}/002-libavro-1.10.0-add-threadsafe-option.patch"
}

build() {
  for lang in c c++
  do
    rm -rf "${srcdir}/build-${MINGW_CHOST}-${lang}"
    mkdir "${srcdir}/build-${MINGW_CHOST}-${lang}"
    cd "${srcdir}/build-${MINGW_CHOST}-${lang}"

    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE="Release" \
      -DTHREADSAFE=ON \
      "../avro-release-${pkgver}/lang/${lang}"

    make
  done
}

check() {
  for lang in c c++
  do
    cd "${srcdir}/build-${MINGW_CHOST}-${lang}"
    ctest
  done
}

package() {
  for lang in c c++
  do
    cd "${srcdir}/build-${MINGW_CHOST}-${lang}"
    make DESTDIR="${pkgdir}" install
  done
  rm "${pkgdir}${MINGW_PREFIX}"/include/avro/{msinttypes,msstdint}.h
}
