#!/bin/bash
# Bump the tag patch version up by one
#
# Without any arguments it will simply print the next tag.
# With any command line arguments it will run 'git tag $@ ${tag}'
# for you where $@ can be any git-tag options.
#
if [ "$1" = "-h" -o "$1" = "--help" ]; then
    echo "git bump [tag options]"
    echo 
    echo "Increment the last package tag by one patch version."
    echo "If called without arguments it will print the new tag"
    echo "If called with --last it will put the last commit short log into tag"
    echo "If called with any other arguments (from git-tag) it will apply the new tag"
    echo
    echo "Examples:"
    echo "  git bump"
    echo "  git bump -am 'Tag message here'"
    echo "  git bump --last"
    exit 0
fi
dir=$(basename $(git rev-parse --show-toplevel))
last_tag=$(git tag -l ${dir}-??-??-?? | tail -1)
if [ -z "$last_tag" ]; then
    echo ${dir}-00-00-01
    exit 0
fi

patch=$(echo ${last_tag} | sed "s;${dir}-..-..-;;")
prefix=$(echo ${last_tag} | sed "s;-..$;;")
tag=$(printf "${prefix}-%02d\n" $(expr $patch + 1))

if [ $# -gt 0 ]; then
    case "$1" in
       --last)
         git tag -am "$(git log --format=format:%s -1)" ${tag}
         ;;
       *)
         git tag $@ ${tag}
         ;;
    esac
else
    echo $tag
fi

