#!/bin/bash

set -e
PROJECT=force-language-services
RELEASE_BRANCH=master
NPM_REGISTRY=https://heroku-npm.herokuapp.com

# Reset any state
echo
echo "Checking status of current branch"
echo

# This checks that you are on the master branch
if [[ -z $(git rev-parse --abbrev-ref HEAD | grep $RELEASE_BRANCH) ]]; then
    echo "You are not on the $RELEASE_BRANCH branch, the branch which we release from."
    echo "You should not be releasing from any other branch using this script."
    exit 1
fi

# This checks your current branch for uncommitted changes
if [[ -n $(git status -s) ]]; then
    echo 'You have unstaged changes according to `git status -s`'
    echo "Please commit, reset, or stash those changes"
    exit 1
fi

# This checks your current branch for differences
if [[ -z $(git status -uno | grep up-to-date) ]]; then
    echo "Your branch is *not* up-to-date with origin/$RELEASE_BRANCH"
    echo "You should either push or reset to what is at master."
    echo "If you are unsure, you most likely want to do git --reset hard origin/$RELEASE_BRANCH"
    exit 1
fi

# Reset any state
echo
echo "Resetting state from Git"
echo
# Checks in case there are any changes upstream and pulls it in
git pull
git clean -xfd

# Install the dependencies
# This will also update package.json (and override any old versions)
echo
echo "Installing dependencies and updating package.json"
echo
npm install
npm install --save --save-exact salesforce-alm@latest

# Runs the tests and generates the JavaScript files
echo
echo "Generating JavaScript and running tests"
echo
gulp clean
gulp test

# Commit the changes first
echo
echo "Committing changes into git locally"
echo
git add .
git commit -m "Preparing for release" || true

# Increment the version (use patch for now)
echo
echo "Determining the version for publishing"
echo "Your version to publish from local package.json: $(grep version package.json)"
echo "The versions available at $NPM_REGISTRY: $(npm info $PROJECT versions)"
echo
echo "How do you want to increment the version?"
echo "Select *minor* if this is a weekly release"
echo "Select *patch* if this is a hot fix to the weekly release"
echo "Select *none* if you are just going to use your package.json as-is"
select version in 'minor' 'patch' 'none'; do
    case $version in
        minor ) npm version minor; break;;
        patch ) npm version patch; break;;
        none ) break;;
        * ) echo "Please answer minor or patch";;
    esac
done

#
# The following could be moved to a Jenkins job since it doesn't require much human input
#

# Publish
echo
echo "Publishing to npm registry at $NPM_REGISTRY"
echo
# This checks that you are logged into the repository
if [[ -z $(npm whoami --registry $NPM_REGISTRY) ]]; then
    echo 'You are not logged in to the registry to publish.'
    echo 'Please log in or create an account, if necessary.'
    exit 1
fi
npm publish --tag latest --registry $NPM_REGISTRY

# Push the changes to Git
echo
echo "Pushing changes to remote git"
echo
git push

# A developer should now merge this back into the release branch
echo
echo "DONE. Please remember to merge this back into the branch called release."
echo