#!/bin/bash

if [ "$MAKETAG" == "false" ]; then
    exit 0
fi

if [ ! -f version ]; then
    ${dirname $BASH_SOURCE)/genversion > version
fi

. ~/.buildtoolsrc
git pull

TAGSHA1=$(git rev-list $TAG | head -n 1)
if [ $REBUILDING -eq 0 ]; then
    echo "This is a rebuild of a previous tag, not tagging or pushing" >&2
elif [ "$TAGSHA1" != "" ] && [ "$TAGSHA1" != "$SHA1" ]; then
    echo "Someone else tagged my buildnumber (branch|${MAJOR}|${BUILD}) onto $TAGSHA1, while I built it from $SHA1 ... "'Help!' >&2
    exit 1
elif [ "$TAGSHA1" != "" ] && [ "$TAGSHA1" == "$SHA1" ]; then
    echo "Someone else built this version at the same time I did, and we both tagged the same SHA1 with the same build tag. Not pushing my tag."
else
    git tag -f -a "build,${MAJOR},${BUILD}" $SHA1
    if [ "$PUSHTAG" != "false" ]; then
	git push
    fi
fi
