#! /bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
TAG="$($DIR/next_tag)"

commit_snapshot () {
  $DIR/clean

  git commit -am "Creating new snapshot: $($DIR/get_version)"
  git push
}

if [ -z "$1" ]; then
  echo "Please supply a version increment argument: major, minor, or patch"
  echo "  e.g., src/main/tools/travis/prepare_release patch"
  exit 1
fi

echo "Preparing $($DIR/get_version)"

$DIR/version
$DIR/clean

mvn -q clean install

git commit -am "Creating new version: $($DIR/get_version)"
git push

$DIR/tag "$TAG"
$DIR/push_tag

if [ "$1" == 'major' ]; then
  $DIR/set_version "$($DIR/increment_version -M $($DIR/get_version))-SNAPSHOT"
  commit_snapshot
elif [ "$1" == 'minor' ]; then
  $DIR/set_version "$($DIR/increment_version -m $($DIR/get_version))-SNAPSHOT"
  commit_snapshot
elif [ "$1" == 'patch' ]; then
  $DIR/set_version "$($DIR/increment_version -p $($DIR/get_version))-SNAPSHOT"
  commit_snapshot
else
  echo "Error: unrecognized version increment argument"
  echo "  Please run this script with major, minor, or patch"
fi

