1#!/bin/sh
2
3tarball_version_file=VERSION
4
5# Use tarball version file (included in release tarballs) if present,
6# otherwise, try git-describe
7if test -f $tarball_version_file
8then
9    v=$(cat $tarball_version_file) || v=
10fi
11
12if test "x$v" != "x"
13then
14    : # use $v
15# Make sure it's a Git tree
16elif test "$(git log -1 --pretty=format:x 2>&1)" = "x"
17then
18    v=$(git describe --match 'v[0-9]*' HEAD 2>/dev/null | sed -e 's/^v//') \
19        || v="UNKNOWN"
20
21    # Append "-dirty" suffix if there is local change
22    git update-index --refresh > /dev/null 2>&1
23    if test "x$(git diff-index --name-only HEAD 2>/dev/null)" != "x"
24    then
25        v="$v-dirty"
26    fi
27fi
28
29printf "%s" "$v"
30