#!/bin/bash

#############################################################################
# System Variables Export - All Platform-Specific Configuration
# Designed to be sourced by scripts to get complete system awareness
# Aggregates all SYS_* variables from detection and derivation files
#############################################################################

# Source guard
if [ -n "${_SYSTEM_VARIABLES_LOADED:-}" ]; then
    return 0
fi
readonly _SYSTEM_VARIABLES_LOADED=1

# Ensure system detection has run (should be done by launcher.sh)
if [ -z "${SYS_DETECTION_COMPLETE:-}" ]; then
    # Fallback: try to source all derivation files
    SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
    for lib_file in system-detect log-paths database-paths service-info control-panel-paths web-server-config firewall-operations security-tools system-authentication; do
        if [ -f "$SCRIPT_DIR/lib/$lib_file.sh" ]; then
            source "$SCRIPT_DIR/lib/$lib_file.sh"
        fi
    done
fi

#############################################################################
# SYSTEM DETECTION VARIABLES (from lib/system-detect.sh)
#############################################################################

export SYS_CONTROL_PANEL
export SYS_CONTROL_PANEL_VERSION
export SYS_OS_TYPE
export SYS_OS_VERSION
export SYS_OS_DISTRO
export SYS_WEB_SERVER
export SYS_WEB_SERVER_VERSION
export SYS_DB_TYPE
export SYS_DB_VERSION
export SYS_MAIL_SYSTEM
export SYS_MAIL_SYSTEM_VERSION
export SYS_FIREWALL
export SYS_FIREWALL_VERSION
export SYS_USER_HOME_BASE
export SYS_LOG_DIR
export SYS_DETECTION_COMPLETE

#############################################################################
# LOG PATH VARIABLES (from lib/log-paths.sh)
#############################################################################

# Web Server Logs
export SYS_LOG_WEB_ACCESS
export SYS_LOG_WEB_ERROR
export SYS_LOG_WEB_DOMAIN_ACCESS
export SYS_LOG_WEB_DOMAIN_ERROR

# Authentication Logs
export SYS_LOG_AUTH
export SYS_LOG_SSH
export SYS_LOG_WTMP
export SYS_LOG_BTMP

# Mail System Logs
export SYS_LOG_MAIL_MAIN
export SYS_LOG_MAIL_REJECT
export SYS_LOG_MAIL_PANIC
export SYS_MAIL_QUEUE_DIR

# Firewall Logs
export SYS_LOG_FIREWALL
export SYS_LOG_FIREWALL_BLOCK

# Control Panel Logs
export SYS_LOG_PANEL
export SYS_LOG_PANEL_ERROR
export SYS_LOG_PANEL_ACCESS

# Database Logs
export SYS_LOG_DB_ERROR
export SYS_LOG_DB_SLOW

# Security Scanner Logs
export SYS_LOG_CLAMAV
export SYS_LOG_MALDET
export SYS_LOG_RKHUNTER
export SYS_LOG_IMUNIFY

# System Logs
export SYS_LOG_SYSTEM
export SYS_LOG_MESSAGES
export SYS_LOG_KERN
export SYS_LOG_AUDIT
export SYS_LOG_PKG_MGR

# PHP Logs
export SYS_LOG_PHP_FPM
export SYS_LOG_PHP_ERROR

# Service Logs
export SYS_LOG_FTP
export SYS_LOG_DNS

#############################################################################
# DATABASE PATH VARIABLES (from lib/database-paths.sh)
#############################################################################

# MySQL/MariaDB Paths
export SYS_DB_SOCKET
export SYS_DB_CONFIG
export SYS_DB_CONFIG_DIR
export SYS_DB_DATA_DIR
export SYS_DB_BINARY
export SYS_DB_TMPDIR
export SYS_DB_PID_FILE

# PostgreSQL Paths
export SYS_PG_SOCKET
export SYS_PG_CONFIG
export SYS_PG_DATA_DIR
export SYS_PG_BINARY

