#!/bin/bash -e
#
# Attempt to bump the rkt release to the specified version by replacing all
# occurrences of the current/previous version.
#
# Generates two commits: the release itself and the bump to the next +git
# version
#
# YMMV, no disclaimer or warranty, etc.

# make sure we are running in a toplevel directory
if ! [[ "$0" =~ "scripts/bump-release" ]]; then
	echo "This script must be run in a toplevel rkt directory"
	exit 255
fi

if ! [[ "$1" =~ ^v[[:digit:]]+.[[:digit:]]+.[[:digit:]]$ ]]; then
	echo "Usage: scripts/bump-release <VERSION>"
	echo "   where VERSION must be vX.Y.Z"
	exit 255
fi

function replace_stuff() {
	local FROM
	local TO
	local REPLACE

	FROM=$1
	TO=$2
	# escape special characters
	REPLACE=$(sed -e 's/[]\/$*.^|[]/\\&/g'<<< $FROM)
	shift 2
	echo $* | xargs sed -i --follow-symlinks -e "s/$REPLACE/$TO/g"
}

function replace_all() {
	replace_stuff $1 $2 $(git ls-files | grep -Ev "(CHANGELOG.md|vendor|manifest\.d|glide.lock|glide.yaml)")
}

function replace_version() {
	replace_stuff $1 $2 configure.ac scripts/build-rir.sh tests/rkt-monitor/build-stresser.sh
}

NEXT=${1:1}             # 0.2.3
NEXTGIT="${NEXT}+git"   # 0.2.3+git

PREVGIT=$(grep -Po 'AC_INIT\(\[rkt\], \[\K[^\]]*(?=])' configure.ac) # 0.1.2+git
PREV=${PREVGIT::-4}     # 0.1.2

replace_version $PREVGIT $NEXT
replace_all $PREV $NEXT
git commit -am "version: bump to v${NEXT}"

replace_version $NEXT $NEXTGIT
git commit -am "version: bump to v${NEXTGIT}"
