#!/bin/sh
# This script will automatically increment the package.json revision number
# Before publishing to npm. package.json Major / minor revision changes
# should be more deliberate and should be incremented manually.

# Parse package.json for package name / version.
PACKAGE_NAME=`grep "name" package.json | sed -e "s/\"name\": \"*//" -e "s/\",//" -e "s/ //g"`
VERSION=`grep "version" package.json | sed -e "s/\"version\": \"*//" -e "s/\",//" -e "s/ //g"`

# Replace points and split into array.
VERSION_ARRAY=(${VERSION//./ })

# Increment version.
((VERSION_ARRAY[2]++))
VERSION_NEXT="${VERSION_ARRAY[0]}.${VERSION_ARRAY[1]}.${VERSION_ARRAY[2]}"

# Inject new version into package.json.
echo "Incrementing package revision \"${VERSION}\" -> \"${VERSION_NEXT}\""
sed -i '' "s/\"${VERSION}\"/\"${VERSION_NEXT}\"/" package.json

# Publish!
echo "Publishing ${PACKAGE_NAME} ${VERSION_NEXT}"
npm publish
