#!/bin/bash
# Setup script for MP-QUIC experiment environment.
# Run on each Linux machine (server + client) before experiments.
set -euo pipefail

echo "╔═══════════════════════════════════════════╗"
echo "║  MP-QUIC Experiment Environment Setup     ║"
echo "╚═══════════════════════════════════════════╝"

# --- OS Check ---
if [[ ! -f /etc/os-release ]]; then
    echo "❌ Not running on Linux. eBPF tracing requires Linux."
    echo "   You can still build and run the Go binaries on this machine."
    exit 1
fi

. /etc/os-release
echo "📋 Detected OS: $PRETTY_NAME"

# --- Install dependencies ---
if [[ "$ID" == "ubuntu" || "$ID" == "debian" ]]; then
    echo ""
    echo "📦 Installing packages..."
    sudo apt-get update -qq
    sudo apt-get install -y -qq \
        bpfcc-tools \
        python3-bpfcc \
        linux-headers-$(uname -r) \
        golang-go \
        iperf3 \
        net-tools \
        iproute2

    # Ensure Python3 BCC bindings work
    python3 -c "from bcc import BPF; print('   ✓ BCC Python bindings OK')" 2>/dev/null || {
        echo "   ⚠  BCC Python import failed. Try: sudo apt install python3-bpfcc"
    }
elif [[ "$ID" == "fedora" || "$ID" == "rhel" || "$ID" == "centos" ]]; then
    echo ""
    echo "📦 Installing packages (DNF)..."
    sudo dnf install -y \
        bcc-tools \
        python3-bcc \
        kernel-devel-$(uname -r) \
        golang \
        iperf3 \
        iproute
else
    echo "⚠  Unsupported distro: $ID"
    echo "   Please install manually: bcc-tools, python3-bcc, golang, linux-headers"
fi

# --- Verify Go ---
echo ""
if command -v go &>/dev/null; then
    echo "✓ Go $(go version | awk '{print $3}')"
else
    echo "❌ Go not found. Install from https://go.dev/dl/"
    exit 1
fi

# --- Kernel config check ---
echo ""
echo "🔍 Checking kernel eBPF support..."
if [[ -d /sys/kernel/debug/tracing ]]; then
    echo "   ✓ debugfs mounted"
else
    echo "   ⚠  debugfs not mounted. Run: sudo mount -t debugfs debugfs /sys/kernel/debug"
fi

if grep -q CONFIG_BPF=y /boot/config-$(uname -r) 2>/dev/null; then
    echo "   ✓ CONFIG_BPF enabled"
else
    echo "   ⚠  Could not verify CONFIG_BPF (may still work)"
fi

# --- Network tuning (optional) ---
echo ""
echo "🔧 Applying network tuning..."
sudo sysctl -w net.core.rmem_max=26214400 2>/dev/null && echo "   ✓ rmem_max=25MB" || true
sudo sysctl -w net.core.wmem_max=26214400 2>/dev/null && echo "   ✓ wmem_max=25MB" || true
sudo sysctl -w net.core.rmem_default=1048576 2>/dev/null || true
sudo sysctl -w net.core.wmem_default=1048576 2>/dev/null || true

echo ""
echo "╔═══════════════════════════════════════════╗"
echo "║  ✓ Setup complete!                        ║"
echo "║                                           ║"
echo "║  Quick start:                             ║"
echo "║    python3 run_experiment.py --duration 10 ║"
echo "║                                           ║"
echo "║  With eBPF (needs sudo):                  ║"
echo "║    python3 run_experiment.py --ebpf        ║"
echo "╚═══════════════════════════════════════════╝"
