#!/bin/bash

#############################################################################
# Control Panel Specific Paths
# Derives panel-specific configuration and data directories
# Must be sourced AFTER lib/system-detect.sh has set SYS_* variables
#############################################################################

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

#############################################################################
# CPANEL SPECIFIC PATHS
#############################################################################

derive_cpanel_paths() {
    export SYS_CPANEL_VERSION_FILE="/usr/local/cpanel/version"
    export SYS_CPANEL_BIN_DIR="/usr/local/cpanel/bin"
    export SYS_CPANEL_SCRIPTS_DIR="/usr/local/cpanel/scripts"
    export SYS_CPANEL_LOGS_DIR="/usr/local/cpanel/logs"
    export SYS_CPANEL_ACCESS_LOG="/usr/local/cpanel/logs/access_log"
    export SYS_CPANEL_ERROR_LOG="/usr/local/cpanel/logs/error_log"
    export SYS_CPANEL_LOGIN_LOG="/usr/local/cpanel/logs/login_log"

    export SYS_CPANEL_USERS_DIR="/var/cpanel/users"
    export SYS_CPANEL_USERDATA_DIR="/var/cpanel/userdata"
    export SYS_CPANEL_MAINIP_FILE="/var/cpanel/mainip"
    export SYS_CPANEL_UPDATELOGS_DIR="/var/cpanel/updatelogs"
    export SYS_CPANEL_HULK_DB="/var/cpanel/hulkd/cphulk.sqlite"
    export SYS_CPANEL_HULK_CTL="/usr/local/cpanel/bin/cphulk_pam_ctl"
    export SYS_CPANEL_HULK_WHITELIST="/usr/local/cpanel/scripts/cphulkdwhitelist"

    export SYS_CPANEL_PHP_DIR="/usr/local/php"
    export SYS_CPANEL_PHP_LOG="/usr/local/php/lib/php.log"

    # Domain logs directory (varies by Apache setup)
    if [ -d "/var/log/apache2/domlogs" ]; then
        export SYS_CPANEL_DOMAIN_LOGS="/var/log/apache2/domlogs"
    elif [ -d "/usr/local/apache/domlogs" ]; then
        export SYS_CPANEL_DOMAIN_LOGS="/usr/local/apache/domlogs"
    else
        export SYS_CPANEL_DOMAIN_LOGS="/var/log/apache2/domlogs"
    fi
}

#############################################################################
# PLESK SPECIFIC PATHS
#############################################################################

derive_plesk_paths() {
    export SYS_PLESK_VERSION_FILE="/usr/local/psa/version"
    export SYS_PLESK_BIN_DIR="/usr/local/psa/bin"
    export SYS_PLESK_LOGS_DIR="/var/log/plesk"

    export SYS_PLESK_VHOSTS_BASE="/var/www/vhosts"
    export SYS_PLESK_CONFIG_DIR="/var/lib/psa/db"

    # Determine Plesk log structure version
    if [ -d "/var/www/vhosts/system" ]; then
        # Plesk 18.0.50+
        export SYS_PLESK_LOG_STRUCTURE="new"
        export SYS_PLESK_VHOSTS_LOGS_BASE="/var/www/vhosts/system"
    else
        # Plesk < 18.0.50
        export SYS_PLESK_LOG_STRUCTURE="old"
        export SYS_PLESK_VHOSTS_LOGS_BASE="/var/www/vhosts"
    fi
}

#############################################################################
# INTERWORX SPECIFIC PATHS
#############################################################################

derive_interworx_paths() {
    export SYS_INTERWORX_VERSION_FILE="/etc/interworx/iworx.ini"
    export SYS_INTERWORX_BIN_DIR="/home/interworx/bin"
    export SYS_INTERWORX_LOGS_DIR="/home/interworx/var/log"
    export SYS_INTERWORX_IWORX_LOG="/home/interworx/var/log/iworx.log"
    export SYS_INTERWORX_SITEWORX_LOG="/home/interworx/var/log/siteworx.log"

    export SYS_INTERWORX_HOME="/home/interworx"
    export SYS_INTERWORX_CHROOT_BASE="/chroot/home"
}

#############################################################################
# STANDALONE PATHS (NO CONTROL PANEL)
#############################################################################

derive_standalone_paths() {
    # No panel-specific paths
    export SYS_STANDALONE_APACHE_CONFIG="/etc/httpd/conf"
    export SYS_STANDALONE_DOMAIN_BASE="/var/www"
}

#############################################################################
# COMMON PANEL TOOL PATHS
#############################################################################

derive_common_panel_tools() {
    # Tools that might exist on multiple panels
    export SYS_PANEL_TOOL_NGINX="/usr/local/cpanel/scripts/ea-nginx"
    export SYS_PANEL_TOOL_CLOUDFLARE="/usr/local/cpanel/bin/cloudflare"
    export SYS_PANEL_TOOL_LETSENCRYPT="/usr/local/cpanel/scripts/new_ssl"
}

#############################################################################
# MAIN DERIVATION FUNCTION
#############################################################################

derive_all_control_panel_paths() {
    case "$SYS_CONTROL_PANEL" in
        cpanel)
            derive_cpanel_paths
            ;;
        plesk)
            derive_plesk_paths
            ;;
        interworx)
            derive_interworx_paths
            ;;
        *)
            derive_standalone_paths
            ;;
    esac

    # Common tools (check if they exist)
    derive_common_panel_tools
}

# Auto-run if sourced with detection complete
if [ -n "${SYS_DETECTION_COMPLETE:-}" ]; then
    derive_all_control_panel_paths
fi
