#!/bin/sh
#
# Copyright (C) 2000-2026 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Test if Volumes created automatically by bacula during backup
# are set with the APPEND flag, so that it cannot be overwritten accidentally.
# This test creates temporary bacula-sd.service file so that we can run SD
# as systemd service.
#
TestName="volume-append-many-vols-test"
JobName=append

. scripts/functions

require_linux

scripts/cleanup
scripts/copy-confs

cleanup()
{
   #Stop the daemon if the test was interrupted in the middle of a run
   sudo systemctl stop test-bacula-sd > /dev/null 2>&1
   #Disable the service
   sudo systemctl disable test-bacula-sd
   #Clear volume attributes if needed
   sudo chattr -ia tmp/*
}

trap 'cleanup' EXIT INT TERM

$bperl -e 'add_attribute("$conf/bacula-sd.conf", "SetVolumeAppendOnly", "yes", "Device", "FileChgr1-Dev1")'
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "SetVolumeAppendOnly", "yes", "Device", "FileChgr1-Dev2")'

# Create temporary bacula-sd.service file so that we can run SD as systemd service
scripts/create_sed
sed -f ${tmp}/sed_tmp ${cwd}/build/platforms/systemd/bacula-sd.service.in > ${tmp}/test-bacula-sd.service
sudo systemctl enable ${tmp}/test-bacula-sd.service

# 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

change_jobname BackupClient1 $JobName

start_test

# Set small MaxVolSize so that we are sure many volumes are created during backup
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "MaximumVolumeSize", "5M", "Device", "FileChgr1-Dev1")'

# Run FD and DIR the usual way, SD has to be run with systemd
${bin}/bacula-ctl-fd start
if test $? -ne 0; then
      print_debug "ERROR: Failed to start the FD!"
         dstat=1
fi
sudo systemctl start test-bacula-sd
if test $? -ne 0; then
      print_debug "ERROR: Failed to start the SD!"
      dstat=1
fi
${bin}/bacula-ctl-dir start
if test $? -ne 0; then
      print_debug "ERROR: Failed to start the DIR!"
      dstat=1
fi

# No volume is created manually before running backup job so that Bacula has to create it by itself
cat <<END_OF_DATA >$tmp/bconcmds
@output /dev/null
messages
@$out $tmp/log1.out
run job=$JobName yes
wait
messages
@$out $tmp/log2.out
restore where=$tmp/bacula-restores select all done yes
wait
messages
quit
END_OF_DATA

run_bconsole

backup_res=`cat $tmp/log1.out| grep 'Termination:' | grep 'Backup OK' | wc -l`
if [ ${backup_res} -ne 1 ]; then
   estat=1
   print_debug "ERROR: JobId 1 did not succed, see ${tmp}/log2.out!"
fi

# Check how many volumes were created
n_vols=`grep 'Volume name(s):' $tmp/log1.out | grep -o 'Vol-' | wc -l`

# Check if all of the volumes created have the flag set
append_find=`lsattr ${tmp}/Vol-* -l | grep 'Append_Only' | wc -l`
if [ ${append_find} -ne ${n_vols} ]; then
   estat=1
   print_debug "ERROR: Expected ${n_vols} volumes with the Append flag set, found ${append_find} instead!"
fi

# Check if restore was ok
restore_res=`cat $tmp/log2.out| grep 'Termination:' | grep 'Restore OK' | wc -l`
if [ ${restore_res} -ne 1 ]; then
   estat=1
   print_debug "ERROR: Restoring job 1 did not succed, see ${tmp}/log3.out!"
fi

sudo systemctl stop test-bacula-sd
${bin}/bacula-ctl-fd stop
${bin}/bacula-ctl-dir stop

end_test
