#!/bin/sh
# Copyright (C) 2020 Patrik Dufresne <patrik@ikus-soft.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 3 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, see <http://www.gnu.org/licenses/>.
#
#
#
# rdiff-backup-wrap
#
# This script is used to help the migration from legacy version (v1.2.8)
# to new version (v2.0.0) by auto-detecting the remote rdiff-backup version
# and then launch the right version locally.

# Extract the source and destination from the arguments
for ARG in "$@"; do
    SOURCE=$DEST
    DEST=$ARG
done

# Check if the source is remote.
case "$SOURCE" in
    *::*) REMOTE_SOURCE=1 ;;
    *) REMOTE_SOURCE=0 ;;
esac

# Pick the right version of rdiff-backup
CMD=rdiff-backup	# default if nothing else is found

for cmd in rdiff-backup-1 rdiff-backup2
do
	if command -v "$cmd" 2>&1 > /dev/null
	then
		CMD="$cmd"
	fi
done

if [ $REMOTE_SOURCE -eq 1 ]; then
    if rdiff-backup --test-server "$SOURCE" 2>&1 > /dev/null; then
	CMD="rdiff-backup"
    else
	# because the version we found previously might also not work
	if ! "$CMD" --test-server "$SOURCE" 2>&1 > /dev/null; then
	    echo "ERROR:   Couldn't find a working version of rdiff-backup, tried 'rdiff-backup' and '$CMD'" >&2
	    exit 1
	fi
    fi
fi

exec "$CMD" "$@"
