#!/bin/bash

# create a restore of a backup in -b <dir>

prefix=/usr
exec_prefix=/usr
datadir=/usr/share/rdup

# common stuff
source $datadir/shared.sh

usage() {
        echo "$PROGNAME SOURCE [SOURCE ...] DIR"
        echo
        echo "This is a wrapper around rdup and rdup-cp"
        echo DIR \ - directory to restore to
        echo SOURCE \ - source to restore from:
	echo "  ssh://user@host/directory (note: no colon after the hostname"
        echo "  file:///directory (note: 3 slashes)"
        echo "  /directory"
        echo "  directory"
        echo
        echo OPTIONS:
        echo " -k KEYFILE decrypt all files, using rdup-crypt"
        echo " -z         decompress all files, using rdup-gzip"
        echo "            If both -z and -k are selected, decryption is done first"
        echo " -S         rdup-snapshot restore (does not use rdup -E)"
        echo " -D         rdup-dump restore (does use rdup -E), default"
        echo " -a         use extended attributes with uid/gid"
        echo " -v         echo the files processed to stderr"
        echo " -x         pass -x to rdup"
        echo " -h         this help"
        echo " -V         print version"
}

PROGNAME=$0
BACKUPDIR=""
c=""
pipe=""
ssh=""
zip=false
dump=true
while getopts ":ak:vzxhSDV" o; do
        case $o in
                k) KEYFILE=$OPTARG
                if [[ -z "$OPTARG" ]]; then
                        _echo2 "-k needs an argument"
                        exit 1
                fi
                if [[ ! -r "$OPTARG" ]]; then
                        _echo2 "Cannot read keyfile \`$OPTARG': failed"
                        exit 1
                fi
                pipe="$pipe | /usr/bin/rdup-crypt -d $OPTARG"
                c="-c"
		if $zip; then
			_echo2 "Select decryption first, then decompression"
			exit 1
		fi
                ;;
                D)
                dump=true;;
                S)
                dump=false;;
                z) pipe="$pipe | /usr/bin/rdup-gzip -d"
                c="-c"
		zip=true
                ;;
                a) OPT="$OPT -a";;
                v) OPT="$OPT -v";;
                x) x="-x";;
                V) version && exit;;
                h) usage && exit;;
                \?) _echo2 "Invalid option: $OPTARG"; exit 1;;
        esac
done
shift $((OPTIND - 1))
if [[ $# -eq 0 ]]; then
	usage
        exit
fi

# rdup [options] sources destination
#dest="ssh://elektron.atoom.net/directory"
#dest="ssh://elektron.atoom.net/directory/var/"
#dest="file:///var/backup"
#dest="/var/backup"
#dest="ssh://miekg@elektron.atoom.net/directory"

# look through the source dirs
# either one ssh:// or all local is allowed
i=1; last=$#; SOURCE=
ssh_seen=false
while [[ $i -lt $last ]]; do
	src=$1
	if [[ ${src:0:6} == "ssh://" ]]; then
		if $ssh_seen; then
			_echo2 "Sorry; there can only be one remote source"
			exit 1
		fi

		rest=${src/ssh:\/\//}
		u=`echo $rest | cut -s -f1 -d@`
		rest=${rest/$u@/}
		h=`echo $rest | cut -s -f1 -d/`

		c="-c"
		l=""     # enable race checking
		ssh_seen=true
		if [[ -z $u ]]; then
			ssh=" ssh -x $h"
		else
			ssh=" ssh -x $u@$h"
		fi
		SOURCE=${rest/$h/}
	fi
	if [[ ${src:0:7} == "file://" ]]; then
		if $ssh_seen; then
			_echo2 "Sorry; a remote source has been specified"
			exit 1
		fi
		rest=${src/file:\/\//}
		SOURCE="$SOURCE $rest"
	fi
	if [[ ${src:0:1} == "/" ]]; then
		if $ssh_seen; then
			_echo2 "Sorry; a remote source has been specified"
			exit 1
		fi
		SOURCE="$SOURCE $src"
	fi

	# relative dirs
	shift
	((i=$i+1))
done
dest=$1
if [[ ${dest:0:6} == "ssh://" ]]; then
	_echo2 "Destionation cannot be a remote host"
	exit 1
fi

# add pwd if the path was relative
if [[ ${1:0:1} == "/" ]]; then
        dest="$1"
else
        dest="`pwd`/$1"
fi

# regular expresion to restore only the latest #
#REG='\+\d\d\.\d\d:\d[\d/|\d$]'
REG='\+..\...\:..$'
if $dump; then
        # nice :-)
	if [[ -z $ssh ]]; then
	        r="-E <(echo '$REG')"
	else
	        r="-E \"<(echo '$REG')\""
	fi
else
        r=""
fi

# create the command line
if [[ -z $ssh ]]; then
	pipe="$pipe | /usr/bin/rdup-cp $c $OPT $dest"
	cmd="/usr/bin/rdup $r $x $l $c /dev/null $SOURCE $pipe"
else
	# needs to be found with PATH
	pipe="$pipe | rdup-cp $c $OPT $dest"
	cmd="$ssh /usr/bin/rdup $r $x $l $c /dev/null $SOURCE $pipe"
fi
#cmd="/usr/bin/rdup $r $x $l $c /dev/null $SOURCE $pipe"

# execute the backup command
# _echo2 "Executing: ${cmd}"
eval ${cmd}
