#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Test univention-samba4-backup"
## exposure: dangerous
## packages:
##  - univention-samba4
## roles:
## - domaincontroller_master
## - domaincontroller_backup
## - domaincontroller_slave

# shellcheck source=/dev/null
. "$TESTLIBPATH/base.sh" || exit 137

backup_dir="/var/univention-backup/samba"
domainname="$(ucr get domainname)"
domainname="${domainname,,}"
samba_dir="/var/lib/samba/sysvol/$domainname"

cleanup () {
	test -L "$samba_dir/scripts/fstab" && rm -f "$samba_dir/scripts/fstab"
}
trap cleanup INT TERM EXIT

# backup
number_of_backup_files_before="$(find "$backup_dir"/samba-backup-*.tar.bz2 | wc -l)"
univention-samba4-backup || fail_test "univention-samba4-backup failed"
number_of_backup_files_after="$(find "$backup_dir"/samba-backup-*.tar.bz2 | wc -l)"
test "$number_of_backup_files_after" -gt "$number_of_backup_files_before" ||  fail_test "no backup archive found after running univention-samba4-backup"

# check backup if symlink exists
ln -s /etc/fstab "$samba_dir/scripts/" || fail_test "could not create link"
univention-samba4-backup || fail_test "univention-samba4-backup failed with symlink in $samba_dir"


exit "$RETVAL"
