-- ============================================================================
-- MARS-1409 Master Installation Script
-- ============================================================================
-- Author: Grzegorz Michalski
-- Purpose: Add A_WORKFLOW_HISTORY_KEY column to A_SOURCE_FILE_RECEIVED table
-- Target Schema: CT_MRDS
-- Estimated Time: 1-2 minutes
-- Prerequisites: FILE_MANAGER v3.x, ENV_MANAGER v3.x, ADMIN privileges
-- ============================================================================

SET SERVEROUTPUT ON SIZE UNLIMITED
SET VERIFY OFF
SET FEEDBACK ON
SET ECHO OFF

-- Create log directory if it doesn't exist
host mkdir log 2>nul

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

PROMPT
PROMPT ============================================================================
PROMPT MARS-1409 Installation Starting
PROMPT ============================================================================
PROMPT Package: CT_MRDS.FILE_MANAGER v3.X.X
PROMPT Change: Add A_WORKFLOW_HISTORY_KEY to A_SOURCE_FILE_RECEIVED
PROMPT Purpose: Direct tracking of workflow history keys in file registration
PROMPT Steps: 11 (DDL, ENV_MANAGER Update, FILE_MANAGER Update, FILE_ARCHIVER Update, Trigger Update, Verification, Tracking, Version Verification)
PROMPT Timestamp: 
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS install_start FROM DUAL;
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(-20000, 'Installation aborted by user');
    END IF;
END;
/

PROMPT
PROMPT ============================================================================
PROMPT STEP 1: Add A_WORKFLOW_HISTORY_KEY column to A_SOURCE_FILE_RECEIVED
PROMPT ============================================================================
@@01_MARS_1409_add_workflow_history_key_column.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 2: Update ENV_MANAGER package specification
PROMPT ============================================================================
@@02_MARS_1409_install_CT_MRDS_ENV_MANAGER_SPEC.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 3: Update ENV_MANAGER package body
PROMPT ============================================================================
@@03_MARS_1409_install_CT_MRDS_ENV_MANAGER_BODY.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 4: Update FILE_MANAGER package specification
PROMPT ============================================================================
@@04_MARS_1409_install_CT_MRDS_FILE_MANAGER_SPEC.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 5: Update FILE_MANAGER package body
PROMPT ============================================================================
@@05_MARS_1409_install_CT_MRDS_FILE_MANAGER_BODY.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 6: Update FILE_ARCHIVER package specification
PROMPT ============================================================================
@@06_MARS_1409_install_CT_MRDS_FILE_ARCHIVER_SPEC.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 7: Update FILE_ARCHIVER package body
PROMPT ============================================================================
@@07_MARS_1409_install_CT_MRDS_FILE_ARCHIVER_BODY.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 8: Update A_WORKFLOW_HISTORY trigger
PROMPT ============================================================================
@@08_MARS_1409_install_CT_MRDS_A_WORKFLOW_HISTORY.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 9: Verify installation
PROMPT ============================================================================
@@09_MARS_1409_verify_installation.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 10: Track package versions
PROMPT ============================================================================
@@track_package_versions.sql

PROMPT
PROMPT ============================================================================
PROMPT STEP 11: Verify package versions
PROMPT ============================================================================
@@verify_packages_version.sql

PROMPT
PROMPT ============================================================================
PROMPT MARS-1409 Installation Complete
PROMPT ============================================================================
PROMPT Final Status:
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS install_end FROM DUAL;
PROMPT
PROMPT Review the log file for detailed results: &_filename
PROMPT ============================================================================

spool off
