# Maintainer: Kouhei Sutou <kou@clear-code.com>

_realname=arrow
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=22.0.0
pkgrel=4
pkgdesc="Apache Arrow is a cross-language development platform for in-memory data (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://arrow.apache.org/"
msys2_repository_url="https://github.com/apache/arrow"
msys2_references=(
  'archlinux: arrow'
  "cpe: cpe:/a:apache:arrow"
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-abseil-cpp"
         "${MINGW_PACKAGE_PREFIX}-aws-sdk-cpp"
         "${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-flatbuffers"
         "${MINGW_PACKAGE_PREFIX}-gflags"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-grpc"
         "${MINGW_PACKAGE_PREFIX}-libutf8proc"
         "${MINGW_PACKAGE_PREFIX}-llvm-libs"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-re2"
         "${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-thrift"
         "${MINGW_PACKAGE_PREFIX}-uriparser"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-rapidjson"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-xsimd")
source=("https://www.apache.org/dyn/closer.lua?action=download&filename=arrow/arrow-${pkgver}/apache-arrow-${pkgver}.tar.gz"
        "https://www.apache.org/dyn/closer.lua?action=download&filename=arrow/arrow-${pkgver}/apache-arrow-${pkgver}.tar.gz.asc")
sha256sums=('131250cd24dec0cddde04e2ad8c9e2bc43edc5e84203a81cf71cf1a33a6e7e0f'
            'SKIP')

# See generate-valid-keys.sh
validpgpkeys=('A2AC7132B5DA7C273A7A147665F4A8CA9769ECD7')

build() {
  if check_option "debug" "n"; then
    export _cmake_build_type="Release"
  else
    export _cmake_build_type="Debug"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
      -G Ninja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=${_cmake_build_type} \
      -DARROW_ACERO=ON \
      -DARROW_BUILD_UTILITIES=ON \
      -DARROW_COMPUTE=ON \
      -DARROW_CSV=ON \
      -DARROW_DATASET=ON \
      -DARROW_FILESYSTEM=ON \
      -DARROW_FLIGHT=ON \
      -DARROW_FLIGHT_SQL=ON \
      -DARROW_GANDIVA=ON \
      -DARROW_HDFS=ON \
      -DARROW_JSON=ON \
      -DARROW_PACKAGE_KIND=${MINGW_PACKAGE_PREFIX} \
      -DARROW_PACKAGE_PREFIX=${MINGW_PREFIX} \
      -DARROW_PARQUET=ON \
      -DARROW_S3=ON \
      -DARROW_THRIFT_USE_SHARED=ON \
      -DARROW_USE_GLOG=OFF \
      -DARROW_DEPENDENCY_SOURCE=SYSTEM \
      -DARROW_WITH_BROTLI=ON \
      -DARROW_WITH_BZ2=ON \
      -DARROW_WITH_LZ4=ON \
      -DARROW_WITH_SNAPPY=ON \
      -DARROW_WITH_ZLIB=ON \
      -DARROW_WITH_ZSTD=ON \
      -DBOOST_ROOT=${MINGW_PREFIX} \
      -DPARQUET_BUILD_EXECUTABLES=ON \
      -DPARQUET_REQUIRE_ENCRYPTION=ON \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      -S "apache-${_realname}-${pkgver}/cpp" \
      -B "build-${MSYSTEM}-cpp"

  ${MINGW_PREFIX}/bin/cmake --build "build-${MSYSTEM}-cpp"

  declare -a _meson_extra_config
  if check_option "debug" "n"; then
    _meson_extra_config+=("--buildtype=release")
  else
    _meson_extra_config+=("--buildtype=debug")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson setup \
    --prefix=${MINGW_PREFIX} \
    "${_meson_extra_config[@]}" \
    -Darrow_cpp_build_dir="${srcdir}"/build-${MSYSTEM}-cpp \
    -Darrow_cpp_build_type="${_cmake_build_type}" \
    -Dgtk_doc=true \
    "build-${MSYSTEM}-c-glib" \
    "apache-${_realname}-${pkgver}/c_glib"

  PATH="${srcdir}"/build-${MSYSTEM}-cpp/${_cmake_build_type}:$PATH \
    ${MINGW_PREFIX}/bin/meson compile -C "build-${MSYSTEM}-c-glib"
}

package() {
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install "build-${MSYSTEM}-cpp"

  PATH="${srcdir}"/build-${MSYSTEM}-c-glib/arrow-glib:"${srcdir}"/build-${MSYSTEM}-cpp/${_cmake_build_type}:$PATH \
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson install -C "build-${MSYSTEM}-c-glib"

  # Remove full path reference
  local _PREFIX_WIN="$(cygpath -wm ${MINGW_PREFIX})"

  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${_PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done

  for cmfile in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/arrow/*.cmake; do
    sed -e "s|${_PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i "${cmfile}"
  done
}
