#!/bin/bash
test -x /usr/bin/rdiff-backup || exit 0

# source the conf file
. /etc/backpack.conf

if [[ $enabled != 1 ]]; then
        exit 0
fi

# check if our destination is a local mount point
if [[ $destination =~ /mnt/* ]]; then
	# The mount point may have become unmounted (eg on reboot).
	# I will assume the mount point is the first folder under /mnt.
	# use cut to slice the first two folders off the destination path.
	mount_point="`cut -d '/' -f 1,2,3 <<< "$destination"`"

	# Check if the directory is in our mounts list.
	if [ "`mount | grep $mount_point`" == "" ]; then
		# It does not seem to be mounted. Try to auto-mount from fstab
		mount -a
		# Check again - is it mounted now?
		if [ "`mount | grep $mount_point`" == "" ]; then
			# Still not mounted, so abort
			exit 0
		fi
	fi
fi

# make the destination folder if it doesn't exist
mkdir -p $destination

# swap semicolons in the source string for the --include directive
includes="`sed 's/;/ --include /g' <<< ";$source"`"

# run our rdiff-backup command
rdiff-backup $includes --exclude '**' / $destination
