#!/bin/sh

. scripts/functions

#
# Check to use multiple fileset option blocks.
#

# Define the Name of the test as "TestName".
# Should be the same as the filename, therefore we use the filename as default.
TestName="`basename $0`"

# set other test specific variables
Client=bareos-fd
JobName=backup-bareos-fd
Storage=File1
VolumeName=TestVolume001

# Directory to backup.
# This example uses a subdirectory of the bareos source/build directory,
# that contains some file but isn't to large.
BackupDirectory="${tmp}/data"

# Remove old configuration, working and tmp files. Setup the database.
cleanup

# Config files will be copied to required location.
# Base configuration files come from the
# configs/BASE/ directory, see
# configs/BASE/README.txt for an overview of the provides resources.
# Store your additonal resources to
# configs/$TestName.
# It is also possible to overwrite resources from configs/BASE.
# If you define a full config file (instead of config files for individual resources),
# only these will be taken.
copy_configs

for i in 1 2 3 4 5 6; do
    mkdir -p ${tmp}/data/data$i
    echo "test" > ${tmp}/data/data${i}/test.dat
done

# start the test
start_test

# ${tmp}/bconcmds lists the bconsole commands executed by "run_bareos"
cat <<END_OF_DATA >${tmp}/bconcmds
messages
show fileset=FS_TESTJOB
@$out ${tmp}/log1.out w
label storage=${Storage} volume=${VolumeName} pool=Default
run job=$JobName storage=${Storage} yes
wait
messages
@#
@# now do a restore
@#
@$out ${tmp}/log2.out w
restore client=${Client} where=${tmp}/bareos-restores select storage=${Storage}
unmark *
mark *
done
yes
wait
messages
quit
END_OF_DATA

# Start the bareos daemons
# and run the bconsole commands from ${tmp}/bconcmds
# Further bconsole commands can be executed by "run_bconsole".
run_bareos

# verify that all are terminated
check_for_zombie_jobs storage=File1 client=${Client}

# stop bareos
stop_bareos

# check tmp/log1.out and tmp/log2.out for errors
check_two_logs

# check for differences between original files and restored files
# gives an error, because top-level data directory is not backed up
# (and has therefore other permissions)
check_restore_diff ${BackupDirectory}/data1
check_restore_diff ${BackupDirectory}/data2
check_restore_diff ${BackupDirectory}/data3
check_restore_diff ${BackupDirectory}/data4
#check_restore_diff ${BackupDirectory}/data5
if [ -e ${tmp}/bareos-restores/${BackupDirectory}/data5 ]; then
    set_error "Directory data5 has been restored, however it should be excluded from the backup."
fi
#check_restore_diff ${BackupDirectory}/data6
if [ -e ${tmp}/bareos-restores/${BackupDirectory}/data6 ]; then
    set_error "Directory data6 has been restored, however it should be excluded from the backup."
fi

# do some manual testing
if ! [ -d ${BackupDirectory} ]; then
    set_error "Directory ${BackupDirectory} does not exists any more."
fi

check_compression "${Storage}" "${VolumeName}" "1" "${tmp}/data/data2/test.dat" "gzip"
check_compression "${Storage}" "${VolumeName}" "1" "${tmp}/data/data3/test.dat" "gzip" "9"
# Warning: LZ4 compression support requested in fileset but not available on this platform. Disabling ...
#check_compression "${Storage}" "${VolumeName}" "1" "${tmp}/data/data3/test.dat" "LZ4"

# end tests and check for error codes
end_test
