#! /bin/bash

#
# A simple script to create a Git tag. If no tag is supplied to it, it creates a one from the
# snapshot version.
#

# Get the directory from which this script is run
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
TAG_PREFIX=$(mvn -q -Dexec.executable="echo" -Dexec.args='${tag.prefix}' --non-recursive exec:exec)

# Test if a tag is passed to this script
if [ -z "$1" ]; then
  if [[ $TAG_PREFIX == *"tag.prefix"* ]]; then
    TAG=$($DIR/next_version)
  else
    TAG="${TAG_PREFIX}-$($DIR/next_version)"
  fi
else
  if [[ $TAG_PREFIX == *"tag.prefix"* ]]; then
    TAG="$1"
  else
    TAG="${TAG_PREFIX}-${1}"
  fi
fi

git tag -s "$TAG" -m "Tagging \"$TAG\" for release"

echo "Created new tag: $TAG"
