# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Maintainer: Morten Linderud <foxboron@archlinux.org>
# Contributor: Lance Chen <cyen0312@gmail.com>

_bootstrap=0
_bootstrap_version=3.13.0
pkgname=python-wheel
_name=${pkgname#python-}
pkgver=0.45.1
pkgrel=1
pkgdesc="A built-package format for Python"
arch=(any)
url="https://pypi.python.org/pypi/wheel"
license=('MIT')
depends=('python')
if (( _bootstrap == 0 )); then
  depends+=('python-packaging')
fi
optdepends=('python-keyring: for wheel.signatures'
            'python-xdg: for wheel.signatures'
            'python-setuptools: for legacy bdist_wheel subcommand')
if (( _bootstrap == 0 )); then
  makedepends=('python-build' 'python-flit-core' 'python-installer')
else
  makedepends=('git')
fi
checkdepends=('python-jsonschema' 'python-pytest' 'python-keyring' 'python-keyrings-alt'
              'python-xdg' 'python-setuptools')
if (( _bootstrap == 0 )); then
  source=(
    "$pkgname-$pkgver.tar.gz::https://github.com/pypa/wheel/archive/$pkgver.tar.gz"
    $pkgname-0.45.0-devendor.patch
  )
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=('d15299512d0b401214c866334dfa68b11810da02f9dd67925d66f546c7ca62eda37312fb37dec0e1a7d861b1f507131029ab4a42d63f5dd27aaca50c517e1d50'
            'b9e705f1ca4f71332cd20b404d3eaae13a0785958a021078d786177b161e789c370d20dd0411246e53890235d8dc527c72a4cc8023c0bcff8921135422b98173')

prepare() {
  if (( _bootstrap == 0 )); then
    cd wheel-$pkgver
    # remove vendored python-packaging
    patch -Np1 -d . -i ../$pkgname-0.45.0-devendor.patch
    rm -rv src/wheel/vendored
  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 wheel-$pkgver
    python -m build --wheel --no-isolation
  else
    cd python-bootstrap
    python -m bootstrap.build
  fi
}

check() {
  # Hack entry points by installing it

  cd wheel-$pkgver
  python -m installer --destdir="$PWD/tmp_install" dist/*.whl
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
  PYTHONPATH="$PWD/tmp_install/$site_packages" pytest
}

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