-- ===================================================================
-- MARS-835-PREHOOK INSTALL SCRIPT: DRY Refactoring for DATA_EXPORTER
-- ===================================================================
-- Purpose: Pre-hook for MARS-835 - DRY refactoring of DATA_EXPORTER BY_DATE procedures
-- Author: Grzegorz Michalski
-- Date: 2025-12-19
-- Version: 2.2.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_835_PREHOOK_' || 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

PROMPT =========================================================================
PROMPT MARS-835-PREHOOK: DRY Refactoring for DATA_EXPORTER Package
PROMPT =========================================================================
PROMPT 
PROMPT This script will:
PROMPT - Deploy DATA_EXPORTER v2.2.0 with DRY code refactoring
PROMPT - Refactor EXPORT_TABLE_DATA_BY_DATE (eliminate code duplication)
PROMPT - Refactor EXPORT_TABLE_DATA_TO_CSV_BY_DATE (eliminate code duplication)
PROMPT - Track new package version in ENV_MANAGER
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(-20999, 'Installation aborted by user.');
    END IF;
END;
/
WHENEVER SQLERROR CONTINUE

PROMPT 
PROMPT =========================================================================
PROMPT Step 1: Deploy DATA_EXPORTER Package Specification (v2.2.0)
PROMPT =========================================================================
@@01_MARS_835_install_DATA_EXPORTER_SPEC.sql

PROMPT 
PROMPT =========================================================================
PROMPT Step 2: Deploy DATA_EXPORTER Package Body (v2.2.0)
PROMPT =========================================================================
@@02_MARS_835_install_DATA_EXPORTER_BODY.sql

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

PROMPT 
PROMPT =========================================================================
PROMPT Step 4: Verify Package Versions
PROMPT =========================================================================
@@verify_packages_version.sql

PROMPT 
PROMPT =========================================================================
PROMPT MARS-835-PREHOOK Installation - COMPLETED
PROMPT =========================================================================
PROMPT Check the log file for complete installation details.
PROMPT =========================================================================

spool off

quit;
