#!/bin/bash -e

call_svnversion() {
    svnrevision=`LC_ALL=C svn info | awk '/^Revision:/ {print $2}'`
    svndate=`LC_ALL=C svn info | awk '/^Last Changed Date:/ {print $4,$5}'`

    now=`date`

    if [ "$svnrevision" != "" ]; then
	if [ "$svndate" != "" ]; then
	    cat <<EOF > svnversion.h

// Do not edit!  This file was autogenerated 
//      by $0 
//      on $now
//
// svnrevision and svndate are as reported by svn at that point in time,
// compiledate and compiletime are being filled gcc at compilation

#include <stdlib.h>
 
static const char* svnrevision = "$svnrevision";
static const char* svndate = "$svndate";
static const char* compiletime = __TIME__;
static const char* compiledate = __DATE__;

EOF
	fi
    fi
}

call_gitversion() {
    gitrevision=$(git log --format=fuller | head -6 | awk '/^commit / {print substr($2,1,7)}')
    gitdate=$(git log --format=fuller | head -6 | awk '/^CommitDate: / {print $2, $3, $4, $5, $6, $7}')

    now=$(date)

    if [ "$gitrevision" != "" ]; then
	if [ "$gitdate" != "" ]; then
	    cat <<EOF > gitversion.h

// Do not edit!  This file was autogenerated 
//      by $0 
//      on $now
//
// gitrevision and gitdate are as reported by git at that point in time,
// compiledate and compiletime are being filled gcc at compilation

#include <stdlib.h>
 
static const char* gitrevision = "$gitrevision";
static const char* gitdate = "$gitdate";
static const char* compiletime = __TIME__;
static const char* compiledate = __DATE__;

EOF
	fi
    fi
}

if [ "$#" -ge 0 ]; then
    if [ "$1" = "--gitversion" ]; then
        # added hoops: make sure we only call this when we have a 
	# git binary in the path ... so that this does not get called
	# on machines that do not have git
	set +e
	gitprog=$(type -p git)
	set -e
	if [ "${gitprog}" != "" ]; then
	    call_gitversion 
	fi
	exit
    fi
fi

#test -f svnversion.h || call_svnversion
test -f gitversion.h || call_gitversion
aclocal 
autoheader 
automake 
autoconf 
./configure 
make
