#!/usr/bin/env bash
# Check Besu Configuration and Enable DEBUG API
# Can be run on RPC node or via SSH

set -euo pipefail

RPC_IP="${1:-192.168.11.250}"
SSH_PASSWORD="${SSH_PASSWORD:-${2:-}}"
if [ -z "${SSH_PASSWORD}" ]; then
    echo "ERROR: SSH_PASSWORD is required. Pass it as an argument or export SSH_PASSWORD in the environment." >&2
    echo "       Hardcoded default removed for security; see docs/SECURITY.md." >&2
    exit 2
fi

CONFIG_FILE="${3:-/etc/besu/config-rpc-core.toml}"

echo "╔══════════════════════════════════════════════════════════════╗"
echo "║     CHECKING BESU CONFIGURATION                              ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""

# Check if running locally or via SSH
if [ "$RPC_IP" = "localhost" ] || [ "$RPC_IP" = "127.0.0.1" ]; then
    # Running locally on RPC node
    echo "Checking local configuration..."
    if [ -f "$CONFIG_FILE" ]; then
        echo "✅ Config file found: $CONFIG_FILE"
        echo ""
        echo "Current rpc-http-api setting:"
        grep "rpc-http-api" "$CONFIG_FILE" | head -1
        echo ""
        
        if grep -q "rpc-http-api" "$CONFIG_FILE" | grep -q "DEBUG"; then
            echo "✅ DEBUG API is already enabled"
        else
            echo "❌ DEBUG API is NOT enabled"
            echo ""
            echo "To enable, run:"
            echo "  sed -i 's/rpc-http-api=\[\"ETH\",\"NET\",\"WEB3\",\"TXPOOL\",\"QBFT\",\"ADMIN\"\]/rpc-http-api=[\"ETH\",\"NET\",\"WEB3\",\"TXPOOL\",\"QBFT\",\"ADMIN\",\"DEBUG\",\"TRACE\"]/g' $CONFIG_FILE"
            echo "  systemctl restart besu-rpc"
        fi
    else
        echo "❌ Config file not found: $CONFIG_FILE"
        echo ""
        echo "Available config files:"
        ls -la /etc/besu/*.toml 2>/dev/null || echo "No config files found"
    fi
else
    # Running via SSH
    if ! command -v sshpass >/dev/null 2>&1; then
        echo "⚠️  sshpass not installed. Installing..."
        sudo apt-get update -qq && sudo apt-get install -y sshpass 2>/dev/null || {
            echo "❌ Cannot install sshpass automatically"
            exit 1
        }
    fi
    
    echo "Checking remote configuration on $RPC_IP..."
    echo ""
    
    CONFIG_CONTENT=$(sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 \
        root@"$RPC_IP" \
        "cat $CONFIG_FILE 2>/dev/null || echo 'FILE_NOT_FOUND'" 2>&1)
    
    if echo "$CONFIG_CONTENT" | grep -q "FILE_NOT_FOUND"; then
        echo "❌ Config file not found: $CONFIG_FILE"
        echo ""
        echo "Available config files:"
        sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 \
            root@"$RPC_IP" \
            "ls -la /etc/besu/*.toml 2>/dev/null || echo 'No config files found'"
    else
        echo "✅ Config file found"
        echo ""
        echo "Current rpc-http-api setting:"
        echo "$CONFIG_CONTENT" | grep "rpc-http-api" | head -1
        echo ""
        
        if echo "$CONFIG_CONTENT" | grep "rpc-http-api" | grep -q "DEBUG"; then
            echo "✅ DEBUG API is already enabled"
        else
            echo "❌ DEBUG API is NOT enabled"
            echo ""
            echo "To enable, SSH into the node and run:"
            echo "  sed -i 's/rpc-http-api=\[\"ETH\",\"NET\",\"WEB3\",\"TXPOOL\",\"QBFT\",\"ADMIN\"\]/rpc-http-api=[\"ETH\",\"NET\",\"WEB3\",\"TXPOOL\",\"QBFT\",\"ADMIN\",\"DEBUG\",\"TRACE\"]/g' $CONFIG_FILE"
            echo "  systemctl restart besu-rpc"
        fi
    fi
fi

echo ""

