#!/bin/bash
# Check if qemu-guest-agent is installed inside VM 100
# Run on Proxmox node: root@ml110-01

VMID=100

echo "=========================================="
echo "Checking qemu-guest-agent in VM $VMID"
echo "=========================================="
echo ""

# Step 1: Check VM status
echo "Step 1: VM Status"
echo "--------------------------------------"
VM_STATUS=$(qm status $VMID | awk '{print $2}')
echo "VM Status: $VM_STATUS"
if [ "$VM_STATUS" != "running" ]; then
    echo "⚠️  VM is not running. Start it first:"
    echo "   qm start $VMID"
    exit 1
fi
echo ""

# Step 2: Check guest agent config in Proxmox
echo "Step 2: Guest Agent Configuration (Proxmox)"
echo "--------------------------------------"
AGENT_CONFIG=$(qm config $VMID | grep '^agent:' || echo "")
if [ -z "$AGENT_CONFIG" ]; then
    echo "❌ Guest agent NOT configured in Proxmox"
    echo "   This needs to be set first: qm set $VMID --agent 1"
else
    echo "✅ Guest agent configured: $AGENT_CONFIG"
fi
echo ""

# Step 3: Try to check if package is installed via guest exec
echo "Step 3: Checking if qemu-guest-agent Package is Installed"
echo "--------------------------------------"
echo "Attempting to check via qm guest exec..."
echo ""

# Try to check package installation
PACKAGE_CHECK=$(qm guest exec $VMID -- dpkg -l | grep qemu-guest-agent 2>&1)
EXEC_EXIT_CODE=$?

if [ $EXEC_EXIT_CODE -eq 0 ] && echo "$PACKAGE_CHECK" | grep -q "qemu-guest-agent"; then
    echo "✅ qemu-guest-agent package IS installed"
    echo ""
    echo "Package details:"
    echo "$PACKAGE_CHECK" | grep qemu-guest-agent
    echo ""
    
    # Check if service is running
    echo "Checking service status..."
    SERVICE_STATUS=$(qm guest exec $VMID -- systemctl status qemu-guest-agent --no-pager 2>&1)
    if echo "$SERVICE_STATUS" | grep -q "active (running)"; then
        echo "✅ qemu-guest-agent service IS running"
    elif echo "$SERVICE_STATUS" | grep -q "inactive"; then
        echo "⚠️  qemu-guest-agent service is installed but NOT running"
        echo ""
        echo "To start it:"
        echo "  qm guest exec $VMID -- systemctl enable --now qemu-guest-agent"
    else
        echo "⚠️  Could not determine service status"
        echo "Service status output:"
        echo "$SERVICE_STATUS"
    fi
elif echo "$PACKAGE_CHECK" | grep -q "No QEMU guest agent configured"; then
    echo "❌ Guest agent not configured in Proxmox"
    echo "   Run: qm set $VMID --agent 1"
elif echo "$PACKAGE_CHECK" | grep -q "QEMU guest agent is not running"; then
    echo "⚠️  Guest agent configured but service not running"
    echo "   The package may not be installed, or the service isn't started"
    echo ""
    echo "Try installing via console or SSH:"
    echo "  sudo apt-get update"
    echo "  sudo apt-get install -y qemu-guest-agent"
    echo "  sudo systemctl enable --now qemu-guest-agent"
else
    echo "❌ qemu-guest-agent package is NOT installed"
    echo ""
    echo "Error details:"
    echo "$PACKAGE_CHECK"
    echo ""
    echo "To install, you need to access the VM via:"
    echo "  1. SSH (if you have the IP and SSH access)"
    echo "  2. Proxmox console (qm terminal $VMID or via web UI)"
    echo ""
    echo "Then run inside the VM:"
    echo "  sudo apt-get update"
    echo "  sudo apt-get install -y qemu-guest-agent"
    echo "  sudo systemctl enable --now qemu-guest-agent"
fi
echo ""

# Step 4: Alternative check methods
echo "Step 4: Alternative Check Methods"
echo "--------------------------------------"
echo "If qm guest exec doesn't work, try these:"
echo ""
echo "1. Get VM IP address (if guest agent working):"
echo "   qm guest exec $VMID -- hostname -I"
echo ""
echo "2. Check via SSH (if you have IP and access):"
echo "   ssh admin@<VM_IP> 'dpkg -l | grep qemu-guest-agent'"
echo ""
echo "3. Use Proxmox console:"
echo "   - Open Proxmox web UI"
echo "   - Go to VM 100 > Console"
echo "   - Login and run: dpkg -l | grep qemu-guest-agent"
echo ""
echo "4. Check cloud-init logs (if available):"
echo "   qm guest exec $VMID -- cat /var/log/cloud-init-output.log | grep -i 'qemu\|guest'"
echo ""

# Step 5: Summary
echo "=========================================="
echo "Summary"
echo "=========================================="
if [ -n "$AGENT_CONFIG" ]; then
    echo "✅ Proxmox config: Guest agent enabled"
else
    echo "❌ Proxmox config: Guest agent NOT enabled"
fi

if [ $EXEC_EXIT_CODE -eq 0 ] && echo "$PACKAGE_CHECK" | grep -q "qemu-guest-agent"; then
    echo "✅ Package: qemu-guest-agent IS installed"
    echo "✅ Status: Ready to use"
else
    echo "❌ Package: qemu-guest-agent NOT installed or not accessible"
    echo ""
    echo "Next steps:"
    echo "  1. Ensure agent=1 is set: qm set $VMID --agent 1"
    echo "  2. Install package inside VM (via SSH or console)"
    echo "  3. Enable and start service inside VM"
fi
echo ""

