#!/bin/bash -exu

HERE=`dirname $0`
cd "$HERE"
HERE=`pwd`

if [[ $1 ]]
then
  echo "Performing version bump of type '$1'"
else
  exit -1
fi

cd "$HERE"
CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD`
if [[ $CURRENT_BRANCH == "master" ]]
then
  echo "On the master branch; looks good"
else
  git add .
  git commit -a
  git checkout master
  git pull
  git checkout "$CURRENT_BRANCH"
  git rebase master
  git checkout master
  git merge "$CURRENT_BRANCH"
fi

cd "$HERE"
git add .      # Ensure everything is staged
git commit -a || ( git commit -a | grep "nothing to commit" )  # Ensure everything is checked in
git pull
git push
npm version $1
git push origin --tags
npm publish
git push --all
