#!/bin/sh
#
# Usage:
#   gnc-svnversion <srcdir>
#
# Prints the revision number to stdout and exits 0 on success
# exits with errorcode 1 if we're not in an svn, svk or git checkout
#
# Written By:  Derek Atkins <derek@ihtfp.com>
#
# $Id$

# Print an error message and then exit
my_die()
{
  echo "$1"
  exit 1
}

# Make sure we have a srcdir
[ -n "$1" ] || my_die "Usage: $0 <srcdir>"
[ -d "$1" ] || my_die "$0: $1: not a directory"

# Find the real srcdir.
# This handles the case of a symlink (lndir) tree
# $real_srcdir will hold the actual source dir
if test -h "$1"/Makefile.am
then 
  tmp_srcdir=`readlink "$1"/Makefile.am`
  real_srcdir="$1/`dirname ${tmp_srcdir}`"
else
  real_srcdir="$1"
fi

# Test if this code is an SVN Checkout
#   If this is an svn checkout we assume you have svnversion!
if test -d "${real_srcdir}"/.svn
then
  svnversion "$real_srcdir"
  exit $?
fi

# If we get here then this is NOT an svn checkout.

# Maybe it's git?
real_gitdir="${real_srcdir}"/.git
if test -d "${real_gitdir}"
then
  githead=`git --git-dir "${real_gitdir}" log -1 --pretty=format:"%h" HEAD 2>/dev/null`  # short hash only
  if test $? = 0 ; then
    echo -n $githead
    # Add a "+" to the hash if there deleted or modified files (not excluded by .gitignore and friends)
    # "Ignores" untracked files
    # [ $(git --git-dir "${real_gitdir}" ls-files -dkm 2>/dev/null | wc -l) -gt 0 ] && echo -n "+"
    # Includes untracked files
    [ $(git --git-dir "${real_gitdir}" ls-files -dkmo --exclude-standard 2>/dev/null | wc -l) -gt 0 ] && echo -n "+"
    echo
    exit 0
  else
    exit 1
  fi
fi

if test -d "${real_srcdir}"/.bzr ;
then
    bzrhead=`(cd "${real_srcdir}"; bzr version-info --custom --template='{revno}\n')`
    if test $? = 0 ; then
	echo "$bzrhead";
	exit 0
    else
	exit 1
    fi
fi

if test $OSTYPE -a $OSTYPE = "msys";
then
  svk_name="svk.bat"
  svk_cmd="cmd \/c svk"
else
  svk_name="svk"
  svk_cmd="svk"
fi

# Maybe it's SVK?  First, check if we've got 'svk' in the path.  If not,
# then exit with an error code of 1..
which $svk_name >/dev/null 2>&1
if test $? != 0 ; then exit 1 ; fi

# Okay, we have 'svk'.  Now see if $real_srcdir is an svk checkout
# Note that the 'echo n' is to protect against having svk installed
# without having configured the depotmap.
svkinfo=`echo n | $svk_cmd info "$real_srcdir" 2>&1`
if test $? != 0 ; then exit 1 ; fi

# If we got here, then $real_srcdir is an svk checkout.  Parse out
# the revision info, print it out, and then output 0.  Just combine
# all the revision numbers into a single string by combining them
# with periods.
svkinfo=`$svk_cmd info "$real_srcdir" | grep Rev | head -5 | sed -e 's/^.* \([0-9:]*\)$/\1/'`
echo $svkinfo | sed -e 's/ /./g'

exit 0