#############################################################################
# SERVICE INFORMATION VARIABLES (from lib/service-info.sh)
#############################################################################

# Web Server Service Info
export SYS_WEB_SERVICE
export SYS_WEB_USER
export SYS_WEB_GROUP
export SYS_WEB_CONFIG_DIR
export SYS_WEB_MODULES_DIR
export SYS_WEB_VHOSTS_DIR
export SYS_WEB_PID_FILE

# Database Service Info
export SYS_DB_SERVICE
export SYS_DB_USER
export SYS_DB_GROUP

# Mail Service Info
export SYS_MAIL_SERVICE
export SYS_MAIL_USER
export SYS_MAIL_GROUP
export SYS_MAIL_CONFIG
export SYS_MAIL_ALIAS_FILE

# SSH/Auth Service Info
export SYS_AUTH_SERVICE
export SYS_AUTH_USER
export SYS_AUTH_CONFIG

# Firewall Service Info
export SYS_FIREWALL_SERVICE
export SYS_FIREWALL_CONFIG
export SYS_FIREWALL_ALLOW
export SYS_FIREWALL_DENY

# Package Manager Info
export SYS_PKG_MANAGER
export SYS_PKG_MANAGER_CMD
export SYS_PKG_MANAGER_UPDATE
export SYS_PKG_MANAGER_INSTALL
export SYS_PKG_MANAGER_REMOVE
export SYS_PKG_MANAGER_UPGRADE

# Init System Info
export SYS_INIT_SYSTEM
export SYS_SERVICE_CMD
export SYS_SERVICE_START
export SYS_SERVICE_STOP
export SYS_SERVICE_RESTART
export SYS_SERVICE_STATUS
export SYS_SERVICE_ENABLE
export SYS_SERVICE_DISABLE

#############################################################################
# CONTROL PANEL SPECIFIC VARIABLES (from lib/control-panel-paths.sh)
#############################################################################

# cPanel Paths
export SYS_CPANEL_VERSION_FILE
export SYS_CPANEL_BIN_DIR
export SYS_CPANEL_SCRIPTS_DIR
export SYS_CPANEL_LOGS_DIR
export SYS_CPANEL_ACCESS_LOG
export SYS_CPANEL_ERROR_LOG
export SYS_CPANEL_LOGIN_LOG
export SYS_CPANEL_USERS_DIR
export SYS_CPANEL_USERDATA_DIR
export SYS_CPANEL_MAINIP_FILE
export SYS_CPANEL_UPDATELOGS_DIR
export SYS_CPANEL_HULK_DB
export SYS_CPANEL_HULK_CTL
export SYS_CPANEL_HULK_WHITELIST
export SYS_CPANEL_PHP_DIR
export SYS_CPANEL_PHP_LOG
export SYS_CPANEL_DOMAIN_LOGS

# Plesk Paths
export SYS_PLESK_VERSION_FILE
export SYS_PLESK_BIN_DIR
export SYS_PLESK_LOGS_DIR
export SYS_PLESK_VHOSTS_BASE
export SYS_PLESK_CONFIG_DIR
export SYS_PLESK_LOG_STRUCTURE
export SYS_PLESK_VHOSTS_LOGS_BASE

# InterWorx Paths
export SYS_INTERWORX_VERSION_FILE
export SYS_INTERWORX_BIN_DIR
export SYS_INTERWORX_LOGS_DIR
export SYS_INTERWORX_IWORX_LOG
export SYS_INTERWORX_SITEWORX_LOG
export SYS_INTERWORX_HOME
export SYS_INTERWORX_CHROOT_BASE

# Common Panel Tools
export SYS_PANEL_TOOL_NGINX
export SYS_PANEL_TOOL_CLOUDFLARE
export SYS_PANEL_TOOL_LETSENCRYPT

