#!/bin/bash

# Inspired by linux-2.6/scripts/setlocalversion

set -e

FILE=localversion

compute_version()
{
    if [ ! -d .git ]; then
        return;
    fi
    head=$(git rev-parse --verify --short HEAD)
    printf "~git-%s" "$head"
    # Check for uncommitted changes
    if git diff-index --name-only HEAD | read dummy; then
        printf '%s' "-dirty"
    fi

}

[ -f "$FILE" ] || touch "$FILE"

old_version=$(< "$FILE")
version=$(printf '"%s"\n' "$(compute_version)")

if [ "$version" != "$old_version" ]; then
    echo "$version" > "$FILE"
fi