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

_bootstrap=0
_bootstrap_version=3.13.0
_parent_name=flit
_name=flit_core
pkgname=python-flit-core
pkgver=3.12.0
pkgrel=1
pkgdesc="A PEP 517 build backend for packages using Flit"
arch=(any)
url="https://github.com/pypa/flit/tree/main/flit_core"
_url="https://github.com/pypa/flit"
license=(BSD-3-Clause)
groups=(python-build-backend)
depends=(python)
if (( _bootstrap == 0 )); then
  makedepends=(
    python-build
    python-installer
  )
else
  makedepends=(
    git
  )
fi
checkdepends=(
  python-pytest
  python-testpath
)
if (( _bootstrap == 0 )); then
  source=($_parent_name-$pkgver.tar.gz::$_url/archive/refs/tags/$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=('ff6df66dfdae6fdf7b277cc3fbb7c9a569103cbe82a3e3ce6f315ec6adec46be8692eba4549a66b3af537d128e6b57ed8f49316b705636466c25674198503643')
b2sums=('b7e97a8665aeac90d81b033450cec7e3d1f6661916e3f5f7443025723047291b3482414d3189cafa56df859a3a4ba8f868526ec39db7888b55f24a12529ececf')

prepare() {
  if (( _bootstrap == 0 )); then
    cd $_parent_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 $_parent_name-$pkgver/$_name
    python -m build --wheel --skip-dependency-check --no-isolation
  else
    cd python-bootstrap
    python -m bootstrap.build
  fi
}

check() {
  cd $_parent_name-$pkgver/$_name
  pytest -vv
}

package() {
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")

  if (( _bootstrap == 0 )); then
    cd $_parent_name-$pkgver/$_name
    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/flit_core-*-py3-none-any.whl -d "$pkgdir"
    install -vDm 644 external/flit/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  fi

  # remove tests
  rm -frv "$pkgdir/$site_packages/${_name/-/_}/tests/"
  # remove vendored tomli
  rm -frv "$pkgdir/$site_packages/${_name/-/_}/vendor/"
}
