-- ===================================================================
-- MARS-956 INSTALL SCRIPT: C2D MPEC Data Export to External Tables
-- ===================================================================
-- Purpose: One-time bulk export of 3 C2D MPEC tables from OU_LEGACY_C2D schema
--          to OCI buckets (ODS bucket CSV format)
--          Uses DATA_EXPORTER v2.7.5 with pRegisterExport for file registration
-- Author: Grzegorz Michalski
-- Date: 2026-02-12

-- Dynamic spool file generation (using SYS_CONTEXT - no DBA privileges required)
-- Log files are automatically created in log/ subdirectory
-- IMPORTANT: Ensure log/ directory exists before SPOOL (use host mkdir)
host mkdir log 2>nul

var filename VARCHAR2(100)
BEGIN
    :filename := 'log/INSTALL_MARS_956_' || SYS_CONTEXT('USERENV', 'CON_NAME') || '_' || TO_CHAR(SYSDATE,'YYYYMMDD_HH24MISS') || '.log';
END;
/
column filename new_value _filename
select :filename filename from dual;
spool &_filename

SET ECHO OFF
SET TIMING ON
SET SERVEROUTPUT ON SIZE UNLIMITED
SET PAUSE OFF

-- Set current schema context (optional - use when modifying packages in specific schema)
-- ALTER SESSION SET CURRENT_SCHEMA = CT_MRDS;

PROMPT =========================================================================
PROMPT MARS-956: C2D MPEC Data Export to External Tables (One-Time Migration)
PROMPT =========================================================================
PROMPT 
PROMPT This script will export 3 C2D MPEC tables to OCI buckets:
PROMPT 
PROMPT TARGET: ODS Bucket (CSV format):
PROMPT   - MPEC_ADMIN
PROMPT   - MPEC_CONTENT
PROMPT   - MPEC_CONTENT_CRITERION
PROMPT 
PROMPT Key Features:
PROMPT   - Files registered in A_SOURCE_FILE_RECEIVED for tracking
PROMPT   - Template table column order matching (CT_ET_TEMPLATES.C2D_MPEC_*)
PROMPT   - ODS/C2D bucket path structure
PROMPT =========================================================================

-- Confirm installation with user
ACCEPT continue CHAR PROMPT 'Type YES to continue with installation, or Ctrl+C to abort: '
WHENEVER SQLERROR EXIT SQL.SQLCODE
BEGIN
    IF '&continue' IS NULL OR TRIM('&continue') IS NULL OR UPPER(TRIM('&continue')) != 'YES' THEN
        RAISE_APPLICATION_ERROR(-20001, 'Installation aborted by user');
    END IF;
END;
/
WHENEVER SQLERROR CONTINUE

PROMPT 
PROMPT =========================================================================
PROMPT Step 1: Export C2D MPEC Data to ODS Bucket
PROMPT =========================================================================
@@01_MARS_956_export_c2d_mpec_data.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 2: Verify Exports (File Registration Check)
PROMPT =========================================================================
@@02_MARS_956_verify_exports.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 3: Verify Data Integrity (Source vs Exported)
PROMPT =========================================================================
@@03_MARS_956_verify_data_integrity.sql

PROMPT 
PROMPT =========================================================================
PROMPT MARS-956 Installation - COMPLETED
PROMPT =========================================================================
PROMPT Check the log file for complete installation details.
PROMPT For rollback, use: rollback_mars956.sql
PROMPT =========================================================================

spool off

quit;