#!/bin/bash
# $Id$
usage () {
    echo "tagversion, tag version in git and push it"
    echo "Tag format is stable-<VERSION> for master or <branch>-<VERSION>,"
    echo "e.g. stable-2.26.3 or Code-11-SP3-2.25.15"
    echo ""
    echo "options:"
    echo "  -h, --help:     this help"
    echo "  -c, --check:    check if package is already tagged (quick local check)"
}

OPTIONS=`getopt --name $0 --options hc --longoptions help,check -- "$@"` || { usage; exit 1; }
eval set -- "$OPTIONS"
while true; do
    case "$1" in
	--)
	    shift
	    break
	    ;;
	-h|--help)
	    usage
	    exit
	    ;;
  -c|--check)
	    CHECK=1
	    ;;
    esac
    shift
done

p=`cat RPMNAME`
v=`grep -m 1 '^[[:space:]]*Version:' package/$p.spec | sed -e 's/Version:[[:space:]]*\([[:print:]]\+\)/\1/' | tr "." "_"`
if [ -z "$p" -o -z "$v" ]; then
    echo "Cannot find RPMNAME or Version value in package/$p.spec in `pwd`"
    exit 1
fi

BRANCH=`git branch | sed '/^[^\*]/d;s/^\*[[:space:]]*//;'`
if [ "$BRANCH" = "master" ]; then
  t=stable-$v
else
  t=$BRANCH-$v
fi

if [ $CHECK -eq 1 ]; then
  # simple check local tags if developer do not forget to create tag
  COUNT=`git tag | grep -c $t`
  if [ $COUNT -ge 1 ]; then
    echo "Tag is there."
    exit 0
  else
    echo "Tag '$t' missing."
    exit 1
  fi
else
  git tag $t
  git push --tags
fi

