# Maintainer: David Runge <dvzrv@archlinux.org>

_bootstrap=0
_bootstrap_version=3.13.0
pkgname=python-pyproject-hooks
_name=${pkgname#python-}
pkgver=1.2.0
pkgrel=3
pkgdesc="A low-level library for calling build-backends in pyproject.toml-based project"
arch=(any)
url="https://github.com/pypa/pyproject-hooks"
license=(MIT)
depends=(python)
if (( _bootstrap == 0 )); then
  makedepends=(
    python-build
    python-installer
    python-flit-core
    python-wheel
  )
else
  makedepends=(
    git
  )
fi
checkdepends=(
  python-pytest
  python-setuptools
  python-testpath
)
if (( _bootstrap == 0 )); then
  source=($pkgname-$pkgver.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz)
else
  source=(
    python-bootstrap::git+https://gitlab.archlinux.org/archlinux/python-bootstrap.git#tag=$_bootstrap_version
    python-build::git+https://github.com/pypa/build.git
    python-flit::git+https://github.com/pypa/flit.git
    python-installer::git+https://github.com/pypa/installer.git
    python-wheel::git+https://github.com/pypa/wheel.git
    python-packaging::git+https://github.com/pypa/packaging
    python-pyproject-hooks::git+https://github.com/pypa/pyproject-hooks
    python-setuptools::git+https://github.com/pypa/setuptools.git
  )
fi
sha512sums=('c198624ca278001922e07039333aa623a87bd9ef9f38ec98346d6d49f19dec422f788f6737623b461d76586bc8fa752518906bc7501e822429803881700ce701')
b2sums=('cb4503a1e79bd338a14ec1583b3755c7135258ce318cb804527ea83dc032d14462654e9bfa3c1f8bb5f099d72dc2dffc2bd9bb41d1591d8b3d77a2c49e217176')

prepare() {
  if (( _bootstrap == 0 )); then
    cd $_name-$pkgver
  else
    cd python-bootstrap
    git submodule init

    git config submodule."external/build".url ../python-build
    git config submodule."external/flit".url ../python-flit
    git config submodule."external/installer".url ../python-installer
    git config submodule."external/wheel".url ../python-wheel
    git config submodule."external/packaging".url ../python-packaging
    git config submodule."external/pyproject-hooks".url ../python-pyproject-hooks
    git config submodule."external/setuptools".url ../python-setuptools

    git -c protocol.file.allow=always submodule update
    git submodule update --init --recursive
  fi
}

build() {
  if (( _bootstrap == 0 )); then
    cd $_name-$pkgver
    python -m build --wheel --no-isolation
  else
    cd python-bootstrap
    python -m bootstrap.build
  fi
}

check() {
  cd $_name-$pkgver
  export PYTHONPATH="$PWD/src:$PYTHONPATH"
  pytest -vv
}

package() {
  if (( _bootstrap == 0 )); then
    cd $_name-$pkgver
    python -m installer --destdir="$pkgdir" dist/*.whl
    install -vDm 644 LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  else
    cd python-bootstrap
    python -m bootstrap.install dist/${_name/-/_}-*-py3-none-any.whl -d "$pkgdir"
    install -vDm 644 external/$_name/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  fi
}
