#!/bin/sh
#
# Copyright (C) 2000-2026 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# This script will test prune algo 
#
. scripts/functions

TestName="director-autoprune-test"
JobName=NightlySave

scripts/cleanup
scripts/copy-test-confs

setup_shstore # simulate shared storage if enabled

start_test

echo $PWD/build/po > tmp/file-list
# We want to have pruning only when doing prune command
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "AutoPrune", "No", "Director")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Job Retention", "1s", "Client")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "File Retention", "1s", "Client")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Schedule", "WeeklyCycle", "Job")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "MaximumVolumeJobs", "1", "Pool")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "LabelFormat", "Vol", "Pool")'

cat <<EOF > ${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out $tmp/log1.out
run job=$JobName level=full yes
wait
messages
@exec "touch $cwd/build/po/fr.po"
run job=$JobName level=incremental yes
wait
messages
@exec "touch $cwd/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 yes
list jobs
@$out $tmp/log4.out
@################################################################
@# Should prune the two firsts incrementals
run job=$JobName level=differential yes
wait
messages
@exec "touch $cwd/build/po/es.po"
run job=$JobName level=incremental yes
wait
messages
status dir
@sleep 2
list jobs
prune jobs 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 yes
list jobs
@$out $tmp/log6.out
@################################################################
run job=$JobName level=incremental yes
wait
messages
@$out $tmp/log2.out
restore where=${cwd}/tmp/bacula-restores select all storage=File done
yes
wait
messages
@sleep 2
@$out $tmp/log6.out
@################################################################
@# Should not prune anything
list jobs
status dir
list jobs
@$out $tmp/log7.out
@################################################################
@# Should prune the restore job and the last incremental job (jobfiles=0)
list jobs
prune jobs yes
list jobs
quit
quit
EOF

run_bacula

$bperl -e 'add_attribute("$conf/bacula-dir.conf", "AutoPrune", "Yes", "Director")'

cat <<EOF > ${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out $tmp/alog7.out
reload
run job=$JobName level=incremental yes
wait
messages
EOF

run_bconsole

stop_bacula

check_two_logs

estat=0

grep -i 'auto prune' $tmp/log*.out > /dev/null
if [ $? = 0 ]; then
    print_debug "ERROR: Should not find Autoprune messages in job log"
    estat=1
fi

grep -i 'auto prune' $tmp/alog7.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Should find Autoprune messages in job log $tmp/alog7.log"
    estat=1
fi

###############################################################################
# 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 I R)     -- Incremental have 0 file
$bperl -e "check_prune_list('$tmp/log6.out')"
estat=$(($estat + $?))

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

$rscripts/diff.pl -s $cwd/build/po -d $tmp/bacula-restores/$cwd/build/po
rstat=$(($rstat + $?))

end_test
