#!/bin/bash
set -e
set -o pipefail
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



# Fill ${BackupDirectory} with 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
label volume=TestVolume002 storage=File pool=Full
label volume=TestVolume003 storage=File pool=Full
label volume=TestVolume004 storage=File pool=Full
label volume=TestVolumeIncremental storage=File pool=Incremental
status director
status client
status storage=File
wait
messages

run job=$JobName Level=Full yes
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
update volume=TestVolume001 volstatus=Used
prune volume=TestVolume001 yes
wait
messages

@$out $tmp/log3.out
@#
@# now do the same but using the prune all command
@#
@#

run job=$JobName Level=Full yes
run job=$JobName Level=Full yes
wait
update volume=TestVolume002 volstatus=Used

run job=$JobName Level=Full yes
run job=$JobName Level=Full yes
wait
update volume=TestVolume003 volstatus=Used

@sleep 6

prune volume all yes
wait
messages


run job=$JobName Level=Full yes
wait

quit
END_OF_DATA

run_bareos "$@"

touch $tmp/data/weird-files/newfilecreatedjustforthistest

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out $tmp/log4.out
@#
@# Prune all volumes of a given pool
@#
@#

run job=$JobName Level=Incremental yes
wait

@sleep 6

update volume=TestVolume004 volstatus=Used
update volume=TestVolumeIncremental volstatus=Used

prune volume all pool=Incremental yes

wait
messages
quit
END_OF_DATA

run_bconsole

# Pruning jobs

job_pruning_log="$tmp"/job_pruning_log.out

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out $job_pruning_log
run job=$JobName Level=Full yes
run job=$JobName Level=Full yes
wait
@sleep 2
prune jobs yes
wait
messages
quit
END_OF_DATA

run_bconsole

# Pruning files

file_pruning_log="$tmp"/file_pruning_log.out

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out $file_pruning_log
prune files yes
wait
restore jobid=11
no

messages
quit
END_OF_DATA

run_bconsole

check_for_zombie_jobs storage=File

check_two_logs

if ! grep "Purging the following 2 JobIds: 1,2" "$tmp"/log2.out; then
  echo "'Purging the following JobIds: 1,2' not found in $tmp/log2.out" >&2
  grep 'Purging the' "$tmp"/log2.out >&2 || :
  estat=1
fi

if ! grep -F 'Volume "TestVolume001" contains no jobs after pruning.' $tmp/log2.out; then
  echo "'Volume \"TestVolume001\" contains no jobs after pruning.' not found in $tmp/log2.out" >&2
  grep 'Volume.*contains no jobs after pruning.' $tmp/log2.out >&2 || :
  estat=1
fi

## prune all volumes checks

if ! grep "Purging the following 2 JobIds: 4,5" "$tmp"/log3.out; then
  echo "'Purging the following JobIds: 4,5' not found in $tmp/log3.out" >&2
  grep 'Purging the' "$tmp"/log3.out >&2 || :
  estat=1
fi

if ! grep -F 'Volume "TestVolume002" contains no jobs after pruning.' $tmp/log3.out; then
  echo "'Volume \"TestVolume002\" contains no jobs after pruning.' not found in $tmp/log3.out" >&2
  grep 'Volume.*contains no jobs after pruning.' $tmp/log3.out >&2 || :
  estat=1
fi

if ! grep "Purging the following 2 JobIds: 6,7" "$tmp"/log3.out; then
  echo "'Purging the following JobIds: 6,7' not found in $tmp/log3.out" >&2
  grep 'Purging the' "$tmp"/log3.out >&2 || :
  estat=1
fi

if ! grep -F 'Volume "TestVolume003" contains no jobs after pruning.' $tmp/log3.out; then
  echo "'Volume \"TestVolume003\" contains no jobs after pruning.' not found in $tmp/log3.out" >&2
  grep 'Volume.*contains no jobs after pruning.' $tmp/log3.out >&2 || :
  estat=1
fi

# Prune all volumes of specific pool

if ! grep "Purging the following 1 JobIds: 9" "$tmp"/log4.out; then
  echo "'Purging the following JobIds: 9' not found in $tmp/log4.out" >&2
  grep 'Purging the' "$tmp"/log3.out >&2 || :
  estat=1
fi

if ! grep -F 'Volume "TestVolumeIncremental" contains no jobs after pruning.' $tmp/log4.out; then
  echo "'Volume \"TestVolumeIncremental\" contains no jobs after pruning.' not found in $tmp/log4.out" >&2
  grep 'Volume.*contains no jobs after pruning.' $tmp/log3.out >&2 || :
  estat=1
fi

#job pruning

expect_grep "Purging the following 3 JobIds: 3,8,10" \
            "$job_pruning_log" \
            "job pruning went wrong"

expect_grep "Pruned 3 Jobs for client bareos-fd from catalog." \
            "$job_pruning_log" \
            "job pruning went wrong"

#file pruning

expect_grep "Pruned Files from 1 Jobs" \
            "$file_pruning_log" \
            "file pruning went wrong"

expect_grep "Most likely your retention policy pruned the files." \
            "$file_pruning_log" \
            "file pruning went wrong"

end_test
