#!/bin/bash

# CHANGES:
# Feb 2nd 2010: Fixing a nasty bug found bug Jason Judge that would make
#		the script fail once in a while;

#------------------------------- Subroutines ---------------------------------
usage(){
echo " Usage: $(basename $0) PATH" 
echo ""
echo "Automatically commits the changes of svn working copy located in PATH."
echo "The new files are automatically added and the files that have been removed"
echo "are removed."
echo ""
echo "By Gael Varoquaux"
}

#------------------------------- Process the options -------------------------
if [ $# -eq 1 ]
then
    workingdir="$1"
else
    usage
    exit 1
fi

if ! cd $workingdir
then
    echo $workingdir is not a accessible path.
    usage
    exit 1
fi

#------------------------------- Find out what has changed -------------------

# A warning if this fails :
echo "SVN autocommit failed" > $HOME/local/motd

svnstatus=$(svn status $workingdir)
added=$(printf "%s" "$svnstatus" | sed -n 's/^[A?] *\(.*\)/\1/p')
removed=$(printf "%s" "$svnstatus" | sed -n 's/^! *\(.*\)/\1/p')

if [ "x$added" != "x" ]
then
    echo adding "$added" to repository
    svn add "$added"
fi

if [ "x$removed" != "x" ]
then
    echo removing "$removed" to repository
    svn remove "$removed"
fi

svn commit -m "autocommit" && rm $HOME/local/motd
