#!/bin/bash

srcdir=$1

if test -d .svn; then
    svn info . > conftemp.svn
else
    echo "" > conftemp.svn
fi

if test -d .svn && ( grep Revision: conftemp.svn > /dev/null; ); then
#if grep Revision: conftemp.svn > /dev/null ; then
    SVN_REV=`grep Revision: conftemp.svn|cut -d: -f 2|cut -c2-`
    SVN_URL=`grep URL: conftemp.svn|cut -d: -f 2-|cut -c2-`
    SVN_PATH=`grep URL: conftemp.svn|cut -d "/" -f 7-`
    SVN_TYPE=`echo ${SVN_PATH} |cut -d "/" -f 1`
    SVN_BRANCH=`echo "${SVN_PATH}"| cut -d "/" -f 2`
    url=`grep URL: conftemp.svn|cut -d: -f 2-|cut -c2-`
fi

if test -d .git; then
    GIT_SHA1=`git rev-parse HEAD | cut -c -8 `
fi

if test -f FULL_VERSION; then
    FULL_VERSION_FILE="FULL_VERSION"
    OLD_VERSION=`cat $FULL_VERSION_FILE`
else if test -n "$srcdir" -a -f $srcdir/FULL_VERSION; then
    FULL_VERSION_FILE="$srcdir/FULL_VERSION"
    OLD_VERSION=`cat $FULL_VERSION_FILE`
else
    FULL_VERSION_FILE="FULL_VERSION"
    OLD_VERSION=
fi
fi

if test -n "$srcdir"; then
    VERSION_FILE="$srcdir/VERSION"
else
    VERSION_FILE="VERSION"
fi
VERSION=`cat $VERSION_FILE`

if test -n "$SVN_REV"; then
    if test "${SVN_TYPE}" = "branches"; then
        VERSION=${VERSION}.svn.${SVN_REV}
    else if test "${SVN_TYPE}" = "trunk"; then
        VERSION=${VERSION}.svn.${SVN_REV}
    else
        RC=`echo "${SVN_BRANCH}"| grep "rc"`
        if test -n "$RC"; then
	    VERSION=`echo "${SVN_BRANCH}"| sed 's/[^0-9]*// ; s/[_.]//g'`
	    VERSION=`echo ${VERSION}| sed 's/^\([0-9]\)\([0-9]\)\([0-9]\)/\1.\2.\3/'`
        fi
    fi
    fi

else if test -n "$GIT_SHA1"; then
    VERSION=${VERSION}".git."${GIT_SHA1}

else if test -n "$OLD_VERSION"; then
    VERSION=$OLD_VERSION
fi
fi
fi

if test "$VERSION" != "$OLD_VERSION" -o $VERSION_FILE -nt FULL_VERSION; then
    echo "$VERSION" > FULL_VERSION
fi

