#!/bin/bash
# Force restart VM 100 to apply guest agent fix
# Run on Proxmox node: root@ml110-01

set -e

VMID=100

echo "=========================================="
echo "Force Restart VM 100 for Guest Agent Fix"
echo "=========================================="
echo ""

# Step 1: Check current status
echo "Step 1: Current VM Status"
echo "--------------------------------------"
qm status $VMID
echo ""

# Step 2: Verify guest agent config
echo "Step 2: Guest Agent Configuration"
echo "--------------------------------------"
AGENT_CONFIG=$(qm config $VMID | grep '^agent:' || echo "")
if [ -z "$AGENT_CONFIG" ]; then
    echo "⚠️  Guest agent NOT configured"
    echo "Setting agent=1..."
    qm set $VMID --agent 1
    echo "✅ Guest agent enabled"
else
    echo "✅ Guest agent configured: $AGENT_CONFIG"
fi
echo ""

# Step 3: Force stop VM
echo "Step 3: Force Stopping VM"
echo "--------------------------------------"
CURRENT_STATUS=$(qm status $VMID | awk '{print $2}')
if [ "$CURRENT_STATUS" = "running" ]; then
    echo "VM is running, attempting graceful shutdown first..."
    qm shutdown $VMID &
    SHUTDOWN_PID=$!
    
    # Wait up to 30 seconds for graceful shutdown
    for i in {1..30}; do
        sleep 1
        STATUS=$(qm status $VMID | awk '{print $2}' 2>/dev/null || echo "unknown")
        if [ "$STATUS" != "running" ]; then
            echo "✅ VM shut down gracefully"
            break
        fi
        if [ $i -eq 30 ]; then
            echo "⚠️  Graceful shutdown timed out, forcing stop..."
            kill $SHUTDOWN_PID 2>/dev/null || true
            qm stop $VMID
            echo "✅ VM force stopped"
        fi
    done
else
    echo "VM is already stopped (status: $CURRENT_STATUS)"
fi
echo ""

# Step 4: Wait and verify stopped
echo "Step 4: Verifying VM is Stopped"
echo "--------------------------------------"
sleep 3
FINAL_STATUS=$(qm status $VMID | awk '{print $2}')
if [ "$FINAL_STATUS" = "stopped" ]; then
    echo "✅ VM is stopped"
else
    echo "⚠️  VM status: $FINAL_STATUS"
    echo "Waiting a bit longer..."
    sleep 5
    FINAL_STATUS=$(qm status $VMID | awk '{print $2}')
    echo "Final status: $FINAL_STATUS"
fi
echo ""

# Step 5: Start VM
echo "Step 5: Starting VM"
echo "--------------------------------------"
if [ "$FINAL_STATUS" = "stopped" ]; then
    echo "Starting VM..."
    qm start $VMID
    echo ""
    echo "Waiting 10 seconds for initialization..."
    sleep 10
    echo ""
    echo "VM status after start:"
    qm status $VMID
else
    echo "⚠️  Cannot start VM - current status: $FINAL_STATUS"
    echo "Please check VM state manually"
fi
echo ""

# Step 6: Instructions for guest agent verification
echo "=========================================="
echo "VM Restarted - Next Steps"
echo "=========================================="
echo ""
echo "Wait 30-60 seconds for VM to fully boot, then verify guest agent:"
echo ""
echo "1. Check guest agent service (once VM has booted):"
echo "   qm guest exec $VMID -- systemctl status qemu-guest-agent"
echo ""
echo "2. If guest agent service is not running, install it inside VM:"
echo "   (You'll need to SSH into the VM or use console)"
echo ""
echo "3. Monitor VM status:"
echo "   watch -n 2 \"qm status $VMID\""
echo ""
echo "4. Check VM IP address (once booted):"
echo "   qm guest exec $VMID -- hostname -I"
echo ""

