#!/bin/bash
#
# counts up the jar version file in the Repository
# the version file is that file where the current version is stored
# 
# $Id: countJarVersion,v 1.6 2006-08-30 11:51:52 inva Exp $
#

usage () {
	echo "usage $0 <version file>"
	exit 1;
}

if test $# -ne 1 ; then usage; fi

verpath="eric_admin/versions"
verfile="$verpath/$1"
use_cvs=`[ -d CVS ] && echo 1 || echo 0`
if [ "$use_cvs" = "1" ]; then
    reporoot=$(cat CVS/Root)
else
    reporoot=$(LC_ALL=C svn info | grep Root | awk '{print $3}')
fi
retries=5
count=0

mkdir versions >/dev/null 2>&1
cd versions || exit 1

while [ $count -lt $retries ]; do
    count=$((count+1))

    #checkout version file
    rm -f $verfile
    if [ "$use_cvs" = "1" ]; then
	cvs -d $reporoot -Q co $verfile || continue
    else
	svn -q co $reporoot/trunk/$verpath $verpath || continue
    fi

    #calculate new version
    versionbase=`awk -F. '{print $1 "." $2 "." $3 "."}' < $verfile`
    newversion=`awk -F. '{print $1 "." $2 "." $3 "." $4+1}' < $verfile`
    echo $newversion > $verfile;
    
    #checkin version file
    if [ "$use_cvs" = "1" ]; then
	cvs -d $reporoot -Q commit -m "by countJarVersion" $verfile > /dev/null || continue
    else
	svn -q commit -m "by countJarVersion" $verfile > /dev/null || continue
    fi
    echo $0: new version of versionbase $versionbase is $newversion
    exit 0
done

echo "$0: Giving up on file $1 after $retries failed attempts"
exit 1

