#!/bin/bash
set -e
set -o pipefail
set -u
#
# Run a simple backup
#   then restore it.
#
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

# clean out logfile
rm -f log/bareos.log

start_test

start_bareos

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out ${NULL_DEV}
messages
@$out $tmp/log1.out
setdebug level=100 storage=File
label volume=TestVolume001 storage=File pool=Full
label volume=TestVolume002 storage=File pool=Incremental
label volume=TestVolume003 storage=File pool=VirtualFull
run job=$JobName level=Full yes
wait
messages
list jobs
@exec "sh -c 'touch ${tmp}/data/*.c'"
run job=$JobName level=Incremental yes
wait
messages
list jobs
run job=$JobName level=VirtualFull yes
wait
messages
list jobs
@# make sure we really restore the virtualfull
purge volume=TestVolume001 yes
purge volume=TestVolume002 yes
status director
status client
status storage=File
quit
END_OF_DATA

run_bconsole "$tmp/bconcmds"

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

run_bconsole "$tmp/bconcmds2"

check_for_zombie_jobs storage=File
check_restore_diff "${BackupDirectory}"

cat <<END_OF_DATA >"$tmp/bconcmds3"
@$out ${NULL_DEV}
messages
@$out $tmp/log3.out
run job=$JobName level=Full yes
wait
messages
list jobs
@exec "sh -c 'touch ${tmp}/data/*.c'"
run job=$JobName level=Incremental yes
wait
run job=$JobName level=Incremental yes
wait
run job=$JobName level=Incremental yes
wait
messages
purge files jobid=5 yes
list jobs
@$out $tmp/log4.out
run job=$JobName jobid=7,8 level=VirtualFull yes
wait
messages
@$out $tmp/log5.out
run job=$JobName jobid=3,2,6 level=VirtualFull yes
wait
messages
@$out $tmp/log6.out
run job=$JobName jobid=3,5,6 level=VirtualFull yes
wait
messages
list jobs
quit
END_OF_DATA

run_bconsole "$tmp/bconcmds3"
check_for_zombie_jobs storage=File
check_two_logs

if [[ "${HAVE_WIN32}" = "1" ]]; then
  expected_file_count=".."
else
  expected_file_count=87
fi

if ! grep -q "Consolidating JobIds 1,2 containing ${expected_file_count} files" "$tmp/log1.out"; then
  echo "Consolidation message was not emitted" >&2
  cat "$tmp/log1.out"
  estat=1
fi

if ! grep -q "Fatal error: Could not create bootstrap file" "$tmp/log4.out"; then
  echo "Job with no files contained did not fail as expected" >&2
  cat "$tmp/log4.out"
  estat=1
fi

if ! grep -q "JobId 2 not found in catalog." "$tmp/log5.out"; then
  echo "Consolidating missing jobids did not fail as expected" >&2
  cat "$tmp/log5.out"
  estat=1
fi

if ! grep -q "Termination:.*Backup Error" "$tmp/log6.out"; then
  echo "Consolidating jobs with pruned files did not fail as expected" >&2
  cat "$tmp/log6.out"
  estat=1
fi

end_test