#############################################################################
# WEB SERVER CONFIGURATION VARIABLES (from lib/web-server-config.sh)
#############################################################################

# Apache/httpd Configuration
export SYS_APACHE_MAIN_CONFIG
export SYS_APACHE_CONFIG_DIR
export SYS_APACHE_MODS_DIR
export SYS_APACHE_MODS_AVAILABLE_DIR
export SYS_APACHE_SITES_DIR
export SYS_APACHE_SITES_AVAILABLE_DIR
export SYS_APACHE_CONF_DIR
export SYS_APACHE_CONF_AVAILABLE_DIR
export SYS_APACHE_DEFAULT_SITE
export SYS_APACHE_MOD_SSL
export SYS_APACHE_MOD_DEFLATE
export SYS_APACHE_MOD_REWRITE
export SYS_APACHE_CPANEL_INCLUDES
export SYS_APACHE_CPANEL_MAIN_GLOBAL
export SYS_APACHE_CPANEL_VHOST_DIR

# Nginx Configuration
export SYS_NGINX_MAIN_CONFIG
export SYS_NGINX_CONFIG_DIR
export SYS_NGINX_CONF_DIR
export SYS_NGINX_SITES_DIR
export SYS_NGINX_SITES_AVAILABLE_DIR
export SYS_NGINX_DEFAULT_SITE
export SYS_NGINX_FASTCGI_PARAMS
export SYS_NGINX_PROXY_PARAMS

# LiteSpeed Configuration
export SYS_LITESPEED_HOME
export SYS_LITESPEED_CONF_DIR
export SYS_LITESPEED_CONFIG
export SYS_LITESPEED_VHOSTS_DIR
export SYS_LITESPEED_LOGS_DIR

# Security Modules
export SYS_MODSECURITY_CONF
export SYS_MODSECURITY_RULES_DIR
export SYS_MODSECURITY_AUDIT_LOG
export SYS_FAIL2BAN_CONFIG
export SYS_FAIL2BAN_FILTER_DIR
export SYS_FAIL2BAN_ACTION_DIR
export SYS_CSF_CONFIG
export SYS_CSF_ALLOW
export SYS_CSF_DENY
export SYS_CSF_WHITELIST
export SYS_CSF_REGEX

# Caching & Optimization
export SYS_VARNISH_CONFIG
export SYS_VARNISH_CACHE_DIR
export SYS_PACKAGE_CACHE
export SYS_PACKAGE_LISTS
export SYS_PHP_OPCACHE_DIR

# SSL/TLS Certificates
export SYS_SSL_CERT_DIR
export SYS_SSL_KEY_DIR
export SYS_SSL_CONFIG
export SYS_LETSENCRYPT_DIR
export SYS_LETSENCRYPT_LIVE
export SYS_LETSENCRYPT_ARCHIVE
export SYS_CPANEL_SSL_DIR
export SYS_CPANEL_DOMAINS_SSL

#############################################################################
# FIREWALL OPERATION VARIABLES (from lib/firewall-operations.sh)
#############################################################################

# CSF Firewall
export SYS_CSF_ALLOW
export SYS_CSF_DENY
export SYS_CSF_WHITELIST
export SYS_CSF_REGEX
export SYS_CSF_IGNOREAUTO
export SYS_CSF_IGNORE
export SYS_CSF_LOG
export SYS_CSF_QUEUE
export SYS_CSF_BIN
export SYS_CSF_CMD
export SYS_CSF_IP_CMD
export SYS_CSF_BAN_CMD
export SYS_CSF_UNBAN_CMD
export SYS_CSF_ALLOW_CMD

# Firewalld
export SYS_FIREWALLD_CONFIG
export SYS_FIREWALLD_ZONES
export SYS_FIREWALLD_IPSETS
export SYS_FIREWALLD_SERVICES
export SYS_FIREWALLD_LOG
export SYS_FIREWALLD_DB
export SYS_FIREWALLD_BAN_CMD
export SYS_FIREWALLD_UNBAN_CMD
export SYS_FIREWALLD_ALLOW_CMD
export SYS_FIREWALLD_RELOAD
export SYS_FIREWALLD_IPSET_NAME
export SYS_FIREWALLD_IPSET_FILE

