#!/usr/bin/env bash
#
# Automated creation of major and minor releases
#
# Usage: release VERSION
#
# VERSION should be a semver for the next release
#
# For patch releases (i.e. hotfixes) you need to
# do manually
#
# Environment variables:
# REMOTE   Remote to push the new release branch
# DEVELOP  Branch with features for the release
# STAGING  Branch for staging purposes

set -e

REMOTE=${REMOTE:-origin}
DEVELOP=${DEVELOP:-develop}
STAGING=${STAGING:-homologacao}
RELEASE="release/$1"

echo "Updating staging branch..."
git checkout "$STAGING"
git pull

echo
echo "Merging develop branch..."
git merge "$REMOTE/$DEVELOP" --no-ff -m "Prepare release $1"
git push

echo
echo "Creating release branch..."
git checkout -b "$RELEASE"
git push -u "$REMOTE" "$RELEASE"