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

TestName="delete-volume-test"
JobName=NightlySave

scripts/cleanup
scripts/copy-test-confs

start_test

echo $PWD/build/po > tmp/file-list

$bperl -e "add_attribute('$conf/bacula-dir.conf', 'MaximumVolumeJobs', '10', 'Pool')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'LabelFormat', 'Vol', 'Pool')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'SpoolData', 'no', 'Job')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'Autoprune', 'no', 'Director')"

cat <<EOF >> $conf/bacula-dir.conf
Pool {
 Name = Test1
 Pool Type = Backup
 LabelFormat = Vol
 VolumeRetention = 1d
 MaximumVolumeJobs = 10
 MaximumVolumeBytes = 1MB
}
EOF

cat <<EOF > ${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out $tmp/log1.out
run job=$JobName level=full pool=Test1 yes
wait
messages
@output $tmp/1.out
.jlist pool=Test1
@output $tmp/2.out
.jlist volumes pool=Test1
@$out $tmp/log1.out
delete volume=Vol0001 yes
@output $tmp/3.out
.jlist pool=Test1
quit
EOF

run_bacula
stop_bacula

grep '{' $tmp/1.out | perl -MJSON -e '$obj = decode_json(<>); exit $obj->{data}->[0]->{numvols};'
nb1=$?

grep '{' $tmp/3.out | perl -MJSON -e '$obj = decode_json(<>); exit $obj->{data}->[0]->{numvols};'
nb2=$?

nb3=`expr $nb2 + 1`

if [ $nb3 != $nb1 ]; then
    print_debug "ERROR: Should find one volume less in $tmp/3.out (nb1=$nb1 nb2=$nb2)"
    estat=1
fi

end_test
