#!/bin/sh

# this script creates version.cpp containing version and other compile-time
# information.  it expects to get the source directory as only argument.

output=version.cpp

src=$1
test "$src" = '' && src=.

date=`date --utc +%Y-%m-%d`
if test -e "$src/.git"; then
	commit=`( cd "$src"; git rev-parse HEAD 2> /dev/null; )`
	version=`( cd "$src"; git describe --tags HEAD 2> /dev/null; ) || echo "$commit"`
	if test "$commit" != ''; then
		( cd "$src"; git diff --quiet HEAD; ) || { commit="$commit (dirty)"; version="$version (dirty)"; }
	fi
fi
test -z "$commit" -a -e "$src/gitcommit" && commit=`cat "$src/gitcommit"`
test -z "$version" -a -e "$src/version" && version=`cat "$src/version"`
test -z "$commit" && commit=unknown
test -z "$version" && version=unknown
cat > "$output-new" <<EOF
#include "common.h"
const char *const recorderCommit = "$commit";
const char *const recorderDate = "$date";
const char *const recorderVersion = "$version";
EOF

if test -e "$output" && diff -q "$output" "$output-new" > /dev/null 2>&1; then
	rm "$output-new"
else
	mv -f "$output-new" "$output"
fi

