#!/bin/bash
set -e
set -u
#
# run multiple jobs and verify that the pruning mechanism
# logs the jobs being pruned
#
#
TestName="$(basename "$(pwd)")"
export TestName

JobName=backup-bareos-fd

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

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


# Directory to backup.
# This directory will be created by setup_data "$@"().
BackupDirectory="${tmp}/data"

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data "$@"

start_test

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

run job=$JobName Level=Full yes
run job=$JobName Level=Full yes
run job=$JobName Level=Full yes
run job=$JobName Level=Full yes
run job=$JobName Level=Full yes
wait
@sleep 10
run job=$JobName Level=Full yes

@#
@# now do a restore
@#
wait
restore client=bareos-fd fileset=SelfTest where=$tmp/bareos-restores select all done
yes
wait
messages

@$out $tmp/log2.out
@#
@# now prune using the commandline
@#
@sleep 6
prune volume=TestVolume001 yes
messages
quit
END_OF_DATA

run_bareos "$@"
check_for_zombie_jobs storage=File
stop_bareos

check_two_logs

if ! grep "Purging the following JobIds: 1,2,3,4,5,6" "$tmp"/log1.out; then
  echo "Pruned jobs don't match expectations." >&2
  estat=1
fi

if ! grep -F 'Volume "TestVolume001" contains no jobs after pruning.' \
    $tmp/log2.out; then
  echo "Pruning message is wrong." >&2
  estat=1
fi

end_test
