-- ===================================================================
-- MARS-1057 INSTALL SCRIPT: Batch External Table Creation
-- ===================================================================
-- Purpose: Install FILE_MANAGER v3.5.0 with batch external table creation procedures
--          and area filter functionality
-- Author: Grzegorz Michalski
-- Date: 2026-02-18
-- Version: 3.5.0

-- 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_1057_' || 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-1057: Batch External Table Creation + Area Filter
PROMPT =========================================================================
PROMPT 
PROMPT This script will install FILE_MANAGER package v3.5.0 with new features:
PROMPT - CREATE_EXTERNAL_TABLES_SET procedure
PROMPT - CREATE_EXTERNAL_TABLES_BATCH procedure
PROMPT - pArea parameter for selective table creation (INBOX/ODS/ARCHIVE/ALL)
PROMPT - pRestoreGrants parameter for grant preservation during recreate
PROMPT 
PROMPT And FILE_MANAGER_ODS package v2.4.0 with wrapper procedures.
PROMPT 
PROMPT Changes:
PROMPT - FILE_MANAGER package specification (3.3.0 -> 3.5.0)
PROMPT - FILE_MANAGER package body (3.3.0 -> 3.5.0)
PROMPT - FILE_MANAGER_ODS package specification (2.1.0 -> 2.4.0)
PROMPT - FILE_MANAGER_ODS package body (2.1.0 -> 2.4.0)
PROMPT 
PROMPT New functionality:
PROMPT - pArea => 'INBOX'   : creates only INBOX table
PROMPT - pArea => 'ODS'     : creates only ODS table
PROMPT - pArea => 'ARCHIVE' : creates only ARCHIVE table
PROMPT - pArea => 'ALL'     : creates all tables (default)
PROMPT 
PROMPT Expected Duration: 1-2 minutes
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: Install FILE_MANAGER Package Specification v3.5.0
PROMPT =========================================================================
@@01_MARS_1057_install_CT_MRDS_FILE_MANAGER_SPEC.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 2: Install FILE_MANAGER Package Body v3.5.0
PROMPT =========================================================================
@@02_MARS_1057_install_CT_MRDS_FILE_MANAGER_BODY.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 3: Install FILE_MANAGER_ODS Package Specification v2.4.0
PROMPT =========================================================================
@@03_MARS_1057_install_ODS_FILE_MANAGER_ODS_SPEC.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 4: Install FILE_MANAGER_ODS Package Body v2.4.0
PROMPT =========================================================================
@@04_MARS_1057_install_ODS_FILE_MANAGER_ODS_BODY.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 5: Track Package Versions
PROMPT =========================================================================
@@track_package_versions.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 6: Verify All Tracked Packages
PROMPT =========================================================================
@@verify_packages_version.sql

PROMPT 
PROMPT =========================================================================
PROMPT MARS-1057 Installation - COMPLETED
PROMPT =========================================================================
PROMPT Check the log file for complete installation details.
PROMPT Log file: log/INSTALL_MARS_1057_<PDB>_<timestamp>.log
PROMPT =========================================================================

spool off

quit;
