#!/bin/bash

if ! grep -q 'project[(]dplotter' CMakeLists.txt; then
    echo "Not called in right place"
    exit 1
fi

#ECHO=echo
VERSION=$(grep 'set[(]dplotter_VERSION' CMakeLists.txt | \
    sed -e 's@set[(]dplotter_VERSION[[:space:]]\{1,\}\([0-9.]*\)[[:space:]]*[)]@\1@')
GITTAG=$(echo "${VERSION}")

# root of the repository with source
GITSERVER=$(git remote -v | grep '^origin.*[(]fetch[)]$' | \
                sed -e 's/origin[[:space:]]*\([^[:space:]]*\).*$/\1/')

# does the tag exist?
HAVETAG=$(git tag -l "$GITTAG")

if [ -n "$HAVETAG" ]; then
   if [ x$1 = x-f ]; then
      echo "Forcing, first removing ${GITTAG}"
      $ECHO git push origin --delete ${GITTAG}
   else
      echo "Tag already exists, you might consider forcing a re-tag with -f"
      exit 1
   fi
fi

echo "Creating tag ${GITTAG}"
$ECHO git tag -a "$GITTAG" -m "dplotter version ${VERSION}"
$ECHO git push origin "$GITTAG"
