#!/bin/sh
#----------------------------------------------------------------------
# smeserver-rdiff-backup
# Copyright (C) 2005-2006 Gordon Rowell <gordonr@gormand.com.au>
#
# 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 or 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
#----------------------------------------------------------------------

/sbin/e-smith/config get rdiff-backup >/dev/null || exit 0

ID=$(/sbin/e-smith/config getprop sysconfig SystemID)

FORCE="--force"

RDIFF_BACKUP="/usr/bin/rdiff-backup"
OPTIONS="--include-globbing-filelist /etc/rdiff-backup.filelist"

OPTIONS="$OPTIONS --exclude /"

OPTIONS="$OPTIONS --print-statistics"

OPTIONS="$OPTIONS $FORCE"

REMOVE_OLDER="--remove-older-than $(/sbin/e-smith/config getprop rdiff-backup RemoveOlderThan) --force"

/sbin/e-smith/signal-event pre-backup || exit 1

if [ $(/sbin/e-smith/config getprop rdiff-backup EnableLocal) = 'yes' ]
then
    LOCAL_PATH=$(/sbin/e-smith/config getprop rdiff-backup LocalPath)

    if [ ! -d $LOCAL_PATH ]
    then
        echo "$LOCAL_PATH does not exist - is it mounted?"
        exit 2
    fi

    $RDIFF_BACKUP $OPTIONS / $LOCAL_PATH/$ID || exit 3

    $RDIFF_BACKUP $REMOVE_OLDER $LOCAL_PATH/$ID || exit 4
fi

if [ $(/sbin/e-smith/config getprop rdiff-backup EnableRemote) = 'yes' ]
then
    REMOTE_PATH=$(/sbin/e-smith/config getprop rdiff-backup RemotePath)

    REMOTE_HOST="rdiff-backup-$(/sbin/e-smith/config getprop rdiff-backup RemoteHost)"

    if [ $REMOTE_HOST = "localhost" ]
    then
        echo "$0: Skipping remote backup to localhost"
    else
        $RDIFF_BACKUP $OPTIONS / $REMOTE_HOST::$REMOTE_PATH/$ID || exit 5

        $RDIFF_BACKUP $REMOVE_OLDER $REMOTE_HOST::$REMOTE_PATH/$ID || exit 6
    fi
fi

/sbin/e-smith/signal-event post-backup || exit 7
