#!/bin/bash
set -e
set -o pipefail
set -u
#
TestName="$(basename "$(pwd)")"
export TestName
#shellcheck source=../../environment.in
. ./environment

JobName=backup-bareos-fd
#shellcheck source=../../scripts/functions
. "${BAREOS_SCRIPTS_DIR}"/functions
"${BAREOS_SCRIPTS_DIR}"/cleanup
"${BAREOS_SCRIPTS_DIR}"/setup

# The purge-discovery probe replaces this fixture table with a view.
psql --no-psqlrc --set=ON_ERROR_STOP=1 --set=db_user="$db_user" \
  --dbname="$db_name" <<'SQL'
ALTER TABLE FileSet OWNER TO :"db_user";
SQL

secondary_catalog="${db_name}_secondary"
db_name="$secondary_catalog" "${BAREOS_SCRIPTS_DIR}/drop_bareos_database"
db_name="$secondary_catalog" "${BAREOS_SCRIPTS_DIR}/create_bareos_database"

cleanup_secondary_catalog()
{
  db_name="$secondary_catalog" "${BAREOS_SCRIPTS_DIR}/drop_bareos_database"
}
trap cleanup_secondary_catalog EXIT

db_name="$secondary_catalog" "${BAREOS_SCRIPTS_DIR}/make_bareos_tables"
db_name="$secondary_catalog" "${BAREOS_SCRIPTS_DIR}/grant_bareos_privileges"

exit_failure()
{
  estat=1
  cleanup_secondary_catalog
  end_test
}

start_test

pushd "${BAREOS_UNITTESTS_BINARY_DIR}" >/dev/null
"${CATALOG_TEST_PROGRAM}" || exit_failure
popd

cleanup_secondary_catalog
end_test
