-- ============================================================================
-- MASTER SCRIPT: RUN ALL SYS SCRIPTS
-- ============================================================================
-- Executes all SYS scripts in correct order:
--   1. sys-objects.sql - Core objects (AUTH_PACK, tables, procedures)
--   2. sys-grants.sql - Grants and synonyms
--   3. sys-updates/*.sql - Additional updates (in alphabetical order)
--
-- Usage:
--   sqlplus sys/password@service as sysdba @run-all-sys.sql
--
-- ============================================================================

SET ECHO OFF
SET FEEDBACK ON
SET SERVEROUTPUT ON SIZE UNLIMITED
WHENEVER SQLERROR CONTINUE

PROMPT
PROMPT ============================================================
PROMPT     MASTER SYS INSTALLATION
PROMPT ============================================================
PROMPT

-- Get script directory (assuming run from sql/ folder)
DEFINE script_dir = .

-- ============================================================================
-- STEP 1: Core SYS Objects
-- ============================================================================
PROMPT
PROMPT ============================================================
PROMPT STEP 1: Installing core SYS objects...
PROMPT ============================================================
@sys-objects.sql

-- ============================================================================
-- STEP 2: SYS Grants and Synonyms
-- ============================================================================
PROMPT
PROMPT ============================================================
PROMPT STEP 2: Installing SYS grants and synonyms...
PROMPT ============================================================
@sys-grants.sql

-- ============================================================================
-- STEP 3: Additional SYS Updates
-- ============================================================================
PROMPT
PROMPT ============================================================
PROMPT STEP 3: Running additional SYS updates...
PROMPT ============================================================

-- Note: SQL*Plus doesn't support dynamic script execution from folder
-- Use PowerShell script 02-create-sys-objects.ps1 for full automation
-- Or manually list scripts here:

-- Example:
-- @sys-updates/2026_01_28_01_example.sql

PROMPT
PROMPT   To run additional updates, use PowerShell:
PROMPT   .\scripts\02-create-sys-objects.ps1
PROMPT
PROMPT   Or add @sys-updates/scriptname.sql lines above
PROMPT

-- ============================================================================
-- VERIFICATION
-- ============================================================================
PROMPT
PROMPT ============================================================
PROMPT VERIFICATION
PROMPT ============================================================

PROMPT
PROMPT SYS custom objects:
SELECT object_name, object_type, status
FROM dba_objects
WHERE owner = 'SYS'
  AND object_name IN (
      'AUTH_PACK', 'AUTH_DETALII', 'AUTH_SERII', 'VAUTH_SERII',
      'EXECUTESCRIPTOS', 'NEWSCHEMA', 'NEWSCHEMAJOB', 'UPDATESQLPLUS',
      'PINFO', 'INFO', 'SEQ_AUTH_SERII'
  )
ORDER BY object_type, object_name;

PROMPT
PROMPT SYS public synonyms:
SELECT synonym_name, table_owner, table_name
FROM dba_synonyms
WHERE owner = 'PUBLIC'
  AND table_owner = 'SYS'
  AND synonym_name IN ('SYN_NEWSCHEMA', 'SYN_NEWSCHEMAJOB', 'EXECUTESCRIPTOS', 'SYN_PINFO')
ORDER BY synonym_name;

PROMPT
PROMPT CONTAFIN_ORACLE grants from SYS:
SELECT privilege, table_name
FROM dba_tab_privs
WHERE grantee = 'CONTAFIN_ORACLE'
  AND grantor = 'SYS'
ORDER BY table_name;

PROMPT
PROMPT ============================================================
PROMPT     MASTER SYS INSTALLATION COMPLETE
PROMPT ============================================================
PROMPT
