#!/bin/bash

# To override version/release from git,
# create VERSION file containing text with version/release
# eg. v3.4.0-1
PKG_VERSION=`cat VERSION 2> /dev/null || git describe --tags --match "v[0-9]*" 2> /dev/null`

function get_version ()
{
    # tags and output versions:
    #   - v3.4.0   => 3.4.0 (upstream clean)
    #   - v3.4.0-1 => 3.4.0 (downstream clean)
    #   - v3.4.0-2-g34e62f   => 3.4.0 (upstream dirty)
    #   - v3.4.0-1-2-g34e62f => 3.4.0 (downstream dirty)
    AWK_VERSION='
    BEGIN { FS="-" }
    /^v[0-9]/ {
      sub(/^v/,"") ; print $1
    }'

    echo $PKG_VERSION | awk "$AWK_VERSION" | tr -cd '[:alnum:].'
}

function get_release ()
{
    echo 1
}

if test "x$1" = "x--full"; then
    echo -n "v$(get_version)-$(get_release)"
elif test "x$1" = "x--version"; then
    get_version
elif test "x$1" = "x--release"; then
    get_release
else
    echo "usage: $0 [--full|--version|--release]"
    exit 1
fi
