#!/bin/sh
#
# Copyright (C) 2000-2022 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/log2.out
status storage
@$out $tmp/log3.out
status dir
.api 2
@$out $tmp/log4.out
.status client resources
@$out $tmp/log5.out
.status client running
@$out $tmp/log6.out
.status storage running
@$out $tmp/log7.out
.status dir running
@$out $tmp/log8.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 "Name=" $tmp/log4.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: should find 'Name' in $tmp/log4.out"
    estat=1
fi

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

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


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

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

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

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

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

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

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

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

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

end_test
