#!/usr/bin/env herodo
// Template Script for Cloud Hypervisor SAL
// Copy this template to create your own scripts

// ============================================================================
// CONFIGURATION - CUSTOMIZE THESE VALUES FOR YOUR ENVIRONMENT
// ============================================================================

// Paths to VM resources (EDIT THESE)
let kernel_path = "/path/to/vmlinuz";           // Path to Linux kernel
let initramfs_path = "/path/to/initrd.img";     // Path to initramfs (optional)
let disk_path = "/path/to/disk.img";            // Path to root disk image
let cmdline = "console=ttyS0 root=/dev/vda1";   // Kernel command line

// VM Resources
let cpus = 2;           // Number of vCPUs
let memory_mb = 2048;   // Memory in MB

// Runtime Configuration
let api_socket = "/tmp/ch-template.sock";  // API socket path (must be unique)

// ============================================================================
// SCRIPT LOGIC - NO NEED TO EDIT BELOW THIS LINE
// ============================================================================

print("╔════════════════════════════════════════════════════════════╗");
print("║  Cloud Hypervisor VM Template                              ║");
print("╚════════════════════════════════════════════════════════════╝");
print("");

// Initialize runtime (auto-detects Cloud Hypervisor binary)
print("🔧 Initializing runtime...");
let runtime = RuntimeBuilder()
    .api_socket(api_socket);

if !init_runtime(runtime) {
    print("❌ Failed to initialize runtime. Exiting.");
    print("");
    print("Troubleshooting:");
    print("  1. Ensure Cloud Hypervisor is installed");
    print("  2. Check the error message above for details");
    return;
}
print("✓ Runtime initialized");
print("");

// Create VM configuration
print("📦 Creating VM...");
let vm = VMBuilder()
    .kernel(kernel_path)
    .initramfs(initramfs_path)
    .cmdline(cmdline)
    .cpus(cpus)
    .memory(memory_mb)
    .virtio_blk(disk_path, false)
    .serial("tty")
    .build();

let handle = create_vm(vm);
print("✓ VM created: " + handle.id);
print("");

// Start VM
print("▶️  Starting VM...");
start_vm(handle);
print("✓ VM started");
print("");

// Wait a bit
print("⏳ VM running for 5 seconds...");
sleep(5000);
print("");

// Stop VM
print("⏹️  Stopping VM...");
stop_vm(handle);
print("✓ VM stopped");
print("");

print("╔════════════════════════════════════════════════════════════╗");
print("║  Script Complete!                                          ║");
print("╚════════════════════════════════════════════════════════════╝");
