# Contributor: Valery Kartel <valery.kartel@gmail.com>
maintainer="fossdd <fossdd@pwned.life>"
pkgname=aws-cli
pkgver=2.27.25
pkgrel=0
pkgdesc="Universal Command Line Interface for Amazon Web Services (v2)"
url="https://github.com/aws/aws-cli"
# s390x: py3-awscrt doesn't support big-endian
arch="all !s390x"
license="Apache-2.0"
depends="
	py3-awscrt
	py3-certifi
	py3-colorama
	py3-cryptography
	py3-dateutil
	py3-distro
	py3-docutils
	py3-jmespath
	py3-prompt_toolkit
	py3-ruamel.yaml
	py3-urllib3
	py3-zipp
	python3
	"
makedepends="
	python3-dev
	py3-gpep517
	py3-flit-core
	"
checkdepends="
	procps
	py3-jsonschema
	py3-mock
	py3-pytest
	py3-pytest-mock
	py3-pytest-xdist
	"
subpackages="
	$pkgname-doc
	$pkgname-pyc
	$pkgname-zsh-completion:zshcomp
	$pkgname-bash-completion:bashcomp
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/aws/aws-cli/archive/$pkgver.tar.gz
	allow-egg-info.patch
	botocore-2551.patch
	botocore-2990-rebased.patch
	fix-env.patch
	tmpfile-index.patch
	"

provides="aws-cli-v2=$pkgver-r$pkgrel"
replaces="aws-cli-v2"

case "$CARCH" in
x86*)
	;;
*)
	# save ourselves some time and run tests only on the above
	options="$options !check"
	;;
esac

build() {
	gpep517 build-wheel \
		--wheel-dir .dist \
		--output-fd 3 3>&1 >&2
}

check() {
	export AWS_SECRET_ACCESS_KEY=fake_key
	export AWS_ACCESS_KEY_ID=fake_id

	# each core takes like 2gb, so be a little conservative
	local cores
	cores="$(nproc)"
	cores="$((cores / 3))"

	python3 -m venv --clear --without-pip --system-site-packages .testenv
	.testenv/bin/python3 -m installer .dist/*.whl
	timeout 1800 \
	.testenv/bin/python3 -m pytest tests \
		-p no:warnings \
		-n "$cores" \
		--ignore=tests/backends \
		--ignore=tests/integration \
		--ignore=tests/functional/eks \
		--ignore=tests/functional/botocore/test_credentials.py \
		--deselect=tests/functional/autocomplete/test_main.py::test_smoke_test_completer \
		--deselect=tests/functional/botocore/test_credentials.py::SSOSessionTest::test_token_chosen_from_provider \
		--deselect=tests/functional/eks/test_kubeconfig.py::TestKubeconfigLoader::test_load_empty \
		--deselect=tests/functional/eks/test_kubeconfig.py::TestKubeconfigLoader::test_load_noexist \
		--deselect=tests/functional/autoprompt/test_prompttoolkit.py
}

package() {
	python3 -m installer -d "$pkgdir" \
		.dist/*.whl
	rm "$pkgdir"/usr/bin/aws.cmd
}

bashcomp() {
	pkgdesc="$pkgdesc (bash completions)"
	install_if="$pkgname=$pkgver-r$pkgrel bash-completion"
	mkdir -p "$subpkgdir"/usr/share/bash-completion/completions
	mv "$pkgdir"/usr/bin/aws_bash_completer \
		"$subpkgdir"/usr/share/bash-completion/completions
}

zshcomp() {
	pkgdesc="$pkgdesc (zsh completions)"
	install_if="$pkgname=$pkgver-r$pkgrel zsh"
	mkdir -p "$subpkgdir"/usr/share/zsh/site-functions
	mv "$pkgdir"/usr/bin/aws_zsh_completer.sh \
		"$subpkgdir"/usr/share/zsh/site-functions
}

doc() {
	default_doc

	local pyver="$(python3 -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')"

	amove usr/lib/python3*/site-packages/awscli/topics
	amove usr/lib/python3*/site-packages/awscli/examples

	mkdir -p "$pkgdir"/usr/lib/python$pyver/site-packages/awscli/examples
	# move back top-level so just "aws help" works
	mv "$subpkgdir"/usr/lib/python$pyver/site-packages/awscli/examples/global_options.rst \
		"$pkgdir"/usr/lib/python$pyver/site-packages/awscli/examples/
}

sha512sums="
00689304db189e5f5876d98ab7ff0143d1859f08ba51be49c50678829f914903c656f9401cacb34060ff259790f028441011156fad25d309fe0de79dc5337a4a  aws-cli-2.27.25.tar.gz
5417517378d475fedae6082563d75084a996ec5930c1b8f39e533b836026604246315f86b292b1f1001fcecd8b9fae4b074bea14e37a0c9305247084624d7a92  allow-egg-info.patch
e0647b8690c139617da696318124b74a19b938c56d836f6531a86e031ec9fd9e43798807a3519d78c9abba59aa8b6679623dc75b579ce57ebb4dff55569fa18d  botocore-2551.patch
5ff58e59144d76e2f1601ed7df92341f7d659bd1b5eab9659d39cad85a27204e889e8802bb4495203e50292c5063e44989b21e296887cf467afdb0149526a410  botocore-2990-rebased.patch
850b4ca6e09f96cba8968d2d7a3b60bd5206d7c4c257ba927b576336a992796b51263506cdd100314604ec7ad493a26326bf059ccd7f96772ad9326aa68ee41e  fix-env.patch
bf5c714ca5e030ca829f64aa4be1a1ec4967108af7ec95a8a62c53902ad492161408fc551559bc29a08eaecd28e61109f9a9968609e57efa5463da5537c59dce  tmpfile-index.patch
"
