#!/bin/bash

# Create VM script for Debian Atomic Desktop Bootc Installer
# This creates a real QEMU VM with VNC access

set -e

# Configuration
VM_NAME="debian-atomic-installer-vm"
VM_DISK="vm-disk.qcow2"
VM_MEMORY="4G"
VM_CORES="2"
VNC_PORT="5901"
VNC_DISPLAY=":1"

# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

# Create VM disk
create_vm_disk() {
    log_info "Creating VM disk..."
    if [ -f "$VM_DISK" ]; then
        log_warning "VM disk already exists. Removing it..."
        rm -f "$VM_DISK"
    fi
    
    qemu-img create -f qcow2 "$VM_DISK" 20G
    log_success "VM disk created: $VM_DISK"
}

# Download a minimal Debian ISO for testing
download_debian_iso() {
    log_info "Downloading minimal Debian ISO for testing..."
    
    if [ ! -f "debian-mini.iso" ]; then
        # Download a minimal Debian netinst ISO
        wget -O debian-mini.iso "https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-13.7.0-amd64-netinst.iso"
        log_success "Downloaded Debian netinst ISO"
    else
        log_info "Debian ISO already exists"
    fi
}

# Start the VM with VNC
start_vm() {
    log_info "Starting VM with VNC access..."
    log_info "VNC server will be available at: vnc://localhost:$VNC_PORT"
    log_info "Use a VNC client to connect to: localhost:$VNC_PORT"
    
    # Check if we have an ISO
    if [ ! -f "debian-mini.iso" ]; then
        download_debian_iso
    fi
    
    # Start VM with proper boot order
    sudo qemu-system-x86_64 \
        -name "$VM_NAME" \
        -m "$VM_MEMORY" \
        -smp "$VM_CORES" \
        -enable-kvm \
        -cpu host \
        -machine q35 \
        -drive file="$VM_DISK",format=qcow2,if=virtio \
        -cdrom debian-mini.iso \
        -device virtio-net-pci,netdev=net0 \
        -netdev user,id=net0,hostfwd=tcp::2222-:22 \
        -rtc base=utc \
        -boot order=dc \
        -serial mon:stdio \
        -nographic
}

# Install our installer in the VM
install_in_vm() {
    log_info "Instructions for installing our installer in the VM:"
    echo
    echo "1. Connect to VNC: vnc://localhost:$VNC_PORT"
    echo "2. Install Debian in the VM"
    echo "3. After installation, install our tools:"
    echo "   - Install podman: sudo apt install podman"
    echo "   - Install bootc: Copy from host or install from package"
    echo "   - Install Calamares: sudo apt install calamares"
    echo "4. Test the installer"
    echo
}

# Show help
show_help() {
    echo "Usage: $0 [COMMAND]"
    echo ""
    echo "Commands:"
    echo "  create-disk  - Create VM disk"
    echo "  download-iso - Download Debian ISO"
    echo "  start-vm     - Start VM with VNC access"
    echo "  install      - Show installation instructions"
    echo "  test         - Full test (create disk, download ISO, start VM)"
    echo "  clean        - Clean up VM files"
    echo "  help         - Show this help"
    echo ""
    echo "VNC Access:"
    echo "  After starting the VM, connect to: vnc://localhost:$VNC_PORT"
    echo "  Or use: vncviewer localhost:$VNC_PORT"
}

# Clean up
cleanup() {
    log_info "Cleaning up VM files..."
    rm -f "$VM_DISK"
    rm -f debian-mini.iso
    log_success "Cleanup completed."
}

# Main execution
case "${1:-help}" in
    "create-disk")
        create_vm_disk
        ;;
    "download-iso")
        download_debian_iso
        ;;
    "start-vm")
        if [ ! -f "$VM_DISK" ]; then
            log_warning "VM disk not found. Creating it first..."
            create_vm_disk
        fi
        start_vm
        ;;
    "install")
        install_in_vm
        ;;
    "test")
        create_vm_disk
        download_debian_iso
        start_vm
        ;;
    "clean")
        cleanup
        ;;
    "help"|*)
        show_help
        ;;
esac 