#!/bin/bash

# Revision Log
# 2014-09-23 - Initial version - Ed <szynaka@localnet.com>
# 2014-09-24 - Added revsion notation - Ed <szynaka@localnet.com>

commit=$1

if [ -z "$commit" ]; then
    echo "Be sure your repo is up to date (ex: git pull) or provide a revission number"
    echo -n "Gathering the current revision number. "
    commit=$(git log -n 1 | head -n 1 | cut -c 8-)
    echo "done."
fi
echo -n "Promote commit $commit to live? [y/N] "
read -n 1 answer
echo

if [ "$answer" == "y" -o "$answer" == "Y" ]; then
    echo "Promoting"
    prev_tag="PrevLive";
    live_tag="Live"; 
    prev_rev=$(git rev-list $live_tag | head -n 1)

    # Tag the previous live
    git tag -d $prev_tag
    git push origin :refs/tags/$prev_tag 
    git tag -a -m "Previous Live was $prev_rev" $prev_tag $prev_rev
    git push --tags

    # Tag the new live
    git tag -d $live_tag
    git push origin :refs/tags/$live_tag
    git tag -a -m "Live set to $commit" $live_tag $commit
    git push --tags

    # Notes to user
    echo
    echo "Check the above for errors.  If all went well Live is now updated and PrevLive holds the previous Live commit"
else
    echo "Promotion aborted"
fi

