# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Maintainer: fossdd <fossdd@pwned.life>
pkgname=aws-cli
pkgver=2.17.37
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
# arm*, ppc64le: py3-awscrt
# x86: fails check, so most likely not supported on 32-bit anymore
arch="all !x86 !armhf !armv7 !ppc64le !s390x"
license="Apache-2.0"
depends="
	py3-awscrt
	py3-certifi
	py3-cryptography
	py3-dateutil
	py3-distro
	py3-colorama
	py3-docutils
	py3-jmespath
	py3-urllib3
	py3-prompt_toolkit
	py3-ruamel.yaml
	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
	aws-cli-v2-8838.patch
	aws-cli-v2-ruamel-yaml-v4.patch
	aws-cli-v2-tz-fix.patch
	botocore-2551.patch
	botocore-2922.patch
	botocore-2924.patch
	botocore-2967.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="
6de602199d98e0473488bab677ec4e6802d7beaece0a07e8896e1c6336971c6b4facfa1149ac8e0f96039622d163f8e4f0278ed2a9cb96ac8a722a8a72b2ebf9  aws-cli-2.17.37.tar.gz
3780a1b8af4d0098315beacd9a6a7cb4ebac08d4c1194dc55b5db6f0a28c46e45da85f0b94da2e005c5327686f8c4a2b99ee45b3cf847b3bbf5d82c80b433f50  allow-egg-info.patch
74651082bfd8407bfe2d8febfb90fe036f35b54066a3b6eeb4d8079acb2ed19e96476dae83c9e33f0ed747e2c9875023f457ecd8ac20d36a54fb4e5b7acf8724  aws-cli-v2-8838.patch
b302264d7f28e69701b46fdcd38e2104e5f4ebb064605211c1d92983d0f6732626b4e80bff026547b2216ea69a05010f07ec3f14377539f40cfd889e6044bd94  aws-cli-v2-ruamel-yaml-v4.patch
66c813b52d1fa402d89cd4381237175b3c8f52546e60f4a9703c27281f4a27d579751b90468a84208f94743b3c58e041cf9e9c9f28387ea06897575db8e46946  aws-cli-v2-tz-fix.patch
e0647b8690c139617da696318124b74a19b938c56d836f6531a86e031ec9fd9e43798807a3519d78c9abba59aa8b6679623dc75b579ce57ebb4dff55569fa18d  botocore-2551.patch
0b47b8329863dd71e4aeac18eb1175b47a68e45df0becae738ec3bf72f908cf96f99dffd7714c3835e014b1c100701a6652e1db3d2e415162fcc9c8a098cf201  botocore-2922.patch
99d8e7755149033fa86ae246de07f6c320991d111affd9a98618182fa1fdebf466e3be693671bb1dda4d5ac514f001446ff68cf156b98f33dc48c9a541d89c53  botocore-2924.patch
506733c13d1034684baf771c872aadbc4bad8037e05f588acde68071b73bd2d8fcf41a21562324210316cbdd1929c44ceca3273f174caf7af6470d8b27518fb1  botocore-2967.patch
79ff2138a7b9dbddedd059f0e1e3ef8f92708a93e2c65293de33298de65e5a3360e6e8af05c18b4763f1a7221198f4aa1ca2afd901fd748ad545ffea5fe8471a  botocore-2990-rebased.patch
850b4ca6e09f96cba8968d2d7a3b60bd5206d7c4c257ba927b576336a992796b51263506cdd100314604ec7ad493a26326bf059ccd7f96772ad9326aa68ee41e  fix-env.patch
ecfad03dafbdc8cdc73f41a432f0acb53ce3636f58a8b3d0cfcef06617f24ee8588e0648ee7290148bcfc8a3007d00b865899d473a6653d970a55be63760551f  tmpfile-index.patch
"
