#!/bin/bash

# create a (mirror) backup in -b dir 
# figure out of the dump should be a full one or incremental

prefix=/usr
exec_prefix=/usr
datadir=/usr/share/rdup
cstream=@cstream@
sysconfdir=/etc
lockdir=/var/lock

# shared stuff
source $datadir/shared.sh

usage() {
        echo "$PROGNAME -b DIR [OPTIONS] DIR [DIR ...]"
        echo
        echo "This is a wrapper around rdup and rdup-mirror"
        echo
        echo DIR \ - directories to back up
        echo
        echo OPTIONS:
        echo " -b DIR     backup directory (adds YYYYMM)"
        echo " -u         backup directory is unique, don't add YYYYMM dirs"
        echo " -c REMOTE  dump the backup on a remote machine with ssh"
        echo " -k KEYFILE encrypt all files, using rdup-crypt"
        echo " -n PREFIX  optional prefix for the timestmp and filelist"
        echo " -E FILE    use FILE as an exclude list"
        echo " -f         force a full dump"
        echo " -z         compress all files, using rdup-gzip"
        echo " -a         write extended attributes with uid/gid"
        echo " -e         filelist and timestamp are put in backup directory"
        echo " -v         echo the files processed to stderr"
        echo " -x         pass -x to rdup"
        echo " -h         this help"
        echo " -V         print version"
}

d=`date +%Y%m`
etc=false
PROGNAME=$0
PREFIX=""
BACKUPDIR=""
BACKUPDIR_PLAIN=""
FIRST="" # the first backup dir
OPT=""
c=""
l="-l"
pipe=""
ssh=""
u=false
force=false
E=""

while getopts ":ab:k:c:n:vzfxuehV" o; do
        case $o in
                b) BACKUPDIR="$OPTARG"
                if [[ -z "$OPTARG" ]]; then
                        _echo2 "-b needs an argument"
                        exit 1
                fi;;
                E)
                if [[ -z "$OPTARG" ]]; then
                        _echo2 "-E needs an argument"
                        exit 1
                fi
                E="-E $OPTARG"
                ;;
                k)
                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 $OPTARG"
                c="-c"
                ;;
                c)
                if [[ -z $OPTARG ]]; then
                        _echo2 "-c needs an argument"
                        exit 1
                fi
                ssh=" ssh -x $OPTARG"
                c="-c"
                l=""
                ;;
                n) PREFIX=$OPTARG
                if [[ -z $OPTARG ]]; then
                        _echo2 "-N needs an argument"
                fi;;
                e) etc=true;;
                u) u=true;;
                f) force=true;;
                z)
                pipe="$pipe | /usr/sbin/rdup-gzip"
                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 backup"
        exit 1
fi
if [[ -z "$BACKUPDIR" ]]; then
        _echo2 "The -b argument is mandatory"
        exit 1
fi

# where to put the list and timestamp files.
if $etc; then
        if [[ ! -z $c ]]; then
                _echo2 "The -e option will not work with remote backups"
        fi
        ETC="$BACKUPDIR"
else
        ETC="/etc/rdup"
fi

# fix a lonely root argument
IDENT=`ident_root $(basename $1)`

if [[ ! -z $PREFIX ]]; then
        STAMP="$ETC/$PREFIX.$HOSTNAME.$IDENT.timestamp"
        LIST="$ETC/$PREFIX.$HOSTNAME.$IDENT.list"
else
        STAMP="$ETC/$HOSTNAME.$IDENT.timestamp"
        LIST="$ETC/$HOSTNAME.$IDENT.list"
fi

# if not unique add date dir
if ! $u; then
        BACKUPDIR="$BACKUPDIR/$d"
fi
# create our command line
if [[ -z $ssh ]]; then
        pipe="$pipe | /usr/sbin/rdup-mirror $OPT -b $BACKUPDIR"
else
        pipe="$pipe | $ssh rdup-mirror $c $ROPT $OPT -b $BACKUPDIR"
fi
cmd="/usr/sbin/rdup $E $x $l $c -N $STAMP $LIST $@ $pipe"

mkdir -p "$BACKUPDIR"
if [[ ! $? ]]; then
        _echo2 "Failure to create target backup directory: \`$BACKUPDIR'"
        exit 1
fi

# DIRS in $@
# add pwd if the path was relative
if [[ ${1:0:1} == "/" ]]; then
        dest="$1"
else
        dest="`pwd`/$1"
fi
# check what we should do, only for the first dir...
if ! $force; then
        if [[ ! -e "$BACKUPDIR/$dest" ]]; then
                # kill the timestamp and inc list
                rm -f "$LIST"
                rm -f "$STAMP"
                _echo2 "FULL DUMP"
        else
                _echo2 "INCREMENTAL dump"
        fi
else
        rm -f "$LIST"
        rm -f "$STAMP"
        _echo2 "FULL DUMP"

fi
# execute the backup command
#_echo2 "Executing ${cmd}"
lock $lockdir/rdup
eval ${cmd}
unlock $lockdir/rdup
exit 0
