#!/bin/bash
set -e
set -o pipefail
set -u
#
# This test runs the messages unittest
#  to examine all message directions
#
# The director direction is not tested
#  here because all other tests use it
#

TestName="$(basename "$(pwd)")"
export TestName

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

#shellcheck source=../../scripts/functions
. "${BAREOS_SCRIPTS_DIR}"/functions

console_messages_file="${BAREOS_LOG_DIR}"/.conmsg
syslog_file="${BAREOS_LOG_DIR}"/syslog
dblog_file="${BAREOS_LOG_DIR}"/dblog
stdout_file="${BAREOS_LOG_DIR}"/stdout
stderr_file="${BAREOS_LOG_DIR}"/stderr
extra_append_file="${BAREOS_LOG_DIR}"/append-to-a-very-long-file-12345678901234567890123456789012345678901234567890abcdefghijklmnopqrstuvwxyz12345678901234567890abcdefg
mail_file="${BAREOS_LOG_DIR}"/mail
operator_file="${BAREOS_LOG_DIR}"/operator

rm -f "$console_messages_file"
rm -f "$syslog_file"
rm -f "$dblog_file"
rm -f "$stdout_file"
rm -f "$stderr_file"
rm -f "$extra_append_file"
rm -f "$mail_file"
rm -f "$operator_file"

function exit_error()
{
  [ "${1:-}" == "" ] || echo "$1"
  estat=1
  end_test
  exit 1
}

function check_output_file
{
  [ -f "$1" ] || exit_error "$1 does not exist"

  if ! grep --quiet "!!!This is a test error message!!!" "$1"; then
    exit_error "\"$1\" does not contain the expected text"
  fi
}

start_test

"${MESSAGES_TEST_PROGRAM}" >"$stdout_file" 2>"$stderr_file" || exit_error

check_output_file "$console_messages_file"
check_output_file "$syslog_file"
check_output_file "$dblog_file"
check_output_file "$stdout_file"
check_output_file "$extra_append_file"
check_output_file "$mail_file"
check_output_file "$operator_file"

end_test
