#!/bin/bash

#############################################################################
# Server Toolkit - Diagnostic Report Generator
# Collects system info for troubleshooting
#############################################################################

OUTPUT_FILE="/tmp/toolkit-diagnostic-$(date +%Y%m%d_%H%M%S).txt"

echo "Generating diagnostic report..."
echo "This may take a moment..."
echo ""

{
    echo "========================================================================="
    echo "SERVER TOOLKIT DIAGNOSTIC REPORT"
    echo "Generated: $(date)"
    echo "========================================================================="
    echo ""

    echo "--- BASIC SYSTEM INFO ---"
    echo "Hostname: $(hostname)"
    echo "Kernel: $(uname -r)"
    echo "OS: $(cat /etc/os-release 2>/dev/null | grep "PRETTY_NAME" | cut -d= -f2 | tr -d '"')"
    echo "Uptime: $(uptime -p)"
    echo ""

    echo "--- TOOLKIT INSTALLATION ---"
    echo "Toolkit directory: /root/server-toolkit"
    echo "Directory exists: $([ -d /root/server-toolkit ] && echo "YES" || echo "NO")"
    echo ""
    echo "Library files:"
    ls -lh /root/server-toolkit/lib/*.sh 2>/dev/null || echo "  ERROR: Library files not found!"
    echo ""

    echo "--- CONTROL PANEL DETECTION ---"
    if [ -f "/usr/local/cpanel/version" ]; then
        echo "Control Panel: cPanel"
        echo "Version: $(cat /usr/local/cpanel/version)"
    elif [ -f "/usr/local/psa/version" ]; then
        echo "Control Panel: Plesk"
        echo "Version: $(cat /usr/local/psa/version | head -1)"
    elif [ -d "/usr/local/interworx" ]; then
        echo "Control Panel: InterWorx"
    else
        echo "Control Panel: None (Standalone)"
    fi
    echo ""

    echo "--- ENVIRONMENT VARIABLES ---"
    echo "SYS_* variables currently set:"
    env | grep "^SYS_" | sort || echo "  None found"
    echo ""
    echo "TOOLKIT_* variables:"
    env | grep "^TOOLKIT_" | sort || echo "  None found"
    echo ""

    echo "--- TEST: DOMAIN DETECTION ---"
    if [ -f "/root/server-toolkit/test-domain-detection.sh" ]; then
        bash /root/server-toolkit/test-domain-detection.sh 2>&1
    else
        echo "  ERROR: test-domain-detection.sh not found!"
    fi
    echo ""

    echo "--- USER/DOMAIN FILES ---"
    echo "cPanel user files:"
    cpanel_users_dir="${SYS_CPANEL_USERS_DIR:-/var/cpanel/users}"
    echo "  $cpanel_users_dir: $(ls "$cpanel_users_dir" 2>/dev/null | wc -l) files"
    echo "  /etc/trueuserdomains: $([ -f /etc/trueuserdomains ] && wc -l < /etc/trueuserdomains || echo "NOT FOUND") lines"
    echo "  /etc/userdatadomains: $([ -f /etc/userdatadomains ] && wc -l < /etc/userdatadomains || echo "NOT FOUND") lines"
    echo ""

    echo "--- CACHE FILES ---"
    echo "Reference database:"
    ls -lh /root/server-toolkit/.sysref* 2>/dev/null || echo "  No cache files"
    echo ""
    echo "Temp directories:"
    ls -ld /tmp/server-toolkit-* 2>/dev/null || echo "  No temp directories"
    echo ""

    echo "--- PROCESS INFO ---"
    echo "Running launcher processes:"
    ps aux | grep "[l]auncher.sh" || echo "  None running"
    echo ""

    echo "--- LIBRARY SYNTAX CHECK ---"
    for lib in /root/server-toolkit/lib/*.sh; do
        if bash -n "$lib" 2>/dev/null; then
            echo "  ✓ $(basename "$lib") - syntax OK"
        else
            echo "  ✗ $(basename "$lib") - SYNTAX ERROR!"
            bash -n "$lib" 2>&1 | sed 's/^/    /'
        fi
    done
    echo ""

    echo "--- DISK SPACE ---"
    df -h / | tail -1
    echo ""
    echo "Log directory size:"
    if [ -d "/var/log/apache2/domlogs" ]; then
        du -sh /var/log/apache2/domlogs 2>/dev/null
    elif [ -d "/usr/local/apache/domlogs" ]; then
        du -sh /usr/local/apache/domlogs 2>/dev/null
    else
        echo "  Log directory not found"
    fi
    echo ""

    echo "--- CSF/FIREWALL STATUS ---"
    if command -v csf >/dev/null 2>&1; then
        echo "CSF installed: YES"
        echo "Version: $(csf -v 2>/dev/null | head -1)"
    else
        echo "CSF installed: NO"
    fi
    echo ""

    echo "--- RECENT ERRORS (if any) ---"
    echo "Checking for common error patterns in toolkit logs..."
    grep -i "error\|fail\|no such file" /tmp/bot_analysis_*.txt 2>/dev/null | tail -10 || echo "  No recent errors found"
    echo ""

    echo "========================================================================="
    echo "END OF DIAGNOSTIC REPORT"
    echo "========================================================================="
    echo ""
    echo "If sharing this report, review it first to remove any sensitive info!"

} > "$OUTPUT_FILE" 2>&1

echo "Diagnostic report saved to: $OUTPUT_FILE"
echo ""
echo "To view:"
echo "  cat $OUTPUT_FILE"
echo ""
echo "To share:"
echo "  cat $OUTPUT_FILE | less"
