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

_realname=arrow
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.0.1
pkgrel=1
pkgdesc="Apache Arrow is a cross-language development platform for in-memory data (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://arrow.apache.org/"
license=("Apache-2.0")
depends=("${MINGW_PACKAGE_PREFIX}-aws-sdk-cpp"
         "${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-flatbuffers"
         "${MINGW_PACKAGE_PREFIX}-gflags"
         "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
         "${MINGW_PACKAGE_PREFIX}-grpc"
         "${MINGW_PACKAGE_PREFIX}-libutf8proc"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${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}-boost"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-polly"
             "${MINGW_PACKAGE_PREFIX}-rapidjson")
options=("staticlibs" "strip" "!buildflags")
source=("apache-arrow-${pkgver}.tar.gz"::"https://www.apache.org/dyn/closer.lua?action=download&filename=arrow/arrow-${pkgver}/apache-arrow-${pkgver}.tar.gz")
sha256sums=("75ccbfa276b925c6b1c978a920ff2f30c4b0d3fdf8b51777915b6f69a211896e")

cmake_build_type=release
meson_build_type=debugoptimized

source_dir=apache-${_realname}-${pkgver}
cpp_build_dir=build-${CARCH}-cpp
c_glib_build_dir=build-${CARCH}-c-glib

build() {
  [[ -d ${cpp_build_dir} ]] && rm -rf ${cpp_build_dir}
  mkdir -p ${cpp_build_dir}
  pushd ${cpp_build_dir}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      ../${source_dir}/cpp \
      -DARROW_BUILD_UTILITIES=ON \
      -DARROW_FLIGHT=ON \
      -DARROW_GANDIVA=ON \
      -DARROW_PACKAGE_KIND=${MINGW_PACKAGE_PREFIX} \
      -DARROW_PACKAGE_PREFIX=${MINGW_PREFIX} \
      -DARROW_PARQUET=ON \
      -DARROW_PYTHON=ON \
      -DARROW_S3=ON \
      -DARROW_USE_GLOG=OFF \
      -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} \
      -DCMAKE_BUILD_TYPE=${cmake_build_type} \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_UNITY_BUILD=ON \
      -DPARQUET_BUILD_EXECUTABLES=ON \
      -DPARQUET_REQUIRE_ENCRYPTION=ON \
      -G Ninja
  ninja
  popd

  [[ -d ${c_glib_build_dir} ]] && rm -rf ${c_glib_build_dir}

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  meson \
    ${c_glib_build_dir} \
    ${source_dir}/c_glib \
    --buildtype=${meson_build_type} \
    --prefix=${MINGW_PREFIX} \
    -Darrow_cpp_build_dir=$(pwd)/${cpp_build_dir} \
    -Darrow_cpp_build_type=${cmake_build_type} \
    -Dgtk_doc=true
  sed -i'' -s 's/\r//g' ${c_glib_build_dir}/arrow-glib/version.h
  PATH=$(pwd)/${cpp_build_dir}/${cmake_build_type}:$PATH \
    ninja -C ${c_glib_build_dir}
}

check() {
  # TODO
  # make -C ${cpp_build_dir} test

  # PATH=$(pwd)/${c_glib_build_dir}/arrow-glib:$(pwd)/${cpp_build_dir}/${cmake_build_type}:$PATH \
  #   ninja -C ${c_glib_build_dir} test

  :
}

package() {
  DESTDIR="${pkgdir}" ninja -C ${cpp_build_dir} install

  PATH=$(pwd)/${c_glib_build_dir}/arrow-glib:$(pwd)/${cpp_build_dir}/${cmake_build_type}:$PATH \
  DESTDIR="${pkgdir}" ninja -C ${c_glib_build_dir} install

  # 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
}
