#!/bin/sh
#
# This script will test prune algo
#
. scripts/functions

TestName="prune-test"
JobName=NightlySave

scripts/cleanup
scripts/copy-test-confs

# 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/".
# po.tgz: build/po/*
setup_data data/po.tgz
echo "test" > ${BackupDirectory}/build/po/test1.txt

# the default fileset FS_TESTJOB backups all file and directories defined in "${tmp}/file-list".
echo "${BackupDirectory}" >${tmp}/file-list


start_test


# We want to have pruning only when doing prune command
$bperl -e 'add_attribute("$conf/bareos-dir.conf", "AutoPrune", "No", "Client")'
$bperl -e 'add_attribute("$conf/bareos-dir.conf", "Job Retention", "1s", "Client")'

cat <<EOF > ${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out $tmp/log1.out
label storage=File volume=TestVolume001
run job=$JobName level=full yes
wait
messages
@exec "touch ${BackupDirectory}/build/po/fr.po"
run job=$JobName level=incremental yes
wait
messages
@exec "touch ${BackupDirectory}/build/po/de.po"
run job=$JobName level=incremental yes
wait
messages
@sleep 2
@$out $tmp/log3.out
@################################################################
@# Should not prune anything
list jobs
prune jobs jobtype=backup yes
list jobs
@$out $tmp/log4.out
@################################################################
@# Should prune the two firsts incrementals
run job=$JobName level=differential yes
wait
messages
@exec "touch ${BackupDirectory}/build/po/es.po"
run job=$JobName level=incremental yes
wait
messages
@sleep 2
list jobs
prune jobs jobtype=backup yes
list jobs
@$out $tmp/log5.out
@################################################################
@# Should prune the first Full+Diff job
run job=$JobName level=full yes
wait
messages
@sleep 2
list jobs
prune jobs jobtype=backup yes
list jobs
@$out $tmp/log6.out
@################################################################
run job=$JobName level=incremental yes
wait
messages
@$out $tmp/log2.out
restore where=${cwd}/tmp/bareos-restores select all storage=File done
yes
wait
messages
@sleep 2
@$out $tmp/log6.out
@################################################################
@# Should prune the restore job and the last incremental job (jobfiles=0)
list jobs
prune jobs jobtype=backup yes
list jobs
quit
EOF

run_bareos

check_for_zombie_jobs storage=File
stop_bareos

check_two_logs

estat=0

###############################################################################
# Now we will verify that the pruning code is working as expected.  Each time,
# we run 'list jobs', 'prune', 'list jobs'. check_prune_list ensures that jobids
# in argument are present in the first 'list jobs', the 'prune' command deletes
# them (same number), and checks that the last 'list jobs' doesn't contain them
# anymore. See scripts/functions.pm for details.

# nothing should be pruned
$bperl -e "check_prune_list('$tmp/log3.out')"
estat=$(($estat + $?))

# jobids 2 and 3 should be pruned
# (F I I D) -> (F D)
$bperl -e "check_prune_list('$tmp/log4.out',2,3)"
estat=$(($estat + $?))

# (F D I F) -> (F)
$bperl -e "check_prune_list('$tmp/log5.out',1,4,5)"
estat=$(($estat + $?))

# (F I R) -> (F)     -- Incremental have 0 file
$bperl -e "check_prune_list('$tmp/log6.out',7,8)"
estat=$(($estat + $?))

end_test
