#!/bin/bash

set -e

function usage {
    cat <<EOF
Usage: xivo-bump-package-version [-h] <version> <codename> <branch> <LTS>
	   - <version> is of form <MAJOR>.<MINOR>.<PATCH> (i.e. 2017.03.02)
	   - <codename> is LTS code name (freya, electra,...)
	   - <branch> is the git branch to checkout, default to master
	   - <LTS> set to true if this is the last IV becoming the new LTS or a bugfix release

	   - --help, -h displays this help
EOF
    exit 1
}

function usage_target_version {
    echo "<version> is not of form <MAJOR>.<MINOR>.<PATCH> (i.e. 2017.03.02)"
    usage
}

function parse_version {
	# Check version
	IFS='.' read -ra varray <<< "${version}"
	for i in {0..2}
	do
		if [ -z "${varray[$i]}" ]; then
			usage_target_version
		fi
	done
	XIVOCC_TAG=${varray[0]}.${varray[1]}
}
verify_help_asked() {
    if [ "$1" == "-h" ] || [ "$1" == "--help" ] ; then
        usage
    fi
}

verify_help_asked "$1"
version="$1"
distribution="$2"
GIT_BRANCH="$3"
LTS="$4"

changelog=$(head -1 debian/changelog)

if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]] ; then
	usage
fi

if [ -z "$version" ]; then
	echo "Missing version argument"
	usage
else
	parse_version
fi

if [ -z "$GIT_BRANCH" ]; then
    printf "<branch> argument missing, using master\n"
	GIT_BRANCH=master
fi

if [ -z "$LTS" ]; then
    printf "<LTS> argument missing, using false\n"
	LTS="false"
fi

#move towards given branch
echo "~~ Checking out $GIT_BRANCH ~~"
git checkout "$GIT_BRANCH"
echo "~~ Cleaning git workspace ~~"
git reset --hard
git clean -f -d
echo "~~ Pulling latest changes from server ~~"
git pull >/dev/null
GIT_IS_CLEAN=$(git status --porcelain)
if [ -n "$GIT_IS_CLEAN" ]; then
    echo "Your workspace is not clean !"
    exit 1
fi

#parse changelog
if [[ "$changelog" =~ \((.*)\) ]] ; then
    source_version=${BASH_REMATCH[1]}
else
	echo "Unparseable changelog $changelog"
	exit 1
fi

if [[ "$changelog" =~ \)\ (.*)\; ]] ; then
    source_distribution=${BASH_REMATCH[1]}
else
	echo "Unparseable changelog $changelog"
	exit 1
fi

if [ -n "$distribution" ]; then
	set_distribution="xivo-${distribution}"
else
	set_distribution=$source_distribution
fi

if [[ "$source_version" =~ (.*)\: ]] ; then
    source_prefix=${BASH_REMATCH[1]}
	set_version=$source_prefix:$version
else
	source_prefix=""
	set_version=$version
fi

if [ "${set_version}:${set_distribution}" = "${source_version}:${source_distribution}" ]; then
	echo "Version is already set to $version (changelog ${set_version} $set_distribution)"
	exit 0
fi

echo "Setting version to '$set_version' with distribution '$set_distribution'"
debchange -b -D "$set_distribution" --force-distribution --newversion "$set_version" "Bump version to $version"
echo "Commiting"
git add debian/changelog
if [ -f ./VERSION ]; then
    echo "$version" > ./VERSION
	git add ./VERSION
fi

if [ -f ./VERSION-LTS ]; then
	if [ "$LTS" = "true" ]; then
		echo "${distribution^}" > ./VERSION-LTS
	else
		echo "${distribution^} Intermediate Version - limited support" > ./VERSION-LTS
	fi
	git add ./VERSION-LTS
fi

# factory env
find ./ -name "factory.env" -exec sed -i -E "s/XIVOCC_TAG=.*\$/XIVOCC_TAG=$XIVOCC_TAG/g" {} \; -exec git add {} \;

git commit -m "Bump version to ${version} (${set_distribution})"
git push origin
