#!/bin/bash

set -e
set -o pipefail
set -u

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

JobName=backup-bareos-fd
. ./environment
. ${BAREOS_SCRIPTS_DIR}/functions

${BAREOS_SCRIPTS_DIR}/cleanup
${BAREOS_SCRIPTS_DIR}/setup

# Fill ${BackupDirectory} with data.
setup_data

start_test

cat <<END_OF_DATA >$tmp/bconcmds
@$out ${NULL_DEV}
messages
@$out $tmp/log1.out
run job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bareos

${BAREOS_SCRIPTS_DIR}/webui.sh >"$tmp/php.out" 2>&1 &
PHP_PID=$(echo $!)

# run tests

#
# test with admin profile resource
#
echo "test with ${BAREOS_WEBUI_PROFILE} profile:" >"$tmp/selenium.out" 2>&1
if [ -n "${BAREOS_WEBUI_TESTS}" ]; then
  # Run all tests from the space-separated list in one selenium process
  # shellcheck disable=SC2086
  if ! ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/webui/tests/selenium/webui-selenium-test.py -v ${BAREOS_WEBUI_TESTS} >>"$tmp/selenium.out" 2>&1; then
    set_error "$(cat "$tmp/selenium.out")"
  fi
else
  if ! ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/webui/tests/selenium/webui-selenium-test.py -v "SeleniumTest.test_${BAREOS_WEBUI_TESTNAME}" >>"$tmp/selenium.out" 2>&1; then
    set_error "$(cat "$tmp/selenium.out")"
  fi
fi

kill "${PHP_PID}"

stop_bareos

end_test
