-- ============================================================================
-- SERVER_INFO INITIALIZATION FOR ROA UPDATE SYSTEM
-- ============================================================================
-- Initializes SERVER_INFO table with required values for PACK_UPDATE
-- Passwords are encoded using pack_utils.encodebase64(pack_utils.zipblob(pack_utils.Clob2Blob()))
--
-- Usage:
--   sqlplus contafin_oracle/password@service @server-info-init.sql
--
-- Prerequisites:
--   - CONTAFIN_ORACLE schema imported with PACK_UTILS package
--   - SERVER_INFO table exists
--
-- Substitution Variables (prompted if not defined):
--   &roaupdate_path      - Base path for ROAUPDATE folder (e.g., D:\ROAUPDATE)
--   &sqlplus_path        - Full path to sqlplus.exe
--   &password_sys_encoded    - Encoded SYS password (use encode_password helper)
--   &password_contafin_encoded - Encoded CONTAFIN_ORACLE password
--   &email_from          - Email sender address
--   &email_to            - Email recipient address
--   &email_smtp          - SMTP server hostname
--   &email_username      - SMTP authentication username
--   &email_password      - SMTP authentication password (plain text)
-- ============================================================================

SET ECHO OFF
SET FEEDBACK ON
SET SERVEROUTPUT ON
SET VERIFY OFF
WHENEVER SQLERROR CONTINUE

PROMPT
PROMPT ========================================
PROMPT Initializing SERVER_INFO Configuration
PROMPT ========================================
PROMPT

-- ============================================================================
-- PASSWORD ENCODING HELPER
-- ============================================================================
-- To encode a password, run in SQL*Plus:
--   SELECT CAST(pack_utils.encodebase64(pack_utils.zipblob(
--          pack_utils.Clob2Blob('your_password'))) AS VARCHAR2(500)) FROM dual;
--
-- Examples:
--   'romfastsoft'  => 'H4sIAAAAAAAACyvKz01LLC4pzk8rAQC2TmakCwAAAA=='
--   'ROMFASTSOFT'  => 'H4sIAAAAAAAACwvy93VzDA4J9ncLAQBe3GUpCwAAAA=='
-- ============================================================================

-- Define variables (will prompt if not defined)
DEFINE roaupdate_path = '&roaupdate_path'
DEFINE sqlplus_path = '&sqlplus_path'
DEFINE password_sys_encoded = '&password_sys_encoded'
DEFINE password_contafin_encoded = '&password_contafin_encoded'
DEFINE email_from = '&email_from'
DEFINE email_to = '&email_to'
DEFINE email_smtp = '&email_smtp'
DEFINE email_username = '&email_username'
DEFINE email_password = '&email_password'

PROMPT Configuration values:
PROMPT   ROAUPDATE Path: &roaupdate_path
PROMPT   SQLPlus Path:   &sqlplus_path
PROMPT   Email From:     &email_from
PROMPT   Email To:       &email_to
PROMPT   SMTP Server:    &email_smtp
PROMPT

-- ============================================================================
-- SECTION 1: SYSTEM PASSWORDS (ENCODED)
-- ============================================================================

PROMPT [1/5] Configuring system passwords...

