#!/bin/sh

set -e

. debian/tests/common

cleanup() {
    rm -rf /shares
    rm -f /etc/backuppc/localhost.pl
    if [ -f /etc/samba/smb.conf.dep8 ]; then
        mv -f /etc/samba/smb.conf.dep8 /etc/samba/smb.conf
    fi
}

trap cleanup EXIT

cp /etc/samba/smb.conf /etc/samba/smb.conf.dep8


# See https://bugs.launchpad.net/ubuntu/+source/iputils/+bug/2089938
# The backuppc user will eventually call ping. The user has a low uid/gid, so
# we don't need to allow a large range.
# If the command fails, ignore the error and hope for the best.
/sbin/sysctl -w net.ipv4.ping_group_range="0 1000"

sharename="public"
sharepath="/shares/${sharename}"
rm -rf "${sharepath}"

echo "Populating share path"
populate_directory "${sharepath}"

echo "Adding samba share"
add_samba_share "${sharename}" "${sharepath}" yes

echo "Configuring backuppc"
add_localhost_backuppc_config "${sharename}"

result=0
echo "Performing a full backup"
sudo -u backuppc -H /usr/share/backuppc/bin/BackupPC_dump -v -f localhost || result=$?
check_status $result "Full backup"

result=0
echo "Changing share content and performing an incremental backup"
populate_directory "${sharepath}"
sudo -u backuppc -H /usr/share/backuppc/bin/BackupPC_dump -v -i localhost || result=$?
check_status $result "Incremental backup"

echo "Done."
