#!/bin/bash
set -e
set -o pipefail
set -u

#shellcheck source=../../environment.in
. ./environment

#shellcheck source=../../scripts/functions
. "${rscripts}"/functions

#
# Check backups with different compression options.
#

TestName="$(get_test_name "$0")"
export TestName

# set other test specific variables
Client=bareos-fd
JobName=backup-multi-includes
Fileset=fileset-multi-includes
Storage=File1
VolumeName=Full-0001

export BackupDirectory="${tmp}/data-multi-includes"
BackupFileExtra=${BackupDirectory}/extrafile.txt

start_test

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

# ${tmp}/bconcmds lists the bconsole commands executed by "run_bareos"
cat <<END_OF_DATA >${tmp}/bconcmds
messages
show fileset=${Fileset}
@$out ${tmp}/log1.out w
run job=$JobName level=Full yes
wait
messages
@#
@# now do a restore
@#
@$out ${tmp}/log2.out w
restore client=${Client} fileset=${Fileset} where=${tmp}/bareos-restores select
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_bconsole

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

# 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

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

check_compression "${Storage}" "${VolumeName}" "1" "${BackupDirectory}/data2/test.dat" "gzip"
check_compression "${Storage}" "${VolumeName}" "1" "${BackupDirectory}/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
