#!/bin/bash
# 2520 Jetson环境依赖安装脚本
# Jetson端执行: bash install_jetson_deps.sh
# 如果从PC端SSH执行: ssh nvidia@192.168.1.108 'bash -s' < install_jetson_deps.sh
#
# 说明: 安装Jetson独有Fuzz模块所需的依赖
# 所有依赖可通过Jetson本地APT/PIP安装，无需互联网(需提前配置本地源)
# 如果Jetson有互联网访问则自动安装，否则提供手动安装指南

set -e

echo "========================================="
echo "  2520 - Jetson 依赖安装"
echo "========================================="

# 检查网络
if ping -c 1 -W 2 8.8.8.8 >/dev/null 2>&1; then
    ONLINE=true
    echo "[*] 网络连接正常"
else
    ONLINE=false
    echo "[!] 无互联网连接，将使用本地已安装的包"
fi

# ====== Python工具包 ======
echo ""
echo "[1/5] 安装Python依赖..."

PY_PACKAGES=""
PY_PACKAGES="$PY_PACKAGES python3-pip"

if [ "$ONLINE" = true ]; then
    sudo apt install -y $PY_PACKAGES 2>/dev/null || true
    
    # PIP安装Fuzz模块所需
    # python-can: CAN总线通信
    # pyserial: 串口通信  
    # smbus2: I2C通信
    echo "   安装Python库..."
    pip3 install python-can pyserial smbus2 --quiet 2>&1 | tail -1 || true
else
    echo "   跳过(需要在线安装: python-can, pyserial, smbus2)"
fi

# 检查已安装的Python包
echo "   检查Python包..."
for pkg in can serial smbus2; do
    if python3 -c "import $pkg" 2>/dev/null; then
        echo "   ✓ $pkg"
    else
        echo "   ✗ $pkg (未安装)"
    fi
done

# ====== CAN工具 ======
echo ""
echo "[2/5] 安装CAN工具..."

CAN_TOOLS="can-utils"
if [ "$ONLINE" = true ]; then
    sudo apt install -y $CAN_TOOLS 2>&1 | tail -2 || true
fi

# 检查CAN工具
for tool in candump cangen cansend; do
    if which $tool >/dev/null 2>&1; then
        echo "   ✓ $tool"
    else
        echo "   ✗ $tool (未安装)"
    fi
done

# 加载vcan内核模块
echo "   加载vcan模块..."
sudo modprobe can 2>/dev/null && echo "   ✓ can模块" || echo "   ✗ can模块无法加载"
sudo modprobe can_raw 2>/dev/null && echo "   ✓ can_raw" || echo "   ✗ can_raw"
sudo modprobe vcan 2>/dev/null && echo "   ✓ vcan模块" || echo "   ✗ vcan(可能无硬件CAN)"

# ====== GStreamer + V4L2 ======
echo ""
echo "[3/5] 检查摄像头/多媒体..."

# GStreamer
if gst-launch-1.0 --gst-version >/dev/null 2>&1; then
    echo "   ✓ GStreamer $(gst-launch-1.0 --gst-version 2>&1 | grep -oP '[\d.]+' | head -1)"
else
    echo "   ✗ GStreamer (未安装)"
    if [ "$ONLINE" = true ]; then
        echo "   安装GStreamer..."
        sudo apt install -y gstreamer-1.0 libgstreamer1.0-dev 2>&1 | tail -2 || true
    fi
fi

# V4L2
if v4l2-ctl --version >/dev/null 2>&1; then
    echo "   ✓ v4l2-ctl"
else
    echo "   ✗ v4l2-ctl (未安装)"
fi

# OpenCV (可选, 用于更丰富的摄像头Fuzz)
if python3 -c "import cv2" 2>/dev/null; then
    echo "   ✓ OpenCV-Python"
elif [ "$ONLINE" = true ]; then
    echo "   安装OpenCV..."
    pip3 install opencv-python --quiet 2>&1 | tail -1 || true
fi

# 检查摄像头设备
if [ -e /dev/video0 ]; then
    echo "   ✓ 摄像头设备 (/dev/video0)"
else
    echo "   ✗ 无摄像头设备"
fi

# ====== socat (虚拟串口) ======
echo ""
echo "[4/5] 安装串口工具..."

if which socat >/dev/null 2>&1; then
    echo "   ✓ socat"
else
    if [ "$ONLINE" = true ]; then
        sudo apt install -y socat 2>&1 | tail -2 || true
    fi
fi

# ====== I2C工具 ======
echo ""
echo "[5/5] 检查I2C工具..."

if which i2cdetect >/dev/null 2>&1; then
    echo "   ✓ i2cdetect"
else
    echo "   ✗ i2c-tools (未安装)"
fi

if python3 -c "import smbus2" 2>/dev/null; then
    echo "   ✓ smbus2 (Python I2C库)"
else
    echo "   ✗ smbus2 (未安装)"
fi

# ====== 汇总 ======
echo ""
echo "========================================="
echo "  依赖安装完成"
echo "========================================="
echo ""
echo "✓ = 已安装 | ✗ = 缺失(不影响基础功能)"
echo ""
echo "2520模块可用性:"
echo "  CAN Fuzz:     $( (python3 -c 'import can' 2>/dev/null || which cangen >/dev/null 2>&1) && echo '可用' || echo '部分可用')"
echo "  摄像头Fuzz:   $( [ -e /dev/video0 ] && echo '可用' || echo '不可用(无摄像头)')"
echo "  串口Fuzz:     $( which socat >/dev/null 2>&1 && echo '可用' || echo '部分可用')"
echo "  I2C Fuzz:     $( python3 -c 'import smbus2' 2>/dev/null && echo '可用' || echo '部分可用')"
echo "  Ollama Fuzz:  $( curl -s http://127.0.0.1:11434/api/tags >/dev/null 2>&1 && echo '可用' || echo '未运行')"
echo "  网络Fuzz:     可用(Python标准库)"
