#!/bin/sh

#
# aovsvnmirror - A Subversion post-commit script to mirror a repository
#
# Copyright (C) 2006	  Angel Ortega <angel@triptico.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# http://www.triptico.com
#

VERSION="1.0"

# Configuration -----------------------------

# spool directory
SPOOL="/home/angel/svn-spool"

# remote host and repository
REMOTE_HOST="voksar.triptico.com"
REMOTE_REPO="/home/angel/svn"

# path to svnadmin
SVNADMIN="svnadmin"

# log file
LOGFILE="/tmp/aovsvnmirror.log"

# End of configuration ----------------------

# Arguments
REPO="$1"
REV="$2"

if [ "$REPO" = "" ] ; then
	echo "Usage: $0 {repository} [{revision}]"
	exit 1
fi

# create spool directory
mkdir -p $SPOOL ||
(
	echo "$0: Can't create $SPOOL"
	exit 2
)

# if $REPO is --flush, skip any dumping
if [ "$REPO" != "--flush" ] ; then

	# if no revision, create a full dump file as revision 0
	if [ "$REV" = "" ] ; then
		REV=0
		ARGS=""
	else
		ARGS="-r $REV --incremental"
	fi

	# create job using the revision
	JOBFILE=`printf "%010d" $REV`.dump

	# do the dumping
	$SVNADMIN dump $REPO $ARGS --deltas > ${SPOOL}/${JOBFILE} 2>> ${LOGFILE}
fi

# if $REMOTE_HOST is not alive, do nothing (jobs are queued)
ping -c 1 $REMOTE_HOST > /dev/null 2>> ${LOGFILE} || exit 3

# remote host responded to ping; inject it all spooled jobs
for j in ${SPOOL}/*.dump ; do

	# load into repository or fail
	ssh $REMOTE_HOST "svnadmin load $REMOTE_REPO" < $j 2>> ${LOGFILE} || exit 4

	# done; delete queued job
	rm $j
done
