#!/bin/bash

if [[ $(git branch --show-current) -ne "master" ]]; then
  echo "Can only release on master branch"
  exit 1
fi

if [[ ! -z $(git diff origin/master) ]]; then
  echo "Need HEAD to match origin/master"
  exit 1
fi

echo "Fetching latest tags..."
git fetch --tags

LATEST_TAG=$(git describe --tags 2> /dev/null)

echo "Enter new version: (current version is ${LATEST_TAG:-0.0.0})"
read NEW_TAG

if [[ ! $(echo ${NEW_TAG} | grep '^\d\+\.\d\+\.\d\+$') ]]; then
  echo "New version must be of the format <major>.<minor>.<patch> (e.g. 1.4.2)"
  exit 1
fi

echo "Tagging as ${NEW_TAG}..."
git tag ${NEW_TAG}

echo "Pushing to remote..."
git push origin ${NEW_TAG}

echo -e "\n\n"
echo -e "Click 'Scan Multibranch Pipeline Now' to pick up the new tag from Git and then click the build icon under 'Built On':\nhttps://jenkins-og.xarth.tv/job/octarine-twitchfeatureingestionclient/view/tags/"