#!/usr/bin/env bash
# Provisions the single DDS technical END USER used by business-user searches.
# Required environment: DDS_ADMIN_DB_URL, DDS_ADMIN_USERNAME,
# DDS_ADMIN_PASSWORD, DDS_TOKEN_PASSWORD.
set -Eeuo pipefail

: "${DDS_ADMIN_DB_URL:?set DDS_ADMIN_DB_URL}"
: "${DDS_ADMIN_USERNAME:?set DDS_ADMIN_USERNAME}"
: "${DDS_ADMIN_PASSWORD:?set DDS_ADMIN_PASSWORD}"
: "${DDS_TOKEN_PASSWORD:?set DDS_TOKEN_PASSWORD}"

root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
connect="${DDS_ADMIN_USERNAME}/${DDS_ADMIN_PASSWORD}@${DDS_ADMIN_DB_URL}"

sqlplus -s "$connect" <<SQL
WHENEVER SQLERROR EXIT SQL.SQLCODE
DEFINE DDSUSER_TOKEN_PASSWORD = '${DDS_TOKEN_PASSWORD}'
@${root}/sql/adb/34_dds_token_data_grant_common_auth.sql
@${root}/sql/adb/42_dds_fga_execution_evidence.sql

PROMPT === DDS token search verification ===
SELECT username FROM dba_users WHERE username = 'dds_demo_token';
SELECT grant_name, object_name, grantee
  FROM dba_data_grants
 WHERE grant_name = 'DDS_DEMO_TOKEN_VECTOR_GRANT';
EXIT
SQL

echo 'Provisioning completed. Configure only these server Secret values:'
echo '  DDS_BACKOFFICE_DB_URL=<DB JDBC URL>'
echo '  DDS_BACKOFFICE_TOKEN_USERNAME=dds_demo_token'
echo '  DDS_BACKOFFICE_TOKEN_PASSWORD=<DDS_TOKEN_PASSWORD value>'
