# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=uv
pkgbase=${_realname}
pkgname=("python-${_realname}-build")
pkgver=0.9.20
pkgrel=1
pkgdesc='An extremely fast Python package installer and resolver, written in Rust (python build backend)'
arch=('x86_64')
url="https://github.com/astral-sh/uv"
license=('spdx:Apache-2.0 OR MIT')
makedepends=('rust'
             'python-maturin'
             'python-installer'
             'liblzma-devel'
             'libzstd-devel'
             'pkgconf'
             'git')
options=('!strip')
source=("git+${url}.git#tag=${pkgver}"
        "zstd-sys.tar.gz::https://crates.io/api/v1/crates/zstd-sys/2.0.15+zstd.1.5.7/download"
        "bzip2-sys.tar.gz::https://crates.io/api/v1/crates/bzip2-sys/0.1.13+1.0.8/download"
        "bzip2-use-pkgconfig.patch"
        "link-zstd-dynamically.patch")
sha256sums=('e9dbf2ea5f9cf8271323f325404e65d4615482623301412422a91cf262c8f24e'
            'eb81183ddd97d0c74cedf1d50d85c8d08c1b8b68ee863bdee9e706eedba1a237'
            '225bff33b2141874fe80d71e07d6eec4f85c5c216453dd96388240f96e1acc14'
            'f73309fb98b89b2085bccee6ffb66820082628d096bc989922b25d33a1f5f378'
            '48f4900ceb02d3aaf9a1020f33d56629156e96759f456c0e7ca18bfcf910767b')

prepare() {
  cd "${_realname}"

  patch -d "../zstd-sys-2.0.15+zstd.1.5.7" -i "../link-zstd-dynamically.patch"
  patch -d "../bzip2-sys-0.1.13+1.0.8" -i "../bzip2-use-pkgconfig.patch"
  cat >> Cargo.toml <<END

[patch.crates-io]
zstd-sys.path = "../zstd-sys-2.0.15+zstd.1.5.7"
bzip2-sys.path = "../bzip2-sys-0.1.13+1.0.8"
END

  rm -f rust-toolchain.toml
  cargo update -p mio --precise 1.1.0
  cargo update -p zstd-sys -p bzip2-sys
  cargo fetch --locked
}

build() {
  cd "${_realname}"

  export PKG_CONFIG_ALL_DYNAMIC=1
  export ZSTD_SYS_USE_PKG_CONFIG=1
  maturin build --frozen --strip --release --all-features -m crates/uv-build/Cargo.toml
}

package() {
  depends=('python' 'liblzma' 'libzstd')

  cd "${_realname}"
  
  python -m installer --prefix=/usr \
    --destdir="${pkgdir}" target/wheels/uv_build-$pkgver-*.whl

  install -Dm644 LICENSE-MIT -t "${pkgdir}/usr/share/licenses/${pkgname}/"
  install -Dm644 LICENSE-APACHE -t "${pkgdir}/usr/share/licenses/${pkgname}/"
}