MERGE INTO server_info t
USING (SELECT 'PASSWORD_SYS' AS varname, '&password_sys_encoded' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

MERGE INTO server_info t
USING (SELECT 'PASSWORD_CONTAFIN_ORACLE' AS varname, '&password_contafin_encoded' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

-- ============================================================================
-- SECTION 2: LOCAL PATHS
-- ============================================================================

PROMPT [2/5] Configuring local paths...

MERGE INTO server_info t
USING (SELECT 'ROAUPDATEPATH' AS varname, '&roaupdate_path' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

MERGE INTO server_info t
USING (SELECT 'SQLPLUSPATH' AS varname, '&sqlplus_path' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

MERGE INTO server_info t
USING (SELECT 'POWERSHELLPATH' AS varname, 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

-- ============================================================================
-- SECTION 3: UPDATE CONFIGURATION
-- ============================================================================

PROMPT [3/5] Configuring update settings...

-- Enable prerequisites check
MERGE INTO server_info t
USING (SELECT 'UPDATEPREREQ' AS varname, '1' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

-- Use PowerShell for downloads (more reliable than UTL_HTTP)
MERGE INTO server_info t
USING (SELECT 'POWERSHELLDOWNLOAD' AS varname, '1' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

-- PowerShell download timeout (seconds)
MERGE INTO server_info t
USING (SELECT 'POWERSHELLTIMEOUT' AS varname, '30' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

-- ============================================================================
-- SECTION 4: EMAIL CONFIGURATION
-- ============================================================================

PROMPT [4/5] Configuring email settings...

-- Sender address
MERGE INTO server_info t
USING (SELECT 'EMAIL_FROM' AS varname, '&email_from' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

-- Recipient address
MERGE INTO server_info t
USING (SELECT 'EMAIL_TO' AS varname, '&email_to' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

-- CC (empty by default)
MERGE INTO server_info t
USING (SELECT 'EMAIL_CC' AS varname, '' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

-- SMTP Server
MERGE INTO server_info t
USING (SELECT 'EMAIL_SMTP' AS varname, '&email_smtp' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

-- SMTP Port
MERGE INTO server_info t
USING (SELECT 'EMAIL_PORT' AS varname, '25' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

-- Email method (UTL_SMTP is more compatible than UTL_MAIL)
MERGE INTO server_info t
USING (SELECT 'EMAIL_METHOD' AS varname, 'UTL_SMTP' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

-- SMTP authentication username
MERGE INTO server_info t
USING (SELECT 'EMAIL_USERNAME' AS varname, '&email_username' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

-- SMTP authentication password (stored as plain text - encode if needed)
MERGE INTO server_info t
USING (SELECT 'EMAIL_PASSWORD' AS varname, '&email_password' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

-- ============================================================================
-- SECTION 5: UPDATE SERVER URLs (ROMFAST Standard)
-- ============================================================================

PROMPT [5/5] Configuring update server URLs...

-- Main update server URL
MERGE INTO server_info t
USING (SELECT 'UPD_URL_APP' AS varname, 'https://update.romfast.ro/roa/' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

-- Backup update server URL
MERGE INTO server_info t
USING (SELECT 'UPD_URL_APP_BACKUP' AS varname, 'https://update2.romfast.ro/roa/' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

-- Version check URL
MERGE INTO server_info t
USING (SELECT 'UPD_URL_VERSION' AS varname, 'https://update.romfast.ro/version.txt' AS varvalue FROM dual) s
ON (t.varname = s.varname)
WHEN MATCHED THEN UPDATE SET t.varvalue = s.varvalue
WHEN NOT MATCHED THEN INSERT (varname, varvalue) VALUES (s.varname, s.varvalue);

COMMIT;

-- ============================================================================
-- VERIFICATION
-- ============================================================================

PROMPT
PROMPT ========================================
PROMPT SERVER_INFO Configuration Verification
PROMPT ========================================
PROMPT

PROMPT Current SERVER_INFO values (passwords hidden):
SELECT varname,
       CASE
           WHEN UPPER(varname) LIKE '%PASSWORD%' THEN '***HIDDEN***'
           ELSE varvalue
       END AS varvalue
FROM server_info
WHERE varname IN (
    'PASSWORD_SYS', 'PASSWORD_CONTAFIN_ORACLE',
    'ROAUPDATEPATH', 'SQLPLUSPATH', 'POWERSHELLPATH',
    'UPDATEPREREQ', 'POWERSHELLDOWNLOAD', 'POWERSHELLTIMEOUT',
    'EMAIL_FROM', 'EMAIL_TO', 'EMAIL_CC', 'EMAIL_SMTP', 'EMAIL_PORT',
    'EMAIL_METHOD', 'EMAIL_USERNAME', 'EMAIL_PASSWORD',
    'UPD_URL_APP', 'UPD_URL_APP_BACKUP', 'UPD_URL_VERSION'
)
ORDER BY varname;

PROMPT
PROMPT ========================================
PROMPT SERVER_INFO Initialization Complete
PROMPT ========================================
PROMPT
