# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=polars
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}-runtime-64")
pkgver=1.35.2
pkgrel=1
pkgdesc="Extremely fast Query Engine for DataFrames, written in Rust (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.pola.rs/'
msys2_repository_url='https://github.com/pola-rs/polars'
license=('spdx:MIT')
msys2_references=(
  'archlinux: python-polars'
  'anitya: 179760'
  'purl: pkg:pypi/polars'
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-rust"
  "${MINGW_PACKAGE_PREFIX}-maturin"
  "${MINGW_PACKAGE_PREFIX}-python-installer"
  "${MINGW_PACKAGE_PREFIX}-python-build"
  "${MINGW_PACKAGE_PREFIX}-python-wheel"
  "${MINGW_PACKAGE_PREFIX}-python-setuptools"
  "${MINGW_PACKAGE_PREFIX}-sed"
  'git'
)
options=('!strip' '!lto')
source=("${msys2_repository_url}/archive/py-${pkgver}/${_realname}-py-${pkgver}.tar.gz")
noextract=("${_realname}-py-${pkgver}.tar.gz")
sha256sums=('085d9540df7a27bfc6803bb27915ac62ba641c81516647e90d88c58d1bb6d9be')

prepare() {
  tar -xf "${_realname}-py-${pkgver}.tar.gz" || true

  cd "${_realname}-py-${pkgver}/py-polars"

  cp ../LICENSE .
  cp ../rust-toolchain.toml .

  sed -i 's/polars-runtime-32/polars-runtime-64/g' pyproject.toml

  cargo fetch \
    --locked \
    --config='net.git-fetch-with-cli=true' \
    --target "${RUST_CHOST}"
}

build() {
  cd "${_realname}-py-${pkgver}/py-polars"

  export _PYTHON_HOST_PLATFORM=$(python -c "import sysconfig, sys; sys.stdout.write(sysconfig.get_platform())")
  export WINAPI_NO_BUNDLED_LIBRARIES=1
  python -m build --wheel --no-isolation
  maturin build -o dist --release --locked --strip \
   --no-default-features --features full \
   --compatibility linux \
   --manifest-path runtime/Cargo.toml 
}

package_python-polars() {
  cd "${_realname}-py-${pkgver}/py-polars"

  depends=(
    "${MINGW_PACKAGE_PREFIX}-python"
    "${MINGW_PACKAGE_PREFIX}-python-numpy"
    "${MINGW_PACKAGE_PREFIX}-python-polars-runtime-64"
  )
  optdepends=(
    "${MINGW_PACKAGE_PREFIX}-python-pandas: for interoperability with pandas frames"
    "${MINGW_PACKAGE_PREFIX}-python-pyarrow: for interoperability with arrow types"
    "${MINGW_PACKAGE_PREFIX}-python-pytz: to enable conversion to python datetimes with timezones"
    #"${MINGW_PACKAGE_PREFIX}-python-fsspec: to transparently open files locally or remotely"
  )

  python -m installer -d "$pkgdir" ./dist/polars-*.whl

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" ./dist/polars-*.whl

  install -Dm0644 LICENSE -t "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/"

  # Remove duplicate files (for more info see https://github.com/msys2/MINGW-packages/pull/26356)
  cd "${pkgdir}"
  for _df in $(ls | grep -v ${MINGW_PREFIX/\//}); do rm -rf $_df; done
}

package_python-polars-runtime-64() {
  cd "${_realname}-py-${pkgver}/py-polars"

  pkgdesc+=" (native runtime libraries)"
  depends=("${MINGW_PACKAGE_PREFIX}-python")

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" ./dist/polars_runtime*.whl

  install -Dm0644 LICENSE -t "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}-runtime-64/"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
