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

pkgbase=maturin
pkgname=("python-maturin" "maturin")
pkgver=1.9.6
pkgrel=6
pkgdesc='Build and publish crates with pyo3, rust-cpython and cffi bindings'
arch=('x86_64')
url='https://www.maturin.rs/'
msys2_repository_url='https://github.com/pyo3/maturin'
msys2_references=(
  'purl: pkg:pypi/maturin'
)
license=('spdx:MIT OR Apache-2.0')
depends=("python" "libopenssl")
makedepends=(
  "python-build"
  "python-installer"
  "python-setuptools-rust"
  "openssl-devel"
  "pkgconf")
options=('!strip')
source=("https://pypi.org/packages/source/${pkgbase::1}/${pkgbase}/${pkgbase}-${pkgver}.tar.gz"
        "https://github.com/PyO3/maturin/commit/608963e067b905940658b5cb06d7fc04595a0536.patch"
        "0003-allow-msys-too.patch")
sha256sums=('2c2ae37144811d365509889ed7220b0598487f1278c2441829c3abf56cc6324a'
            '5e636be1400a660a83a4ae2b8e5948610a8a3c147be809720d00f2def87d9bfc'
            '023b52d6eb2985608f1a7206d1950bf7e428bd4c00a7f2b69a8b3cf18c1b4c0c')

prepare() {
  cp -r "${pkgbase}-${pkgver}" "python-build" && cd "python-build"

  # https://github.com/PyO3/maturin/pull/2819
  patch -Np1 -i ../608963e067b905940658b5cb06d7fc04595a0536.patch
  patch -Np1 -i ../0003-allow-msys-too.patch

  # All these can removed once the next release is out
  cargo update -p xattr@1.3.1 --precise 1.5.0
  cargo update -p tempfile@3.11.0 --precise 3.18.0
  cargo update -p which@7.0.0 --precise 7.0.3
  cargo update -p clap@4.5.7 --precise 4.5.19
  cargo update -p psm@0.1.21 --precise 0.1.26

  cargo fetch --locked --target 'x86_64-pc-cygwin'
}

build() {
  cd "python-build"

  export MATURIN_SETUP_ARGS="--no-default-features --features cli-completion,scaffolding"
  export OPENSSL_NO_VENDOR=1
  python -m build --wheel --skip-dependency-check --no-isolation
}

package_python-maturin() {
  pkgdesc+=' (Python bindings)'
  depends=("python" "maturin")

  cd "python-build"

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

  install -Dm644 license-{apache,mit} -t "${pkgdir}/usr/share/licenses/python-${pkgbase}/"

  # split maturin executable
  mkdir -p "dest/usr/bin"
  mv "${pkgdir}/usr"/bin/* "dest/usr/bin/"
}

package_maturin() {
  cd "python-build"

  mv dest/* "${pkgdir}"

  local _complete="${pkgdir}/usr/bin/maturin completions"
  $_complete bash | install -Dm644 /dev/stdin "${pkgdir}/usr/share/bash-completion/completions/maturin"
  $_complete zsh | install -Dm644 /dev/stdin "${pkgdir}/usr/share/zsh/site-functions/_maturin"
  $_complete fish | install -Dm644 /dev/stdin "${pkgdir}/usr/share/fish/vendor_completions.d/maturin.fish"
  install -Dm644 license-{apache,mit} -t "${pkgdir}/usr/share/licenses/${pkgbase}/"
}
