#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Test the events code
#
TestName="events-test"
JobName=backup
. scripts/functions

require_linux
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 "add_log_message('$conf/bacula-sd.conf', '$conf/bacula-fd.conf')"
sed -i "s/ = all/ = all,events/" $conf/bacula-sd.conf
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'catalog', 'all,events', 'Messages')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'console', 'all,!skipped,!saved,events', 'Messages')"

change_jobname BackupClient1 $JobName
start_test

cat <<END_OF_DATA >$tmp/bconcmds
@$out $tmp/log1.out
run job=$JobName level=full yes
wait
run job=$JobName level=full yes
wait
messages
delete jobid=1
purge volume=Vol-0001 yes
run job=$JobName level=full yes
cancel jobid=3 yes
wait
delete volume=Vol-0001 yes
.events type=bweb source=eric text="User login"
.events type=bweb source=eric text="User logout"
.events type="*error*" source=eric text="error!!"
quit
END_OF_DATA

run_bacula
stop_bacula

cat <<END_OF_DATA >$tmp/bconcmds
@$out $tmp/log2.out
list events
@$out $tmp/log3.out
llist events
quit
END_OF_DATA

run_bacula

sed -i "s/ = all/ = all,events/" $conf/bacula-fd.conf
$bin/bacula-ctl-fd restart

cat <<END_OF_DATA >$tmp/bconcmds
@$out $tmp/log4.out
status client
@$out $tmp/log5.out
list events
@$out $tmp/log6.out
llist events type=bweb
@$out $tmp/log7.out
llist events source=*Console*
@$out $tmp/log8.out
llist events code=DJ0001
@# Sending .jlist output only in log9.out
@output $tmp/log9.out
.jlist events
@$out $tmp/log10.out
llist events limit=1
@$out $tmp/log11.out
llist events limit=1 offset=1
@$out $tmp/log12.out
llist events limit=2
quit
END_OF_DATA

run_bconsole

stop_bacula

nb_total=`grep -i "code:" $tmp/log8.out | wc -l`
nb_found=`grep " DJ0001" $tmp/log8.out | wc -l`

if [ $nb_found != $nb_total ]; then
    print_debug "ERROR: Found unexpected codes in $tmp/log8.out ($nb_found/$nb_total)"
    estat=1
fi

nb_total=`grep -i "source:" $tmp/log7.out | wc -l`
nb_found=`grep " \\*Console*" $tmp/log7.out | wc -l`

if [ $nb_found != $nb_total ]; then
    print_debug "ERROR: Found unexpected source in $tmp/log7.out ($nb_found/$nb_total)"
    estat=1
fi

nb_total=`grep -i "type:" $tmp/log6.out | wc -l`
nb_found=`grep " bweb" $tmp/log6.out | wc -l`

if [ $nb_found != $nb_total ]; then
    print_debug "ERROR: Found unexpected type in $tmp/log6.out ($nb_found/$nb_total)"
    estat=1
fi


grep "User login" $tmp/log2.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: should find bweb user login in $tmp/log2.out"
    estat=1
fi

grep "User logout" $tmp/log2.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: should find bweb user logout in $tmp/log2.out"
    estat=1
fi

grep "error" $tmp/log2.out > /dev/null
if [ $? = 0 ]; then
    print_debug "ERROR: should not find incorrect message in $tmp/log2.out"
    estat=1
fi

grep "Director startup" $tmp/log2.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: should find Director startup message in $tmp/log2.out"
    estat=1
fi

grep "Director shutdown" $tmp/log2.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: should find Director shutdown message in $tmp/log2.out"
    estat=1
fi

grep "Connection" $tmp/log2.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: should find Console connection message in $tmp/log2.out"
    estat=1
fi

grep "delete volume" $tmp/log2.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: should find delete volume message in $tmp/log2.out"
    estat=1
fi

grep "purge jobid" $tmp/log2.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: should find delete volume message in $tmp/log2.out"
    estat=1
fi

grep -- "-fd " $tmp/log2.out | grep "Director connection" > /dev/null
if [ $? = 0 ]; then
    print_debug "ERROR: Should not find messages from the FD in the catalog in $tmp/log2.out"
    estat=1
fi

grep -- "-sd " $tmp/log2.out | grep "Job Start" > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Should find messages from the SD in the catalog in $tmp/log2.out"
    estat=1
fi

grep -- "-fd " $tmp/log5.out | grep "Director connection" > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Should find messages from the FD in the catalog in $tmp/log5.out"
    estat=1
fi

grep -i unixtime $tmp/log9.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Should find unixtime in json output in $tmp/log9.out"
    estat=1
fi

grep "\[{" $tmp/log9.out > $tmp/log-json.out
$bperl -e "check_json('$tmp/log-json.out')"
if [ $? != 0 ]; then
    print_debug "ERROR: Should find valid json output in $tmp/log-json.out and $tmp/log9.out"
    estat=1
fi

nb=`grep -i events: $tmp/log10.out | wc -l`
if [ $nb != 1 ]; then
    print_debug "ERROR: Need to find only one event in $tmp/log10.out"
    estat=1
fi

nb=`grep -i events: $tmp/log11.out | wc -l`
if [ $nb != 1 ]; then
    print_debug "ERROR: Need to find only one event in $tmp/log11.out"
    estat=1
fi

nb=`grep -i events: $tmp/log12.out | wc -l`
if [ $nb != 2 ]; then
    print_debug "ERROR: Need to find only one event in $tmp/log12.out"
    estat=1
fi

a=`grep -i events: $tmp/log10.out`
grep "$a" $tmp/log12.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Need the event $a in $tmp/log12.out"
    estat=1
fi

a=`grep -i events: $tmp/log9.out`
grep "$a" $tmp/log12.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Need the event $a in $tmp/log12.out"
    estat=1
fi


end_test
