#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Test for #7739
# Check if bacula denies to delete job which is actually running
#
TestName="delete-running-job-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

change_jobname BackupClient1 $JobName
start_test

cat <<END_OF_DATA >$tmp/bconcmds
@output /dev/null
messages
@$out $tmp/log1.out
label volume=TestVolume001 storage=File1 pool=File slot=1 drive=0
run job=$JobName yes
run job=$JobName yes
@sleep 1
@$out $tmp/log2.out
delete jobid=1,2
@$out $tmp/log3.out
wait
messages
quit
END_OF_DATA

run_bacula

n_job1=`cat ${cwd}/tmp/log2.out | grep "Skipping JobId=1, job is still running!" | wc -l`
if [ $n_job1 -ne 1 ]; then
   estat=1
   print_debug "JobId=1 should be skipped when deleting records!"
fi

n_job2=`cat ${cwd}/tmp/log2.out | grep "Skipping JobId=2, job is still running!" | wc -l`
if [ $n_job2 -ne 1 ]; then
   estat=1
   print_debug "JobId=2 should be skipped when deleting records!"
fi


n_bakup_ok=`cat ${cwd}/tmp/log3.out | grep "Termination: " | tr -s ' ' | grep "Backup OK" | wc -l`
if [ $n_bakup_ok -ne 2 ]; then
   estat=1
   print_debug "Backup of some jobs failed! See ${cwd}/tmp/log2.out for more info!"
fi

stop_bacula

end_test
