#!/bin/bash
# Verify and start VM 100 on Proxmox node
# Run this script on Proxmox node: root@ml110-01

set -e

VMID=100
VM_NAME="basic-vm-001"

echo "=========================================="
echo "VM 100 Verification and Start Script"
echo "=========================================="
echo ""

# Step 1: Check VM status
echo "Step 1: Checking VM status..."
echo "--------------------------------------"
qm status $VMID
echo ""

# Step 2: Check boot order
echo "Step 2: Checking boot order..."
echo "--------------------------------------"
BOOT_ORDER=$(qm config $VMID | grep '^boot:' || echo "")
if [ -z "$BOOT_ORDER" ]; then
    echo "⚠️  Boot order not set - fixing..."
    qm set $VMID --boot order=scsi0
    echo "✅ Boot order set to: order=scsi0"
else
    echo "✅ Boot order: $BOOT_ORDER"
fi
echo ""

# Step 3: Check disk configuration
echo "Step 3: Checking disk configuration..."
echo "--------------------------------------"
SCSI0=$(qm config $VMID | grep '^scsi0:' || echo "")
if [ -z "$SCSI0" ]; then
    echo "❌ ERROR: scsi0 disk not configured!"
    echo "   This is critical - VM cannot boot without disk"
    exit 1
else
    echo "✅ Disk configured: $SCSI0"
    
    # Extract disk name
    DISK_NAME=$(echo "$SCSI0" | sed -n 's/.*local-lvm:\(vm-[0-9]*-disk-[0-9]*\).*/\1/p')
    if [ -n "$DISK_NAME" ]; then
        echo "   Checking if disk exists: $DISK_NAME"
        if lvs | grep -q "$DISK_NAME"; then
            DISK_SIZE=$(lvs | grep "$DISK_NAME" | awk '{print $4}')
            echo "   ✅ Disk exists: $DISK_NAME ($DISK_SIZE)"
        else
            echo "   ❌ ERROR: Disk $DISK_NAME not found in LVM!"
            echo "   Image import may have failed"
            exit 1
        fi
    fi
fi
echo ""

# Step 4: Check cloud-init
echo "Step 4: Checking cloud-init configuration..."
echo "--------------------------------------"
IDE2=$(qm config $VMID | grep '^ide2:' || echo "")
CIUSER=$(qm config $VMID | grep '^ciuser:' || echo "")
if [ -z "$IDE2" ]; then
    echo "⚠️  Cloud-init drive not configured - fixing..."
    qm set $VMID --ide2 local-lvm:cloudinit
    echo "✅ Cloud-init drive configured"
else
    echo "✅ Cloud-init drive: $IDE2"
fi

if [ -z "$CIUSER" ]; then
    echo "⚠️  Cloud-init user not configured - fixing..."
    qm set $VMID --ciuser admin
    echo "✅ Cloud-init user configured"
else
    echo "✅ Cloud-init user: $CIUSER"
fi

IPCONFIG=$(qm config $VMID | grep '^ipconfig0:' || echo "")
if [ -z "$IPCONFIG" ]; then
    echo "⚠️  IP config not set - fixing..."
    qm set $VMID --ipconfig0 ip=dhcp
    echo "✅ IP config set to DHCP"
else
    echo "✅ IP config: $IPCONFIG"
fi
echo ""

# Step 5: Check network
echo "Step 5: Checking network configuration..."
echo "--------------------------------------"
NET0=$(qm config $VMID | grep '^net0:' || echo "")
if [ -z "$NET0" ]; then
    echo "⚠️  Network not configured - fixing..."
    qm set $VMID --net0 virtio,bridge=vmbr0
    echo "✅ Network configured"
else
    echo "✅ Network: $NET0"
fi
echo ""

# Step 6: Check guest agent
echo "Step 6: Checking guest agent..."
echo "--------------------------------------"
AGENT=$(qm config $VMID | grep '^agent:' || echo "")
if [ -z "$AGENT" ]; then
    echo "⚠️  Guest agent not enabled - fixing..."
    qm set $VMID --agent 1
    echo "✅ Guest agent enabled"
else
    echo "✅ Guest agent: $AGENT"
fi
echo ""

# Step 7: Summary
echo "=========================================="
echo "Verification Summary"
echo "=========================================="
echo ""
echo "Configuration checks:"
echo "  ✅ Boot order: $(qm config $VMID | grep '^boot:' || echo 'Not set')"
echo "  ✅ Disk: $(qm config $VMID | grep '^scsi0:' | head -1 || echo 'Not configured')"
echo "  ✅ Cloud-init: $(qm config $VMID | grep '^ide2:' || echo 'Not configured')"
echo "  ✅ Network: $(qm config $VMID | grep '^net0:' || echo 'Not configured')"
echo "  ✅ Guest agent: $(qm config $VMID | grep '^agent:' || echo 'Not enabled')"
echo ""

# Step 8: Start VM
echo "=========================================="
echo "Starting VM $VMID..."
echo "=========================================="
echo ""
echo "Current status:"
qm status $VMID
echo ""
echo "Starting VM..."
qm start $VMID
echo ""
echo "Waiting 5 seconds for VM to initialize..."
sleep 5
echo ""
echo "VM status after start:"
qm status $VMID
echo ""
echo "=========================================="
echo "VM $VMID started!"
echo "=========================================="
echo ""
echo "Monitor with:"
echo "  watch -n 2 'qm status $VMID'"
echo ""
echo "Or from Kubernetes:"
echo "  kubectl get proxmoxvm $VM_NAME -w"
echo ""

