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

TestName="prune-test"
JobName=NightlySave

scripts/cleanup
scripts/copy-test-confs


# 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")'

# Add a new job with a different fileset
$bperl -e 'print get_resource("$conf/bareos-dir.conf", "Job", "NightlySave")' | \
    sed s/NightlySave/DailySave/ | sed 's/Full Set/CompressedSet/' > $tmp/1

# 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 data/small.tgz

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


start_test

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/fr.po"
run job=$JobName level=incremental yes
wait
messages
@exec "touch $BackupDirectory/de.po"
run job=$JobName level=incremental yes
wait
messages
@# run a second set of backup
run job=DailySave level=full yes
wait
messages
@exec "touch $BackupDirectory/fr.po"
run job=DailySave level=incremental yes
wait
messages
@exec "touch $BackupDirectory/de.po"
run job=DailySave level=incremental yes
wait
messages
@sleep 2
@$out $tmp/log10.out
@# should prune nothing
list jobs
prune jobs jobtype=backup yes
list jobs
quit
EOF

run_bareos

# should find nothing to prune
$bperl -e "check_prune_list('$tmp/log10.out')"

# rename the job to see if it's pruned
sed s/DailySave/DisabledBackup/ $conf/bareos-dir.conf > $tmp/1
mv $tmp/1 $conf/bareos-dir.conf

cat <<EOF > ${cwd}/tmp/bconcmds
@$out tmp/log11.out
reload
messages
@# should prune jobs using DailySave
list jobs
prune jobs jobtype=backup yes
list jobs
EOF

run_bconsole

$bperl -e "check_prune_list('$tmp/log11.out', 4, 5, 6)"
estat=$(($estat + $?))

# rename Full Set fileset to see if it will be pruned the next time
$bperl -e 'add_attribute("$conf/bareos-dir.conf", "FileSet", "CompressedSet", "Job", "NightlySave")'
sed "s/Full Set/DisFullSet/g" $conf/bareos-dir.conf > $tmp/1
mv $tmp/1 $conf/bareos-dir.conf

cat <<EOF > ${cwd}/tmp/bconcmds
@$out tmp/log12.out
reload
messages
@# should prune jobs using Full Set
list jobs
prune jobs jobtype=backup yes
list jobs
EOF

run_bconsole

$bperl -e "check_prune_list('$tmp/log12.out', 1, 2, 3)"
estat=$(($estat + $?))

check_for_zombie_jobs storage=File
stop_bareos

touch $tmp/log2.out
check_two_logs

end_test
