#!/bin/bash

function user_continue() {
  read -p "Do you want to continue? [y/n]" -n 1 -r
  echo
  if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    exit 1
  fi
}

if [ "$#" -ne 1 ]; then
  echo "USAGE: `basename $0` <repo-path>"
  exit
fi

REPO_PATH="${1}"
SHA=`git rev-parse --verify HEAD`
echo "================================================================="
echo "BEGIN PUBLISH SNAPSHOT for revision ${SHA} at ${REPO_PATH}"
echo "================================================================="
user_continue

# update pom to release version
mvn clean && mvn package -Dmaven.javadoc.skip=true -B -V

# Clone the maven repo
pushd ${REPO_PATH}
git pull
popd

# Deploy the artifact to the maven repo
mvn deploy -DskipTests -DaltDeploymentRepository=snapshot::default::file://${REPO_PATH}

# Push changes to the maven repo
pushd ${REPO_PATH}
git add .
git commit -a -m "Publish snapshot: ${SHA}"
git push origin master
popd

echo "Successfully published SNAPSHOT artifacts for ${SHA}"
