#!/usr/bin/env bash
#
# Copyright (C) 2000-2021 Kern Sibbald
# Copyright (C) 2021-2026 Bacula Systems SA
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of the Bacula build directory
#   and try to purge/delete it
#

# TODO: check in the following cases
# purge jobs
# autoprune
# prune jobs
# prune client
# purge volume
# delete jobid
# delete volume
# migration purgemigratejob
# virtualfull backuptokeep

TestName="job-prune-test"
JobName=backup
. scripts/functions

scripts/cleanup
scripts/copy-confs

#
# Zap out any schedule in default conf file so that
#  it doesn't start during our test
#
outf="$tmp/sed_tmp"
echo "s%  Schedule =%# Schedule =%g" >${outf}
cp $scripts/bacula-dir.conf $tmp/1
sed -f ${outf} $tmp/1 >$scripts/bacula-dir.conf

$bperl -e "set_global_maximum_concurrent_jobs(10)"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'Autoprune', 'no', 'Director')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'JobRetention', '5s', 'Client')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'FileRetention', '5s', 'Client')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'FileRetention', '5s', 'Pool')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'JobRetention', '5s', 'Pool')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'VolumeRetention', '5s', 'Pool')"
change_jobname BackupClient1 $JobName
start_test

cat <<END_OF_DATA >$tmp/bconcmds
@output /dev/null
messages
@$out $tmp/log1.out
setdebug level=4 storage=File1
label volume=TestVolume001 storage=File1 pool=File slot=1 drive=0
run job=$JobName level=Full yes
run job=$JobName level=Full yes
run job=$JobName level=Full yes
run job=$JobName level=Full yes
wait             
messages
@# Set the maximum protection, no file pruning, no job pruning
update jobid=1 prunefiles=never
update jobid=2 prunefiles=never
update jobid=3 prunefiles=never
update jobid=4 prunejobs=never
END_OF_DATA

run_bacula
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'Autoprune', 'yes', 'Director')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'PruneFiles', 'never', 'Job', 'backup')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'PruneJobs', 'never', 'Job', 'BackupCatalog')"

cat <<END_OF_DATA >$tmp/bconcmds
@$out $tmp/log1.out
reload
@sleep 5
prune files client yes
@#setdebug level=50 tags=sql dir
prune jobs yes
.jlist jobs
purge volume=TestVolume001 yes
.jlist jobs
delete jobid=1
@#setdebug level=0 tags= dir
.jlist jobs
status dir
.jlist jobs
@$out $tmp/log2.out
.jlist media pool=File
@$out $tmp/log3.out
update jobid=1 prunejobs=yes
purge volume=TestVolume001 yes
.jlist jobs
@$out $tmp/log4.out
.jlist media pool=File
update jobid=2 prunejobs=yes
delete jobid=2
update jobid=3 prunejobs=yes
delete jobid=3
update jobid=4 prunejobs=yes
purge volume=TestVolume001 yes
@$out $tmp/log5.out
.jlist media pool=File
@$out $tmp/log6.out
run job=$JobName level=Full yes
wait
messages
.jlist jobid=5
@$out $tmp/log7.out
run job=BackupCatalog level=Full yes
wait
messages
.jlist jobid=6
@$out $tmp/log8.out
update jobid=5 prunejobs=yes
update jobid=6 prunejobs=yes
purge volume=TestVolume001
.jlist media pool=File
quit
END_OF_DATA

run_bconsole

$bperl -e "add_attribute('$conf/bacula-dir.conf', 'PruneJobs', 'yes', 'Job', 'BackupCatalog')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'PruneFiles', 'yes', 'Job', 'backup')"

cat <<END_OF_DATA >$tmp/bconcmds
@$out $tmp/log100.out
reload
run job=BackupCatalog level=full yes
wait
.jlist jobid=7
@$out $tmp/log101.out
run job=backup level=full yes
wait
.jlist jobid=8
quit
END_OF_DATA

run_bconsole
stop_bacula

grep '{' $tmp/log1.out | perl -n -MJSON -e '$j = decode_json(<>); exit 1 if (scalar(@{$j->{data}}) != 4);'

if test $? != 0; then
    print_debug "ERROR: JobId 1 not found in .jlist output in $tmp/log1.out"
    estat=1
fi

grep '{' $tmp/log2.out | perl -MJSON -e '$j = decode_json(<>); exit 1 if (!scalar(@{$j->{data}}) || $j->{data}->[0]->{volstatus} ne "Append");'

if test $? != 0
then
    print_debug "ERROR: Volume not in Append in $tmp/log2.out"
    estat=1
fi

grep '{' $tmp/log3.out | perl -MJSON -e '$j = decode_json(<>); exit 1 if (scalar(@{$j->{data}}) != 3);'

if test $? != 0;
then
    print_debug "ERROR: JobId 1 found in .jlist output in $tmp/log3.out"
    estat=1
fi

grep '{' $tmp/log4.out | perl -MJSON -e '$j = decode_json(<>); exit 1 if (!scalar(@{$j->{data}}) || $j->{data}->[0]->{volstatus} ne "Append");'

if test $? != 0
then
    print_debug "ERROR: Volume not in Append in $tmp/log4.out"
    estat=1
fi

grep '{' $tmp/log5.out | perl -MJSON -e '$j = decode_json(<>); exit 1 if (!scalar(@{$j->{data}}) || $j->{data}->[0]->{volstatus} ne "Purged");'

if test $? != 0
then
    print_debug "ERROR: Volume not in Purged in $tmp/log5.out"
    estat=1
fi            

grep '{' $tmp/log6.out | perl -MJSON -e '$j = decode_json(<>); exit 1 if (!scalar(@{$j->{data}}) || $j->{data}->[0]->{prune} ne "2");'

if test $? != 0
then
    print_debug "ERROR: Job not in Prune=2 in $tmp/log6.out"
    estat=1
fi            

grep '{' $tmp/log7.out | perl -MJSON -e '$j = decode_json(<>); exit 1 if (!scalar(@{$j->{data}}) || $j->{data}->[0]->{prune} ne "1");'

if test $? != 0
then
    print_debug "ERROR: Job not in Prune=1 in $tmp/log7.out"
    estat=1
fi            

grep '{' $tmp/log8.out | perl -MJSON -e '$j = decode_json(<>); exit 1 if (!scalar(@{$j->{data}}) || $j->{data}->[0]->{volstatus} ne "Purged");'

if test $? != 0
then
    print_debug "ERROR: Volume not in Purged in $tmp/log8.out"
    estat=1
fi            

grep '{' $tmp/log100.out | perl -MJSON -e '$j = decode_json(<>); exit 1 if (!scalar(@{$j->{data}}) || $j->{data}->[0]->{prune} ne "0");'
 
if test $? != 0; then
    print_debug "ERROR: JobId 8 has invalid prune status in .jlist output in $tmp/log100.out"
    estat=1
fi

grep '{' $tmp/log101.out | perl -MJSON -e '$j = decode_json(<>); exit 1 if (!scalar(@{$j->{data}}) || $j->{data}->[0]->{prune} ne "0");'

if test $? != 0; then
    print_debug "ERROR: JobId 9 has invalid prune status in .jlist output in $tmp/log101.out"
    estat=1
fi

end_test
