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

pkgbase=maturin
pkgname=("python-maturin" "maturin")
pkgver=1.11.5
pkgrel=1
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"
        "ring-0.17.14.tar.gz::https://crates.io/api/v1/crates/ring/0.17.14/download"
        "0003-allow-msys-too.patch"
        "0004-ring-support-cygwin.patch")
sha256sums=('7579cf47640fb9595a19fe83a742cbf63203f0343055c349c1cab39045a30c29'
            'a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7'
            '023b52d6eb2985608f1a7206d1950bf7e428bd4c00a7f2b69a8b3cf18c1b4c0c'
            '258dc64aab2bb464878b9cc203e59db87d7b769b6b58fb2d9c69bf53bb418683')

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

  patch -Np1 -i ../0003-allow-msys-too.patch
  patch -p1 -d ../ring-0.17.14 -i ../0004-ring-support-cygwin.patch
  cat >> Cargo.toml <<END

[patch.crates-io]
ring.path = "../ring-0.17.14"
END

  cargo update -p ring
  cargo fetch --locked --target "${RUST_CHOST}"
}

build() {
  cd "python-build"

  export MATURIN_SETUP_ARGS="--no-default-features --features full,rustls"
  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}/"
}
