#!/bin/bash

#
# Create release of the project, which means a tag on a separate branch, with a fixed version. 
# NOTE : we expect this script to be in the following directory : ROOT_POM_DIR/build/
# 

# Release version for the tag to create
releaseVersion=

# change SNAPSHOT version after release.
newSnapshot=

# A prefix for the temporary release branch
releaseBranchPrefix="release-"

# Keep original branch to come back to it after release.
sourceBranch=

# Original code from 'http://stackoverflow.com/questions/242538/unix-shell-script-find-out-which-directory-the-script-file-resides'
# Absolute path to this script, e.g. /home/user/bin/foo.sh
SCRIPT=$(readlink -f "$BASH_SOURCE")
# Absolute path this script is in, thus /home/user/bin
SCRIPT_PATH=$(dirname "$SCRIPT")

###################### FUNCTIONS ############################

# Print help on standard output.
function printHelp {
	echo "
	Create a release tag for the current project. Release number will be something like X.x where X is major release version, and x is minor.
	"
	echo  "	WARNING : No check is done to ensure the project is well-formed (no compilation, test, syntax check, etc.)
	"
	echo  "	MANDATORY ARGUMENT : "
	echo  "	-r --release-version : Version to set on new release."
	echo  "	OPTIONAL ARGUMENT : "
	echo  "	-s --snapshot-version : If set, and once the tag is done, the version on original branch is changed according to this argument value."
	echo "	-- help, -h : Print this help."
}

function printError() {
	echo "
	"
	tput setaf 1; tput setab 7; tput bold;
	echo "$*"
	tput sgr0;	
	echo "
	"
}

# Print the given parameters to standard error output, then exit the script in failure mode.
function error() {
	printError "$*"
	git checkout $sourceBranch # come back on the initial branch before exit
	exit 1
}

# do the job
function execute {
	# We place ourself in the script directory. It MUST BE into the git project directory.
	cd $SCRIPT_PATH/..

	# Save original branch name to go back to it at the end of the release.
	sourceBranch="$(git symbolic-ref --short HEAD)"
	echo "Original branch is $sourceBranch"
	createTag
	updateSnapshot
}

# Create the tag itself 
function createTag {
	echo "Create temporary branch for release $releaseVersion" 
	tmpReleaseBranch="$releaseBranchPrefix$releaseVersion" 
	git branch $tmpReleaseBranch || error "Cannot create a temporary branch for release creation."
	git checkout $tmpReleaseBranch || error "Cannot access created branch $tmpReleaseBranch"

	echo "Perform version update for release $releaseVersion" 
	mvn versions:set -DnewVersion=$releaseVersion || error "Cannot update project version to $releaseVersion"

	echo "Commit release versions" 
	git commit -m "Project release $releaseVersion" -a || error "Cannot commit updated poms to the temporary release branch"

	echo "Create tag named $releaseVersion" 
	git tag $releaseVersion $tmpReleaseBranch || error "Cannot create the tag $releaseVersion"

	echo "Remove temporary release branch"
	git checkout $sourceBranch || error "Cannot come back to the source branch $sourceBranch"
	git branch -D $tmpReleaseBranch || printError "WARNING : Cannot delete temporary branch $tmpReleaseBranch. You will have to do it yourself ! "
}

# Update project version on initial branch
function updateSnapshot {
	if [ ! -z "$releaseVersion" ]
		then 
		echo "Update snapshot version $newSnapshot"
		mvn versions:set -DnewVersion=$newSnapshot || error "Cannot update project version to $newSnapshot"

		echo "Commit updated snapshot"
		git commit -m "Snapshot update $newSnapshot" -a || error "Cannot commit updated poms to the source branch"
	fi
}

########################### MAIN ############################# 

# ARGUMENT CHECK
while [ "$1" != "" ]; do
    case $1 in
	-r | --release-version )	shift
			releaseVersion=$1
			;;
	-s | --snapshot-version )	shift
			newSnapshot=$1
			;;
	# etc.
        -h | --help )	printHelp
                        exit
                        ;;
        * )             printHelp
                        exit 1
				;;
    esac
    shift
done


if [ -z "$releaseVersion" ]
	then echo "ERROR: MINOR RELEASE VERSION IS NOT AN INTEGER. FOUND VALUE --> $releaseMinor" >&2; exit 1
fi

# We must ensure that no tag already exists with the same name.
tagDoublon="$(git tag -l|grep $releaseVersion)"

if [ ! -z "$tagDoublon" ]
	then echo "ERROR : A tag already exists for the following version : $releaseVersion" >&2; exit 1
fi

echo "Execute for version $releaseVersion"

# Make release
execute