#!/bin/bash
set -e
set -o pipefail
set -u
#
# This systemtest tests the plugin functionality
# of the Bareos FD by using the supplied module
#   bareos-fd-local-fileset.py
#
# The module will backup some files.
# This plugin is not intended for production,
# but is only a minimal example that shows
# how to use the python plugin interface.
# File attributes like uses and times will not be saved.
#
TestName="$(basename "$(pwd)")"
export TestName

JobName=backup-bareos-fd

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

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

# Fill ${BackupDirectory} with data.
setup_data

# this test does not work with links, because
# python 2 does not support setting mtime on links
# they are restored properly but with wrong timestamps
# This is supposed to work with the future plugin-fd interface
# using Python3
find ${tmp}/data/weird-files -type l -exec rm {} \;
find tmp/data/weird-files -links +1 -type f -exec rm {} \;

grep py3 <<< "$TestName" && rm tmp/data/weird-files/filename-with-non-utf8-bytestring*

print_debug "$(locale)"

start_test

cat <<END_OF_DATA >$tmp/bconcmds
@$out /dev/null
messages
@$out $tmp/log1.out
setdebug level=800 client trace=1
label volume=TestVolume001 storage=File pool=Full
run job=$JobName yes
status director
status client
status storage=File
wait
messages
END_OF_DATA

run_bareos "$@"
if is_debug; then
    cat ${working_dir}/*.trace
fi
check_for_zombie_jobs storage=File
stop_bareos

cat <<END_OF_DATA >$tmp/bconcmds
@#
@# now do a restore
@#
@$out $tmp/log2.out
restore client=bareos-fd fileset=PluginTest where=$tmp/bareos-restores select all done yes
status client=bareos-fd
wait
messages
END_OF_DATA

run_bareos "$@"
check_for_zombie_jobs storage=File

check_two_logs
check_restore_diff ${BackupDirectory}

expect_grep 'name="XATTR name 3", value="XATTR value 3"' tmp/log2.out "could not find recovered XATTR in restore log"
expect_grep 'content="Hello ACL"' tmp/log2.out "could not find recovered ACL in restore log"

expect_grep "Readin.*from file"\
  working/py?plug-fd-local-fileset-basic-fd.trace \
  "io was not handled in python according to trace file."

end_test
bin/bareos stop
