#!/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 -b DIR [OPTIONS] DIR [DIR ...]"
        echo
        echo "This is a wrapper around rdup and rdup-cp"
        echo DIR \ - directories to restore
        echo
        echo OPTIONS:
        echo " -b DIR     restore to this directory"
        echo " -c REMOTE  restore the backup from a remote machine with ssh"
        echo " -k KEYFILE decrypt all files, using rdup-crypt"
        echo " -S         rdup-snapshot restore (does not use rdup -E)"
        echo " -D         rdup-dump restore (does use rdup -E), default"
        echo " -z         decompress all files, using rdup-gzip"
        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=""
pipe=""
ssh=""
dump=true
while getopts ":ab:k:c:vzxhV" o; do
        case $o in
                b) BACKUPDIR="$OPTARG"
                if [[ -z "$OPTARG" ]]; then
                        _echo2 "-b needs an argument"
                        exit 1
                fi
                ;;
                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/sbin/rdup-crypt -d $OPTARG"
                c="-c"
                ;;
                D)
                dump=true;;
                S)
                dump=false;;
                c)
                if [[ -z $OPTARG ]]; then
                        _echo2 "-c needs an argument"
                        exit 1
                fi
                ssh=" ssh -x $OPTARG"
                c="-c"
                l=""
                ;;
                z) pipe="$pipe | /usr/sbin/rdup-gzip -d"
                c="-c"
                ;;
                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
        _echo2 "No directories to restore"
        exit 1
fi
if [[ -z "$BACKUPDIR" ]]; then
        _echo2 "The -b argument is mandatory"
        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 :-)
        r="-E <(echo '$REG')"
else
        r=""
fi
# create the command line
if [[ -z $ssh ]]; then
        pipe="$pipe | /usr/sbin/rdup-cp $c $OPT $BACKUPDIR/$NOW"
else
        pipe="$pipe | $ssh rdup-cp $c $OPT $BACKUPDIR/$NOW"
fi
cmd="/usr/sbin/rdup $r $x $l $c /dev/null $@ $pipe"

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