# iptables
export SYS_IPTABLES_CONFIG
export SYS_IPTABLES_RULES_DIR
export SYS_IPTABLES_STATE_DIR
export SYS_IPTABLES_LOG
export SYS_IPTABLES_BAN_CMD
export SYS_IPTABLES_UNBAN_CMD
export SYS_IPTABLES_ALLOW_CMD
export SYS_IPTABLES_SAVE
export SYS_IPTABLES_IPSET_NAME
export SYS_IPTABLES_IPSET_LIST
export SYS_IPTABLES_IPSET_CREATE
export SYS_IPTABLES_IPSET_ADD
export SYS_IPTABLES_IPSET_DEL
export SYS_IPTABLES_IPSET_FLUSH

# UFW (Ubuntu Firewall)
export SYS_UFW_CONFIG
export SYS_UFW_BEFORE_RULES
export SYS_UFW_AFTER_RULES
export SYS_UFW_RULES_DIR
export SYS_UFW_LOG
export SYS_UFW_DB
export SYS_UFW_BAN_CMD
export SYS_UFW_UNBAN_CMD
export SYS_UFW_ALLOW_CMD
export SYS_UFW_RELOAD
export SYS_UFW_IPSET_NAME
export SYS_UFW_BEFORE_RULES_CUSTOM

# Imunify Firewall
export SYS_IMUNIFY_CONFIG
export SYS_IMUNIFY_CLI
export SYS_IMUNIFY_LOG
export SYS_IMUNIFY_LOG_MAIN
export SYS_IMUNIFY_DB
export SYS_IMUNIFY_BLOCKLIST
export SYS_IMUNIFY_WHITELIST
export SYS_IMUNIFY_BAN_CMD
export SYS_IMUNIFY_UNBAN_CMD
export SYS_IMUNIFY_ALLOW_CMD
export SYS_IMUNIFY_LIST_BLOCKED
export SYS_IMUNIFY_LIST_ALLOWED

# Plesk Firewall
export SYS_PLESK_FW_CONFIG
export SYS_PLESK_FW_RULES
export SYS_PLESK_FW_LOG
export SYS_PLESK_FW_WHITELIST
export SYS_PLESK_FW_BLACKLIST
export SYS_PLESK_FW_CMD

#############################################################################
# MAIL COMMAND VARIABLES (from lib/service-info.sh)
#############################################################################

export SYS_MAIL_BIN_EXIM
export SYS_MAIL_BIN_POSTFIX
export SYS_MAIL_BIN_SENDMAIL
export SYS_MAIL_SPOOL
export SYS_MAIL_CMD_QUEUE_COUNT
export SYS_MAIL_CMD_QUEUE_LIST
export SYS_MAIL_CMD_QUEUE_RETRY
export SYS_MAIL_CMD_QUEUE_REMOVE
export SYS_MAIL_CMD_TEST_ADDRESS

#############################################################################
# DATABASE COMMAND VARIABLES (from lib/service-info.sh)
#############################################################################

export SYS_DB_CLI_COMMAND
export SYS_DB_DUMP_COMMAND
export SYS_DB_ADMIN_COMMAND
export SYS_DB_CHECK_COMMAND
export SYS_DB_REPAIR_COMMAND
export SYS_DB_OPTIMIZE_COMMAND
export SYS_DB_STATUS_COMMAND
export SYS_DB_SHOW_DATABASES
export SYS_DB_SHOW_TABLES

#############################################################################
# SECURITY TOOLS VARIABLES (from lib/security-tools.sh)
#############################################################################

