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

_realname=libavro
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.12.0
pkgrel=2
pkgdesc="The Apache Avro data serialization system (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/apache/avro"
msys2_references=(
  'aur: avro-c'
)
license=('spdx:Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-asciidoc"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-source-highlight")
depends=("${MINGW_PACKAGE_PREFIX}-fmt"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-boost-libs"
         "${MINGW_PACKAGE_PREFIX}-jansson"
         "${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib")
options=(!emptydirs)
source=("https://github.com/apache/avro/archive/release-${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-libavro-1.10.0-fix-mingw-build.patch"
        "002-libavro-1.10.0-add-threadsafe-option.patch"
        "003-libavro-fix-pkgconfig-file.patch"
        "004-Fix-bad-filesystem_path-conversion-type-on-windows.patch::https://github.com/apache/avro/commit/ed85a819.patch")
sha256sums=('51832f9c2e81fa95addb74be627dac27b4883ec2c8627ec8471d99cfea787555'
            '8ffe71a02c0aa62d9b4e29eaaccb7374ae7a98fb5b00ec189d4ce350fa25e739'
            '346e37dc9adb52739e672483ee895299da6e00121ae0061beb2df3063234d62b'
            'eafde6b9ec2fbd06f926b76d017d0d0971fd136ccee1d5cab2842f53c05ae611'
            '94ac77939e2b21864cbf1881f05f802da82a0c430590bc9bb34e8cb67f74986b')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/avro-release-${pkgver}"

  _apply_patch_with_msg \
    001-libavro-1.10.0-fix-mingw-build.patch \
    002-libavro-1.10.0-add-threadsafe-option.patch \
    003-libavro-fix-pkgconfig-file.patch \
    004-Fix-bad-filesystem_path-conversion-type-on-windows.patch
}

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

  CFLAGS+=" -Wno-incompatible-pointer-types"

  for lang in c c++
  do
    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${_extra_config[@]}" \
      -DTHREADSAFE=ON \
      -S avro-release-${pkgver}/lang/${lang} \
      -B build-${MSYSTEM}-${lang}

    ${MINGW_PREFIX}/bin/cmake --build build-${MSYSTEM}-${lang}
  done
}

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

package() {
  for lang in c c++
  do
    cd "${srcdir}/build-${MSYSTEM}-${lang}"
    DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  done

  rm "${pkgdir}${MINGW_PREFIX}"/include/avro/{msinttypes,msstdint}.h

  rm -r "${pkgdir}${MINGW_PREFIX}"/include/fmt/
  rm -r "${pkgdir}${MINGW_PREFIX}"/lib/cmake/fmt/
  rm "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/fmt.pc
  rm "${pkgdir}${MINGW_PREFIX}"/lib/libfmt.a
}
