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

_realname=librdkafka
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.6.1
pkgrel=1
pkgdesc="The Apache Kafka C/C++ client library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/edenhill/librdkafka"
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
depends=("${MINGW_PACKAGE_PREFIX}-cyrus-sasl"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/edenhill/librdkafka/archive/v${pkgver}.tar.gz")
sha256sums=('689028b5fdfdce026f396fc2f3bfe9e38947210d9bd22be28c3159cc8c41b57e')

build() {
  rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE="Release" \
    -DWITHOUT_WIN32_CONFIG=OFF \
    -DWITH_LIBDL=ON \
    -DWITH_PLUGINS=ON \
    -DWITH_SASL=ON \
    -DWITH_SSL=ON \
    -DWITH_ZLIB=ON \
    "../${_realname}-${pkgver}"

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

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  cp -f ./src/librdkafka.dll ./src-cpp/librdkafka++.dll ./tests
  ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target test || true
}

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

  # remove reference to MSYS2 install directory
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/RdKafka/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done
}
