#!/bin/bash
# Set up some useful vars.
FROMREPO="$1"
TOREPO="$2"
if [ "$FROMREPO" = "" ];
then
echo "You must specify a 'from' repository (as a URL)."
exit 1;
fi
if [ "$TOREPO" = "" ];
then
echo "You must specify a 'to' directory."
exit 1;
fi
# Set up binary shortcuts.
SVNADMIN=/usr/bin/svnadmin
SVNSERVE=/usr/bin/svnserve
SVNSYNC=/usr/bin/svnsync
SVN_PORT=7860
TOREPO_URL=svn://localhost:$SVN_PORT/
# Create an empty repository.
"$SVNADMIN" "create" "$TOREPO"
# Set the pre-revprop-change hook.
HOOK_PATH=$TOREPO/hooks/pre-revprop-change
echo "#!/bin/bash" > "$HOOK_PATH"
chmod 755 "$HOOK_PATH"
# Create a root user with full access.
SVNSERVE_PASSWD=$TOREPO/conf/passwd
echo "[users]" > "$SVNSERVE_PASSWD"
echo "root = root" >> "$SVNSERVE_PASSWD"
# Allow authed users write access.
SVNSERVE_CONF=$TOREPO/conf/svnserve.conf
echo "[general]" > "$SVNSERVE_CONF"
echo "anon-access = none" >> "$SVNSERVE_CONF"
echo "auth-access = write" >> "$SVNSERVE_CONF"
echo "password-db = passwd" >> "$SVNSERVE_CONF"
echo "realm = $TOREPO" >> "$SVNSERVE_CONF"
# Launch svnserve as a background process.
$SVNSERVE -d --foreground -r "$TOREPO" --listen-host localhost --listen-port "$SVN_PORT" &
# Preserve the PID.
SVNSERVE_PID=$!
# Check the PID is sensible.
if [ "$SVNSERVE_PID" = 0 ];
then
echo "Failed to start svnserve";
exit 1;
fi
# Output some info.
echo "Started svnserve with PID $!";
# Create a var for our exit status.
EXIT_STATUS=0
# Initialise the sync.
"$SVNSYNC" init --sync-username root --sync-password root "$TOREPO_URL" "$FROMREPO"
if [ "$?" != 0 ];
then
echo "Initialising the sync failed." 1>&2;
EXIT_STATUS=1;
fi
if [ "$EXIT_STATUS" = 0 ];
then
# Start it off!
"$SVNSYNC" sync "$TOREPO_URL";
if [ "$?" != 0 ];
then
echo "Sync failed." 1>&2;
EXIT_STATUS=1;
fi
fi
# Kill the svnserve process.
echo "Closing down svnserve mirror"
kill -9 "$SVNSERVE_PID"
exit "$EXIT_STATUS"