#####!/bin/bash
#
set -xue -o pipefail

generate_shell_completions() {
  : # https://click.palletsprojects.com/en/stable/shell-completion/

  mkdir -p ./dist/completions/"${1}"
  env _AMDFAN_COMPLETE=${1}_source amdfan > dist/completions/"${1}"/"${2:-_amdfan}"
}

echo "cleaning up previous run"
rm -rf dist/*.whl dist/*.gz configure config.status autom4te.cache config.log *.gz *.zst *.log

echo "autoconf"
autoconf

echo "./configure"
./configure --prefix=/usr --libdir=/usr/lib --bindir=/usr/bin

echo "Copy the PKGBUILD to the root dir for running makepkg"
cp dist/pacman/PKGBUILD .

echo "run linting"
poetry run flake8 --verbose

echo "run mypy"
poetry run mypy amdfan/

echo "run the tests"
poetry run pytest

echo "running poetry build"
poetry build

generate_shell_completions zsh
generate_shell_completions bash
generate_shell_completions fish

echo "getting the md5sum of the tarball"
md5sum dist/*.gz

echo "Copying the tarball to the root dir"
cp dist/*.gz .
