#!/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
. "${BAREOS_SCRIPTS_DIR}"/functions
"${BAREOS_SCRIPTS_DIR}"/cleanup
"${BAREOS_SCRIPTS_DIR}"/setup

# Fill ${BackupDirectory} with data.
setup_data

start_test

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out ${NULL_DEV}
messages
@$out $tmp/log1.out
label volume=TestVolume001 storage=File pool=Full
run job=$JobName yes
run job=$JobName yes
status director
status client
status storage=File
wait
messages
update jobid=2 jobtype=archive
@#
@# now do a restore
@#
@$out $tmp/list-backups.out
list backups client=bareos-fd
@$out $tmp/llist-backups.out
llist backups client=bareos-fd
@$out $tmp/comments.out
update jobid=1 comment="job comment it's quoted"
update volume=TestVolume001 comment="volume comment"
llist jobid=1
llist volume=TestVolume001
@$out $tmp/comments-cleared.out
update jobid=1 comment=""
update volume=TestVolume001 comment=""
llist jobid=1
llist volume=TestVolume001
quit
END_OF_DATA

run_bareos "$@"
check_for_zombie_jobs storage=File

echo "Checking list backups for jobtype=backup"
grep -F '1 | backup-bareos-fd | bareos-fd' "$tmp/list-backups.out"

echo "Checking llist backups for jobtype=backup"
grep -F 'jobid: 1' "$tmp/llist-backups.out"

echo "Checking list backups for jobtype=archive"
grep -F '2 | backup-bareos-fd | bareos-fd' "$tmp/list-backups.out"

echo "Checking llist backups for jobtype=archive"
grep -F 'jobid: 2' "$tmp/llist-backups.out"

echo "Checking job and volume comments"
grep -F "comment: job comment it's quoted" "$tmp/comments.out"
grep -F 'comment: volume comment' "$tmp/comments.out"
if grep -E 'comment: .+' "$tmp/comments-cleared.out"; then
  echo "comments were not cleared"
  estat=1
fi
end_test
