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

TestName="prune-many-jobs-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", "Client")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Job Retention", "1s", "Client")'
$bperl -e 'set_global_maximum_concurrent_jobs(100)'

cat <<EOF > ${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out $tmp/log1.out
label storage=File volume=TestVolume001
run job=$JobName level=full yes
wait
messages
@exec "touch $build/po/fr.po"
EOF

for i in `seq 1 3000`; do
    echo "run job=$JobName yes" >> $tmp/bconcmds
done

cat <<EOF >> $tmp/bconcmds
wait
messages
quit
EOF

run_bacula

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

cat <<EOF > $tmp/bconcmds
@$out $tmp/log1.out
reload
run job=$JobName level=full yes
wait
messages
@$out $tmp/log2.out
restore select all done yes where=$tmp/bacula-restores
wait
messages
quit
EOF

run_bconsole


check_for_zombie_jobs storage=File
check_two_logs
stop_bacula

grep "Pruned 3001 Jobs for client" $tmp/log1.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: Should find a large pruning list in $tmp/log1.out"
    estat=1
fi

end_test
