# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: SZanko, szanko at protonmail dot com

pkgname=python-shtab
pkgver=1.7.2
pkgrel=1
pkgdesc='Automagic shell tab completion for Python CLI applications'
arch=('any')
url='https://github.com/iterative/shtab'
license=('Apache-2.0')
depends=('python')
makedepends=(
  'git'
  'python-build'
  'python-installer'
  'python-wheel'
  'python-setuptools-scm'
)
checkdepends=(
  'python-pytest'
  'python-pytest-timeout'
)
source=(
  "$pkgname::git+$url#tag=v$pkgver"
  'remove-cov.patch'
)
sha512sums=('7ef288650e8e605da4866afaff26ef8cef963fd116e7d56d58ccb1bdb14fce008247c182a366b97bbce7ec725525f9b66394c090836fd68688690f9de3654b7d'
            'c1390c0c1140ed701a56394b44bc5a0497901811cfbc7b6185f964b773db7851a13c745fe8122705ee74bbcdb3ce7ff5e19a5b8cfcf20df6e32b28f0887cb18a')
b2sums=('0568cd55cbc77d64d8aee77dccb1649f450fef9ce2f803ac7b96ff5e9ed206562bf785951a63080b972b174379543aec7d100fdec8c35f26a36794d30ab6b2bd'
        '3d265e70111506165beddf031af0efb01ca62bd2d0b41af04e74f175db8b9fec6e1b31ad5dbba5d001d53d48313e581b3bbfa4d2ba7d1094cb983521a5b3c1d2')

prepare() {
  cd "$pkgname"

  patch -p1 -i "$srcdir/remove-cov.patch"
}

build() {
  cd "$pkgname"

  SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver python -m build --wheel --no-isolation

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

  # install to temporary directory
  python -m installer --destdir="$PWD/tmp_install" dist/*.whl

  export PYTHONPATH="$PWD/tmp_install$site_packages"

  # generate shell completions
  for shell in bash zsh; do
    python -m shtab --print-own-completion "$shell" > "$shell.completion"
  done
}

check() {
  cd "$pkgname"

  pytest -v
}

package() {
  cd "$pkgname"

  python -m installer --destdir="$pkgdir" dist/*.whl

  # shell completions
  install -vDm644 bash.completion "$pkgdir/usr/share/bash-completion/completions/shtab"
  install -vDm644 zsh.completion "$pkgdir/usr/share/zsh/site-functions/_shtab"

  # license
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENCE
}
