#!/bin/sh
#
# Copyright (C) 2023-2026 Bacula Systems SA
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Test runscript Queue level
#
. scripts/functions

TestName="runscript-jobq-test"
JobName=backup1

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

cat <<END_OF_DATA >> $bin/bacula-dir.conf
Job {
  Name = "backup"
    FileSet = "Full Set"
    JobDefs = DefaultJob
    RunScript {
      Command = "$tmp/jobqctrl"
      RunsWhen = Queued
      RunsOnClient = no
   }
}
END_OF_DATA

start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/RUN_ALL_OK.log
label volume=TestVolume001 storage=File2 pool=File slot=1 drive=0
run job=backup_before yes
wait
messages
quit
END_OF_DATA

run_bacula

status=`cat $tmp/status_before.log | grep 'executing Before Job Scripts'`
if [ "$status" = "" ]; then
    print_debug "Did not found information about the BeforeJob script, see $tmp/status_before.log"
    estat=1
fi

status=`cat $tmp/status_after.log | grep 'executing After Job Scripts'`
if [ "$status" = "" ]; then
    print_debug "Did not found information about the AfterJob script, see $tmp/status_after.log"
    estat=1
fi

stop_bacula

end_test
