#!/bin/bash -e
cd "$( dirname "${BASH_SOURCE[0]}" )/.."
if [[ ! -d venv-release ]]; then
	virtualenv venv-release
	echo '*' >venv-release/.gitignore
	venv-release/bin/pip install -U pip setuptools sphinx wheel
fi
. $PWD/venv-release/bin/activate
pip install -e $PWD

version=
version_next=

main() {
	branch="${1-$(git symbolic-ref --short HEAD)}"
	version="$(python -c 'import eventlet; print(eventlet.__version__)')"
	printf "\nbranch: %s eventlet.__version__: '%s'\n" $branch $version >&2
	if [[ "$branch" != "master" ]]; then
		echo "Must be on master" >&2
		exit 1
	fi
	if [[ -n "$(git status --short -uall)" ]]; then
		echo "Tree must be clean. git status:" >&2
		echo "" >&2
		git status --short -uall
		echo "" >&2
		exit 1
	fi
	last_commit_message=$(git show --format="%s" --no-patch HEAD)
	expect_commit_message="v$version release"
	if [[ "$last_commit_message" != "$expect_commit_message" ]]; then
		printf "Last commit message: '%s' expected: '%s'\n" "$last_commit_message" "$expect_commit_message" >&2
		if confirm "Create release commit? [yN] "; then
			create_commit
		elif ! confirm "Continue without proper release commit? [yN] "; then
			exit 1
		fi
	fi
	confirm "Continue? [yN] " || exit 1

	echo "Creating tag v$version" >&2
	if ! git tag "v$version"; then
		echo "git tag failed " >&2
		confirm "Continue still? [yN] " || exit 1
	fi

	if confirm "Build documentation (website)? [Yn] " >&2; then
		bin/build-website.bash || exit 1
	fi

	if confirm "Upload to PyPi? [Yn] "; then
		rm -rf build dist
		python setup.py sdist bdist_wheel register upload || exit 1
	fi

	git push --verbose origin master gh-pages || exit 1
	git push --tags
}

create_commit() {
	echo "" >&2
	echo "Plan:" >&2
	echo "1. bump version" >&2
	echo "2. update NEWS, AUTHORS" >&2
	echo "3. commit" >&2
	echo "4. run bin/release again" >&2
	echo "" >&2

	bump_version
	edit_news

	git diff
	confirm "Ready to commit? [Yn] " || exit 1
	git commit -a -m "v$version_next release"

	echo "Re-exec $0 to continue" >&2
	exec $0
}

bump_version() {
	local current=$version
	echo "Current version: '$current'" >&2
	echo -n "Enter next version (empty to abort): " >&2
	read version_next
	if [[ -z "$version_next" ]]; then
		exit 1
	fi
	echo "Next version:    '$version_next'" >&2

	local current_tuple="${current//./, }"
	local next_tuple="${version_next//./, }"
	local version_path="eventlet/__init__.py"
	echo "Updating file '$version_path'" >&2
	if ! sed -i '' -e "s/($current_tuple)/($next_tuple)/" "$version_path"; then
		echo "sed error $?" >&2
		exit 1
	fi
	if git diff --exit-code "$version_path"; then
		echo "File '$version_path' is not modified" >&2
		exit 1
	fi
	echo "" >&2

	local doc_path="doc/real_index.html"
	echo "Updating file '$doc_path'" >&2
	if ! sed -i '' -e "s/$current/$version_next/g" "$doc_path"; then
		echo "sed error $?" >&2
		exit 1
	fi
	if git diff --exit-code "$doc_path"; then
		echo "File '$doc_path' is not modified" >&2
		exit 1
	fi
	echo "" >&2

	confirm "Confirm changes? [yN] " || exit 1
}

edit_news() {
	echo "Changes since last release:" >&2
	git log --format='%h   %an   %s' "v$version"^.. -- || exit 1
	echo "" >&2

	local editor=$(which edit 2>/dev/null)
	[[ -z "$editor" ]] && editor="$EDITOR"
	if [[ -n "$editor" ]]; then
		if confirm "Open default editor for NEWS and AUTHORS? [Yn] "; then
			$editor NEWS
			$editor AUTHORS
		else
			confirm "Change files NEWS and AUTHORS manually and press any key"
		fi
	else
		echo "Unable to determine default text editor." >&2
		confirm "Change files NEWS and AUTHORS manually and press any key"
	fi
	echo "" >&2

	if git diff --exit-code NEWS AUTHORS; then
		echo "Files NEWS and AUTHORS are not modified" >&2
		exit 1
	fi
	echo "" >&2

	confirm "Confirm changes? [yN] " || exit 1
}

confirm() {
	local reply
	local prompt="$1"
	read -n1 -p "$prompt" reply >&2
	echo "" >&2
	rc=0
	local default_y=" \[Yn\] $"
	if [[ -z "$reply" ]] && [[ "$prompt" =~ $default_y ]]; then
		reply="y"
	fi
	[[ "$reply" != "y" ]] && rc=1
	return $rc
}

main "$@"
