#!/bin/bash
# Verify guest agent configuration after fix
# Run on Proxmox node: root@ml110-01

VMID=100

echo "=========================================="
echo "Guest Agent Verification for VM $VMID"
echo "=========================================="
echo ""

# Method 1: Check via qm config
echo "Method 1: qm config"
echo "--------------------------------------"
AGENT_QM=$(qm config $VMID | grep '^agent:' || echo "")
if [ -z "$AGENT_QM" ]; then
    echo "❌ NOT SET via qm config"
else
    echo "✅ Found: $AGENT_QM"
fi
echo ""

# Method 2: Check config file directly
echo "Method 2: Config file"
echo "--------------------------------------"
CONFIG_FILE="/etc/pve/qemu-server/${VMID}.conf"
if [ -f "$CONFIG_FILE" ]; then
    AGENT_FILE=$(grep '^agent:' "$CONFIG_FILE" || echo "")
    if [ -z "$AGENT_FILE" ]; then
        echo "❌ NOT SET in config file"
        echo ""
        echo "Config file location: $CONFIG_FILE"
        echo "First 20 lines of config:"
        head -20 "$CONFIG_FILE"
    else
        echo "✅ Found in file: $AGENT_FILE"
    fi
else
    echo "❌ Config file not found: $CONFIG_FILE"
fi
echo ""

# Summary
echo "=========================================="
echo "Summary"
echo "=========================================="
if [ -n "$AGENT_QM" ] || [ -n "$AGENT_FILE" ]; then
    echo "✅ Guest agent IS configured"
    if [ -n "$AGENT_QM" ]; then
        echo "   - Verified via qm config: $AGENT_QM"
    fi
    if [ -n "$AGENT_FILE" ]; then
        echo "   - Verified in config file: $AGENT_FILE"
    fi
    echo ""
    echo "Next steps:"
    echo "  1. If VM is running, restart it to apply:"
    echo "     qm shutdown $VMID && sleep 5 && qm start $VMID"
    echo ""
    echo "  2. Once VM boots, verify guest agent service inside VM:"
    echo "     qm guest exec $VMID -- systemctl status qemu-guest-agent"
else
    echo "❌ Guest agent is NOT configured"
    echo ""
    echo "Try the fix again:"
    echo "  qm set $VMID --agent 1"
    echo "  echo 'agent: 1' >> $CONFIG_FILE"
    echo "  qm config $VMID | grep '^agent:'"
fi
echo ""