# Malware Scanners
export SYS_SCANNER_CLAMAV
export SYS_SCANNER_CLAMUPDATE
export SYS_SCANNER_CLAMSCAN
export SYS_SCANNER_CLAMAV_DB
export SYS_SCANNER_CLAMAV_LOG
export SYS_SCANNER_MALDET
export SYS_SCANNER_MALDET_DIR
export SYS_SCANNER_MALDET_QUARANTINE
export SYS_SCANNER_MALDET_LOG
export SYS_SCANNER_RKHUNTER
export SYS_SCANNER_RKHUNTER_CONFIG
export SYS_SCANNER_RKHUNTER_DB
export SYS_SCANNER_RKHUNTER_LOG
export SYS_SCANNER_IMUNIFY
export SYS_SCANNER_IMUNIFY_CONFIG
export SYS_SCANNER_IMUNIFY_DB
export SYS_SCANNER_IMUNIFY_LOG

# Control Panel Security Tools
export SYS_CPANEL_WHMAPI
export SYS_CPANEL_UAPI
export SYS_CPANEL_HULK
export SYS_CPANEL_SCAN_TOOL
export SYS_CPANEL_MALWARE_SCANNER
export SYS_PLESK_API
export SYS_PLESK_ADMIN_API
export SYS_PLESK_EXTENSION_API
export SYS_PLESK_MTA_SCAN
export SYS_INTERWORX_BIN
export SYS_INTERWORX_NODEWORX
export SYS_INTERWORX_SITEWORX

# System Security Tools
export SYS_FAIL2BAN_CLIENT
export SYS_FAIL2BAN_CONFIG
export SYS_FAIL2BAN_JAIL
export SYS_MODSECURITY_ENABLED
export SYS_MODSECURITY_CONF
export SYS_MODSECURITY_RULES
export SYS_MODSECURITY_AUDIT_LOG
export SYS_SELINUX_ENABLED
export SYS_SELINUX_STATUS
export SYS_SELINUX_CONFIG
export SYS_APPARMOR_ENABLED
export SYS_APPARMOR_CONFIG

#############################################################################
# SYSTEM AUTHENTICATION VARIABLES (from lib/system-authentication.sh)
#############################################################################

# System Auth Files
export SYS_AUTH_PASSWD_FILE
export SYS_AUTH_SHADOW_FILE
export SYS_AUTH_GROUP_FILE
export SYS_AUTH_GSHADOW_FILE
export SYS_AUTH_SUDOERS_FILE
export SYS_AUTH_SUDOERS_DIR
export SYS_AUTH_PAM_DIR
export SYS_AUTH_SSH_CONFIG
export SYS_AUTH_HOSTS_ALLOW
export SYS_AUTH_HOSTS_DENY
export SYS_AUTH_CRONTAB_DIR
export SYS_LOG_CRON

# User and Group IDs
export SYS_WEB_UID
export SYS_WEB_GID
export SYS_DB_UID
export SYS_DB_GID
export SYS_MAIL_UID
export SYS_MAIL_GID
export SYS_CPANEL_SYSTEM_UID
export SYS_CPANEL_SYSTEM_GID
export SYS_PLESK_SYSTEM_UID
export SYS_PLESK_SYSTEM_GID
export SYS_INTERWORX_SYSTEM_UID
export SYS_INTERWORX_SYSTEM_GID

#############################################################################
# PHP VERSION PATHS (from lib/service-info.sh derivations)
#############################################################################

# cPanel PHP versions
export SYS_CPANEL_EAPHP_BASE
export SYS_CPANEL_EAPHP_BINARY_PATTERN
export SYS_CPANEL_EAPHP_CONFIG_PATTERN
export SYS_CPANEL_EAPHP_FPM_PATTERN

# Plesk PHP versions
export SYS_PLESK_PHP_BASE
export SYS_PLESK_PHP_BINARY_PATTERN
export SYS_PLESK_FPM_SOCKET_DIR
export SYS_PLESK_LOG_STRUCTURE_VERSION

