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

scripts/cleanup
scripts/copy-confs

if [ x$FORCE_SDCALLS = xyes ]; then
    echo "Test disabled with SDCalls"
    exit 0
fi

#
# 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
@$out /dev/null
messages
@$out $tmp/log1.out
status client
@$out $tmp/log111.out
sql
select 'ERROR', Name, Uname, Plugins from Client WHERE Uname = '';


@$out $tmp/log2.out
status storage
@$out $tmp/log3.out
status dir
@$out $tmp/log4.out
disable client
@$out $tmp/log5.out
status client
@$out $tmp/log6.out
status all
@$out $tmp/log7.out
enable client
.api 2
.status client resources
@$out $tmp/log8.out
.status client running
@$out $tmp/log9.out
.status storage running
@$out $tmp/log10.out
.status dir running
@$out $tmp/log11.out
.status storage resources
quit
END_OF_DATA

run_bacula
stop_bacula

grep "Director connected" $tmp/log1.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find director connection in $tmp/log1.out"
    estat=1
fi

grep "Running Jobs:" $tmp/log1.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find 'Running Jobs' in $tmp/log1.out"
    estat=1
fi

grep "Terminated Jobs:" $tmp/log1.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find 'Terminated Jobs' in $tmp/log1.out"
    estat=1
fi

grep "Director connected" $tmp/log2.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find director connection in $tmp/log2.out"
    estat=1
fi

grep "Device status:" $tmp/log2.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find 'Device status' in $tmp/log2.out"
    estat=1
fi

grep "Jobs waiting to reserve a drive:" $tmp/log2.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find 'Jobs waiting to reserve a drive' in $tmp/log2.out"
    estat=1
fi

grep "Device File:" $tmp/log2.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find 'Device File' in $tmp/log2.out"
    estat=1
fi

grep "Used Volume status:" $tmp/log2.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find 'Used Volume status' in $tmp/log2.out"
    estat=1
fi

grep "Console connected" $tmp/log3.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find console connection in $tmp/log3.out"
    estat=1
fi

grep "Scheduled Jobs" $tmp/log3.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find 'Scheduled Jobs' in $tmp/log3.out"
    estat=1
fi

grep "Running Jobs:" $tmp/log3.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find 'Running Jobs' in $tmp/log3.out"
    estat=1
fi

grep "Selection list for \"Client\" is empty!" $tmp/log5.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find 'Selection list for \"Client\" is empty!' in $tmp/log5.out"
    estat=1
fi

grep "Connecting to Client" $tmp/log6.out > /dev/null
if [ $? -eq 0 ]; then
    print_debug "ERROR: should not find 'Connectint to Client' in $tmp/log6.out"
    estat=1
fi

grep "Name=" $tmp/log7.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find 'Name' in $tmp/log7.out"
    estat=1
fi

grep "FDPort=" $tmp/log7.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find 'FDPort' in $tmp/log7.out"
    estat=1
fi

grep "WorkingDirectory=" $tmp/log7.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find 'WorkingDirectory =' in $tmp/log7.out"
    estat=1
fi


grep "DirectorConnected" $tmp/log8.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find director connection in $tmp/log8.out"
    estat=1
fi

grep "Automatically selected Client:" $tmp/log8.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find 'Automatically selected Client' in $tmp/log8.out"
    estat=1
fi

grep "DirectorConnected_epoch" $tmp/log8.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find 'DirectorConnected_epoch' in $tmp/log8.out"
    estat=1
fi

grep "DirectorConnected" $tmp/log9.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find director connection in $tmp/log9.out"
    estat=1
fi

grep "Automatically selected Storage:" $tmp/log9.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find 'Automatically selected Storage' in $tmp/log9.out"
    estat=1
fi

grep "SDPort=" $tmp/log11.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find 'SDPort' in $tmp/log11.out"
    estat=1
fi

grep "MaximumConcurrentJobs" $tmp/log11.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find 'MaximumConcurrentJobsSDPort' in $tmp/log11.out"
    estat=1
fi

nb=`grep "ERROR" $tmp/log111.out | wc -l`
if [ $nb -gt 1 ]; then
    print_debug "ERROR: Should find Uname updated after status client in $tmp/log111.out"
    estat=1
fi

end_test
