#!/bin/bash
set -e
set -o pipefail
set -u
#
# a simple test that checks some client status commands work
#
TestName="$(basename "$(pwd)")"
export TestName

#shellcheck source=../../environment.in
. ./environment

JobName=bconsole-status-client
#shellcheck source=../../scripts/functions
. "${rscripts}"/functions

start_test
rm -f "$tmp/log2.out"

cat <<END_OF_DATA >$tmp/bconcmds
@$out ${NULL_DEV}
messages
@$out $tmp/log1.out
status client=bareos-fd

@$out $tmp/log2.out
messages

@$out $tmp/log3.out
disable client=bareos-fd
status client=bareos-fd
messages

@$out $tmp/log4.out
enable client=bareos-fd
status client=bareos-fd

messages
quit
END_OF_DATA

# Start the bareos daemons
# and run the bconsole commands from ${tmp}/bconcmds
# Further bconsole commands can be executed by "run_bconsole".
run_bconsole

# if log2.out has not been written, bconsole crashed or disconnected
# after status client command
test -f "$tmp/log2.out" || exit 2
# check that bareos-fd did not report that its disabled while being enabled
# and reports being disabled while being disabled
expect_not_grep "Client \"bareos-fd\" is disabled" \
  "$tmp/log1.out" \
  "Error found disabled confirmation while client should be enabled."
expect_not_grep "Client \"bareos-fd\" is disabled" \
  "$tmp/log2.out" \
  "Error found disabled confirmation while client should be enabled."
expect_grep "Client \"bareos-fd\" is disabled" \
  "$tmp/log3.out" \
  "Error missing disabled confirmation."
expect_not_grep "Client \"bareos-fd\" is disabled" \
  "$tmp/log4.out" \
  "Error found disabled confirmation while client should be enabled."

end_test
