#!/bin/bash
# determine head revision number of svn repository
# prints "r###" if no files are different, else prints "r###+"
# if svn is not found, prints the empty string

# if svn client is not in path, print a blank line and exit
# note the "2>/dev/null" before the command to suppress stderr
if [ "_$(2>/dev/null which svn)" == "_" ] ; then
  echo
# echo "no svn!"
  exit
fi

# Note: need to be in root of checked out files for this to work!
# This assumes that changes outside the checked out directory are
# irrelevant to what has been checked out.

# ensure that svn is valid here
svn info > /dev/null
if [ $? -ne 0 ]; then
    echo "rNONE"
    exit 1
fi

# get head revision of checked out files
REVISION=$(svn info -r 'HEAD' | grep Revision | grep -o '[0-9]\+')

# count changed files
CHANGED=$(svn diff | grep ^Index | wc -l | grep -o '[0-9]\+')

#echo "revison = '${REVISION}'"
#echo "changed = '${CHANGED}'"

# print main revision
echo -n "r${REVISION}"

# if files were changed, print a "+"
if [ ! "_${CHANGED}" == "_" ] && [ ! "_${CHANGED}" == "_0" ] ; then
  echo -n "+"
fi

# print a final newline
echo