# InterWorx PHP versions and domain paths
export SYS_INTERWORX_PHP_SYSTEM
export SYS_INTERWORX_PHP_ALT_VERSIONS
export SYS_INTERWORX_DOMAINS_BASE
export SYS_INTERWORX_DOMAIN_HTML
export SYS_INTERWORX_DOMAIN_LOGS
export SYS_INTERWORX_VAR_LOGS_DIR

#############################################################################
# DOMAIN CONFIGURATION ACCESS FILES
#############################################################################

# cPanel domain configuration and mappings
export SYS_CPANEL_USERDATA_DIR
export SYS_CPANEL_DOMAIN_CONFIG_PATTERN
export SYS_CPANEL_TRUEUSERDOMAINS
export SYS_CPANEL_USERDATADOMAINS
export SYS_CPANEL_RETENTIONDOMAINS

#############################################################################
# DOMAIN LOG PATH VARIATIONS
#############################################################################

# cPanel domain logs
export SYS_CPANEL_DOMLOGS_BASE
export SYS_CPANEL_DOMLOGS_PATTERN

# Plesk domain logs (version-dependent)
export SYS_PLESK_DOMLOGS_PATTERN

#############################################################################
# CONVENIENCE FUNCTIONS FOR SCRIPTS
#############################################################################

# Get all available log variables for a specific category
get_log_vars_by_category() {
    local category="$1"
    case "$category" in
        web)
            echo "$SYS_LOG_WEB_ACCESS:$SYS_LOG_WEB_ERROR"
            ;;
        auth)
            echo "$SYS_LOG_AUTH:$SYS_LOG_WTMP:$SYS_LOG_BTMP"
            ;;
        mail)
            echo "$SYS_LOG_MAIL_MAIN:$SYS_LOG_MAIL_REJECT"
            ;;
        firewall)
            echo "$SYS_LOG_FIREWALL"
            ;;
        database)
            echo "$SYS_LOG_DB_ERROR:$SYS_LOG_DB_SLOW"
            ;;
        system)
            echo "$SYS_LOG_SYSTEM:$SYS_LOG_KERN:$SYS_LOG_AUDIT"
            ;;
        php)
            echo "$SYS_LOG_PHP_FPM:$SYS_LOG_PHP_ERROR"
            ;;
        *)
            return 1
            ;;
    esac
}

# Check if a log path exists and is readable
log_exists() {
    local log_var="$1"
    [ -n "$log_var" ] && [ -f "$log_var" ]
}

# Get platform summary
get_platform_summary() {
    cat <<EOF
Control Panel: $SYS_CONTROL_PANEL (v$SYS_CONTROL_PANEL_VERSION)
Operating System: $SYS_OS_TYPE (v$SYS_OS_VERSION)
Web Server: $SYS_WEB_SERVER (v$SYS_WEB_SERVER_VERSION)
Database: $SYS_DB_TYPE (v$SYS_DB_VERSION)
Mail System: $SYS_MAIL_SYSTEM
Firewall: $SYS_FIREWALL
EOF
}

# Restart a service (convenience wrapper)
restart_service() {
    local service="$1"
    if [ "$SYS_INIT_SYSTEM" = "systemd" ]; then
        systemctl restart "$service"
    else
        service "$service" restart
    fi
}

# Check if service is running (convenience wrapper)
is_service_running() {
    local service="$1"
    if [ "$SYS_INIT_SYSTEM" = "systemd" ]; then
        systemctl is-active --quiet "$service"
    else
        service "$service" status >/dev/null 2>&1
    fi
}

# Export all convenience functions
export -f get_log_vars_by_category
export -f log_exists
export -f get_platform_summary
export -f restart_service
export -f is_service_running
export -f firewall_block_ip
export -f firewall_unblock_ip
export -f firewall_is_blocked
export -f firewall_bulk_block_ips
