#!/bin/sh

extract_svn_version()
{
    if [ -d "$1"/.svn ]
    then
        svnversion -n "$1"
    elif [ -d "$1"/.git/svn ]
    then
        cd "$1" || exit 1
        git svn info . | grep '^Revision' | cut -d ' ' -f2 | tr -d '\n'
    else
        echo "unknown"
    fi    
}

srcdir="${1:-.}"

if [ -e "$srcdir"/include/vgversion_dist.h ]
then
    cp "$srcdir"/include/vgversion_dist.h include/vgversion.h.tmp
else
    cat > include/vgversion.h.tmp <<EOF
/* Do not edit: file generated by auxprogs/make_or_upd_vgversion_h.
   This file defines VGSVN and VEXSVN, used to report SVN revision
   when using command line options:  -v --version 
*/
#define VGSVN "$(extract_svn_version "$srcdir/.")"
#define VEXSVN "$(extract_svn_version "$srcdir/VEX")"
EOF
fi

if [ -f include/vgversion.h ]
then
    # There is already a vgversion.h.
    # Update it only if we found a different and real svn version
    if grep -q unknown include/vgversion.h.tmp ||
       cmp -s include/vgversion.h include/vgversion.h.tmp
    then
        rm -f include/vgversion.h.tmp
    else
        mv include/vgversion.h.tmp include/vgversion.h
    fi
else
    # There is no vgversion.h. Use the one just generated, whatever it is.
    mv include/vgversion.h.tmp include/vgversion.h
fi