-- ============================================================================
-- SYS GRANTS AND SYNONYMS FOR ROA APPLICATION
-- ============================================================================
-- Run AFTER sys-objects.sql and AFTER CONTAFIN_ORACLE import
-- These grants enable CONTAFIN_ORACLE packages to work correctly
--
-- Usage:
--   sqlplus sys/password@service as sysdba @sys-grants.sql
--
-- ============================================================================

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

PROMPT
PROMPT ============================================================
PROMPT Installing SYS Grants and Synonyms
PROMPT ============================================================
PROMPT

-- ============================================================================
-- SECTION 1: GRANTS TO CONTAFIN_ORACLE
-- ============================================================================
PROMPT
PROMPT [1/4] Granting privileges to CONTAFIN_ORACLE...

-- Execute on SYS packages
GRANT EXECUTE ON SYS.AUTH_PACK TO CONTAFIN_ORACLE;
GRANT EXECUTE ON SYS.DBMS_SCHEDULER TO CONTAFIN_ORACLE;
GRANT EXECUTE ON SYS.DBMS_LOCK TO CONTAFIN_ORACLE;
GRANT EXECUTE ON SYS.UTL_INADDR TO CONTAFIN_ORACLE;
GRANT EXECUTE ON SYS.UTL_FILE TO CONTAFIN_ORACLE;
GRANT EXECUTE ON SYS.UTL_SMTP TO CONTAFIN_ORACLE;
GRANT EXECUTE ON SYS.UTL_TCP TO CONTAFIN_ORACLE;
GRANT EXECUTE ON SYS.DBMS_CRYPTO TO CONTAFIN_ORACLE;

-- Directory privileges
GRANT CREATE ANY DIRECTORY TO CONTAFIN_ORACLE;
GRANT DROP ANY DIRECTORY TO CONTAFIN_ORACLE;

-- Scheduler privileges
GRANT CREATE JOB TO CONTAFIN_ORACLE;
GRANT CREATE EXTERNAL JOB TO CONTAFIN_ORACLE;
GRANT MANAGE SCHEDULER TO CONTAFIN_ORACLE;

PROMPT   Grants to CONTAFIN_ORACLE complete.

-- ============================================================================
-- SECTION 2: PUBLIC GRANTS
-- ============================================================================
PROMPT
PROMPT [2/4] Granting public privileges...

-- UTL packages for public use
GRANT EXECUTE ON SYS.UTL_FILE TO PUBLIC;

PROMPT   Public grants complete.

-- ============================================================================
-- SECTION 3: SYS PUBLIC SYNONYMS
-- ============================================================================
PROMPT
PROMPT [3/4] Creating SYS public synonyms...

-- Synonyms for SYS procedures
BEGIN EXECUTE IMMEDIATE 'DROP PUBLIC SYNONYM SYN_NEWSCHEMA'; EXCEPTION WHEN OTHERS THEN NULL; END;
/
BEGIN EXECUTE IMMEDIATE 'DROP PUBLIC SYNONYM SYN_NEWSCHEMAJOB'; EXCEPTION WHEN OTHERS THEN NULL; END;
/
BEGIN EXECUTE IMMEDIATE 'DROP PUBLIC SYNONYM EXECUTESCRIPTOS'; EXCEPTION WHEN OTHERS THEN NULL; END;
/
BEGIN EXECUTE IMMEDIATE 'DROP PUBLIC SYNONYM SYN_PINFO'; EXCEPTION WHEN OTHERS THEN NULL; END;
/

CREATE PUBLIC SYNONYM SYN_NEWSCHEMA FOR SYS.NEWSCHEMA;
CREATE PUBLIC SYNONYM SYN_NEWSCHEMAJOB FOR SYS.NEWSCHEMAJOB;
CREATE PUBLIC SYNONYM EXECUTESCRIPTOS FOR SYS.EXECUTESCRIPTOS;
CREATE PUBLIC SYNONYM SYN_PINFO FOR SYS.PINFO;

-- Grants on synonyms
GRANT EXECUTE ON SYN_NEWSCHEMA TO PUBLIC;
GRANT EXECUTE ON SYN_NEWSCHEMAJOB TO PUBLIC;
GRANT EXECUTE ON EXECUTESCRIPTOS TO PUBLIC;
GRANT EXECUTE ON SYN_PINFO TO PUBLIC;

PROMPT   SYS public synonyms complete.

-- ============================================================================
-- SECTION 4: DIRECTORY DMPDIR
-- ============================================================================
PROMPT
PROMPT [4/4] Creating DMPDIR directory...

BEGIN
    EXECUTE IMMEDIATE 'CREATE OR REPLACE DIRECTORY DMPDIR AS ''C:\DMPDIR''';
    DBMS_OUTPUT.PUT_LINE('  Directory DMPDIR created.');
EXCEPTION
    WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('  Directory DMPDIR: ' || SQLERRM);
END;
/

GRANT READ, WRITE ON DIRECTORY DMPDIR TO PUBLIC;

PROMPT
PROMPT ============================================================
PROMPT SYS Grants and Synonyms Installation Complete
PROMPT ============================================================
PROMPT
