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

VMID=100

echo "=== Guest Agent Configuration Check for VM $VMID ==="
echo ""

# Check current configuration
echo "1. Current VM Configuration:"
echo "--------------------------------------"
AGENT_CONFIG=$(qm config $VMID 2>&1 | grep '^agent:' || echo "")
if [ -z "$AGENT_CONFIG" ]; then
    echo "   ❌ Guest agent NOT configured"
    echo "   Current config:"
    qm config $VMID 2>&1 | head -20
    echo ""
    echo "   ⚠️  This means the agent was NOT set during VM creation"
    echo "   This could happen if:"
    echo "     - VM was created before the code fix was deployed"
    echo "     - VM creation was interrupted"
    echo "     - Provider version doesn't include the fix"
    echo ""
    echo "2. Fixing guest agent configuration..."
    echo "--------------------------------------"
    qm set $VMID --agent 1
    echo "   ✅ Guest agent enabled"
    echo ""
    echo "3. Verifying fix:"
    echo "--------------------------------------"
    qm config $VMID | grep '^agent:'
    echo ""
    echo "✅ Guest agent is now configured"
else
    echo "   ✅ Guest agent configured: $AGENT_CONFIG"
    echo ""
    echo "2. Full VM Configuration (relevant parts):"
    echo "--------------------------------------"
    qm config $VMID 2>&1 | grep -E '^agent:|^boot:|^scsi0:|^ide2:|^net0:'
    echo ""
    echo "✅ Guest agent is properly configured"
fi

echo ""
echo "=== Summary ==="
echo ""
if [ -z "$AGENT_CONFIG" ]; then
    echo "Status: ❌ Guest agent was NOT configured (now fixed)"
    echo ""
    echo "This indicates:"
    echo "  - VM was created before the code fix was deployed, OR"
    echo "  - VM creation was interrupted before agent was set, OR"
    echo "  - Provider version doesn't include the fix"
    echo ""
    echo "The code SHOULD set agent: 1 in the initial vmConfig (line 317)"
    echo "before creating the VM (line 345), but it appears this didn't happen"
    echo "for VM 100."
else
    echo "Status: ✅ Guest agent is configured"
    echo ""
    echo "The agent was set during VM creation as expected."
fi
echo ""

