#!/bin/bash
# Run commit every second.
# Why? Cron can't do that.
# This can be used combined with a post-commit hook to make a kind of a versioned FTP :P
# You need to set-up a "path" file in the same directory as the script in this form:
# /absolute/working-copy/|https://repository-path/
#
# Known bugs: 
# Sometimes when you work with someone else on the same file it can overwrite others modifications with yours without throwing a conflict
# Good thing it's svn and you can easily merge the modifications afterwards. Still this bug is annoying

script_path="/home/sasha/Scripts/"
paths_file="/home/sasha/Scripts/svn-paths"

while [ true ]; do
	message="*Auto-message*"
	for local_path in `cat $paths_file| grep -v "#"`; do
		stat=`svn status $local_path`
		if [[ $stat != '' ]]; then #Are there any changes?
			delete_files=`echo $stat|grep -e "^\!"|cut -d" " -f2`
			if [[ $delete_files != '' ]]; then
				svn delete $delete_files>/dev/null 2>/dev/null
				svn commit --non-interactive -m "$message (delete)" $local_path >/dev/null 2>>"$script_path"commit_log
			fi
			# Do we have any files to add
			add_files=`echo $stat|grep -e "^\?"|cut -d" " -f2`
			if [[ $add_files != '' ]]; then
				#svn add $add_files >/dev/null 2>/dev/null # Add new files if there are any
				svn add $add_files>/dev/null 2>/dev/null # Add new files if there are any
				svn commit --non-interactive -m "$message (add)" $local_path >/dev/null 2>>"$script_path"commit_log # Finaly commit
			fi
			svn update $local_path >/dev/null 2>>/dev/null #Checkout first
			svn commit --non-interactive -m "$message (modified)" $local_path >/dev/null 2>>"$script_path"commit_log # Finaly commit
		fi
	done
	sleep 1
done

