#!/bin/bash

if [ ! -z "$EMC2_HOME" ]; then
    source $EMC2_HOME/scripts/githelper.sh
else
    source $(git rev-parse --show-toplevel)/scripts/githelper.sh
fi

githelper $1

if [ "$DEB_COMPONENT" = "scratch" ]; then

    DESCRIBE=$(git describe --tags --exact-match 2>/dev/null)
    if [ -n "$DESCRIBE" ]; then
        echo "$DESCRIBE"
    else
        DESCRIBE=$(git describe --tags 2>/dev/null)
        if [ -n "$DESCRIBE" ]; then
            echo "$DESCRIBE"
        else
            BR=$(printf '%s' "$GIT_BRANCH" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^-.+:~a-z0-9]/-/g; s/-{2,}/-/g; s/^-//; s/-$//')
            echo "v$(git show HEAD:VERSION | cut -d ' ' -f 1)~${BR:-head}~$(git show --pretty=format:%h HEAD | head -1)"
        fi
    fi
else
    # known branches get the "describe" of the most recent signed git tag,
    # or of the most recent unsigned tag if no signed tags are found
    git describe --match "$GIT_TAG"
fi

