#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Test univention-samba4-backup parameters are used in a cron job"
## exposure: safe
## packages:
##  - univention-samba4
## roles:
## - domaincontroller_master
## - domaincontroller_backup
## - domaincontroller_slave

# shellcheck source=../../lib/base.sh
. "$TESTLIBPATH/base.sh" || exit 137
# shellcheck source=../../lib/ucr.sh
. "$TESTLIBPATH/ucr.sh" || exit 137

RETVAL=100
CRON_JOB="/etc/cron.d/univention-samba4-backup"

# restore UCR settings on exit
trap "ucr_restore >/dev/null" EXIT

# skip the test if 'CRON_JOB' file does not exist:
echo "Looking for the samba4 backup cron job: $CRON_JOB"
[ -f $CRON_JOB ] || exit 137


# if UCR 'samba4/backup/cron' setting is empty the cron job should be disabled:
echo "Checking if a UCR 'samba4/backup/cron' variable is set"
if [ "$(ucr get samba4/backup/cron)" == '' ]; then
    echo "Variable is unset, cron job at $CRON_JOB should be disabled"

    if [ "$(grep -F "# disabled by" /etc/cron.d/univention-samba4-backup)" == '' ]; then
            fail_fast 110 "The cron job at $CRON_JOB is not disabled, when the UCR backup variable is unset"
    fi
fi


# force UCR vars settings for the test with options:
echo "Setting the cron variable value and options for the test"
ucr set samba4/backup/cron="0 3 * * *"
ucr set samba4/backup/cron/options="--days 88"


# check that '/etc/cron.d/univention-samba4-backup' has a line from 'samba4/backup/cron':
echo "Checking if UCR 'samba4/backup/cron' settings are present in cron job at $CRON_JOB"
if [ "$(grep -F "$(ucr get samba4/backup/cron)" $CRON_JOB)" == '' ]; then
        fail_fast 110 "The UCR 'samba4/backup/cron' setting was not found in the cron job at $CRON_JOB"
fi


# the UCR 'samba4/backup/cron/options' should also be present in cron job:
echo "Checking if UCR 'samba4/backup/cron/options' are present in the cron job at $CRON_JOB"
if [ "$(grep -F -e "$(ucr get samba4/backup/cron/options)" $CRON_JOB)" == '' ]; then
        fail_fast 110 "The UCR 'samba4/backup/cron/options' were not found in the cron job at $CRON_JOB"
fi

exit $RETVAL
