#!/bin/bash

if [ ! -e .git ]; then
  echo "not in an development environment, no .git directory found" >&2
  exit 1
fi

# find an exact match, if found use it as version as we are currently
# exactly on one tag
version=`git describe --tag --exact-match 2>/dev/null`
if [ $? -eq 0 ]; then
  echo $version | tr -d 'v'
  exit 0
fi

# if we are not on a exact tag, use the last tag and add the date
version=`git tag -l | sort -V | sed -e 's/^v//g' | tail -n 1`
if [ $? -eq 0 ]; then
  version=`echo "$version" | awk -F "-" '{ print $1 }'`
  date=`date +%Y-%m-%d`
  major=` echo $version | cut -d . -f 1`
  minor=`echo $version | cut -d . -f 2 | sed -e 's/^0*//g'`
  if [ "x$minor" = "x" ]; then
    minor=0
  fi

  # do we have a even minor version?
  let mod=$minor%2
  if [ $mod -eq 0 ]; then
    minor=$((minor+1))
  fi
  minor=`printf "%02d" $minor`
  version="$major.$minor"

  echo ${version} ${date} | tr -d 'v'
  exit 0
fi

echo "unknown"
exit 1
