#! /bin/sh

export LANG=""

DUPLICITY=/usr/bin/duplicity
SELECT="$SELECT --exclude-if-present .nobackup"
OPTIONS="$OPTIONS --asynchronous-upload"
ENCRYPT_KEY=
SRC=
DEST=
FTP_PASSWORD=
TMPDIR=
MODE=
CLEAN=

for conf in ./easydup.conf ~/.easydup.conf /etc/easydup.conf; do
		  if [ -r $conf ]; then
					 . $conf
					 break
		  fi
done

export TMPDIR
export FTP_PASSWORD

HOSTNAME=$(hostname)

if [ -z "$SRC" ]; then
		  echo Error '$SRC' cannot be an empty string
		  exit 1
fi

if [ -z "$DEST" ]; then
		  echo Error '$DEST' cannot be an empty string
		  exit 1
fi

src=$SRC/
name=$(echo $SRC | sed s!/!! | sed s!/!_!g)

if [ -n "$ENCRYPT_KEY" ]; then
		  encrypt="--encrypt-key $ENCRYPT_KEY"
else
		  encrypt="--no-encryption"
fi

dest="$DEST/$HOSTNAME/$name"

if [ "$MODE" = "clean" ]; then
    if [ -z "$CLEAN" ]; then
        echo Error '$CLEAN' cannot be empty when using the clean action
        exit 1
    else
        echo \
        $DUPLICITY $CLEAN --force $OPTIONS $encrypt --name $name $dest
        $DUPLICITY $CLEAN --force $OPTIONS $encrypt --name $name $dest
    fi
elif [ "$MODE" = "status" ]; then
    echo \
    $DUPLICITY collection-status $OPTIONS $encrypt --name $name $dest
    $DUPLICITY collection-status $OPTIONS $encrypt --name $name $dest
else
    echo \
    $DUPLICITY $MODE $OPTIONS $volsize --name $name $SELECT $encrypt $src $dest
    $DUPLICITY $MODE $OPTIONS $volsize --name $name $SELECT $encrypt $src $dest
fi

