#!/bin/sh
# Increment the verison number and release a new version of MILC.
#
# Required packages are in requirements-release.txt

set -e
set -x

echo "This script has been deprecated. Use the github action instead."
exit

case "$1" in
	major)
		BUMPVERSION_TYPE=major
	;;
	minor)
		BUMPVERSION_TYPE=minor
	;;
	*)
		BUMPVERSION_TYPE=${BUMPVERSION_TYPE:-patch}
	;;
esac


PYPI_USERNAME=${PYPI_USERNAME:=skully}
TWINE_USERNAME=$PYPI_USERNAME

export TWINE_USERNAME

# Install deps
python3 -m pip install --upgrade -r requirements-release.txt

# Make sure the tree is clean
if ! git status | grep 'working tree clean'; then
	echo '*** Working tree is not clean!'
	exit 1
fi
rm -f dist/*

# Make sure we're current with master
git pull --ff-only

# Generate the documentation
./generate_docs

if ! git status | grep 'working tree clean'; then
	docs_good=1
	for doc in docs/api*.md; do
		if ! grep -q "$(basename $doc)" docs/_summary.md; then
			echo "No _summary entry for ${doc}!"
			docs_good=0
		fi
	done

	if [ $docs_good -eq 0 ]; then
		exit 2
	fi

	git add .
	git commit -m"Generated API documentation."
fi

# Increment the version number
bumpversion $BUMPVERSION_TYPE

# Push our work up
git push origin master --tags

# Build our package
python3 setup.py sdist bdist_wheel

# Upload to pypi
twine upload dist/milc-*
