#!/usr/bin/env herodo
// Resource Management Demo
// Demonstrates memory and CPU hot-add operations

// ============================================================================
// SETUP REQUIRED: Edit these paths for your environment
// ============================================================================
let kernel_path = "/path/to/vmlinuz";
let initramfs_path = "/path/to/initrd.img";
let disk_path = "/path/to/disk.img";
// ============================================================================

print("╔════════════════════════════════════════════════════════════╗");
print("║  Resource Management Demo                                 ║");
print("╚════════════════════════════════════════════════════════════╝");
print("");

// Initialize runtime
let runtime = RuntimeBuilder()
    .ch_path("/usr/local/bin/cloud-hypervisor")
    .api_socket("/tmp/ch-resources.sock");

if !init_runtime(runtime) {
    print("❌ Failed to initialize runtime. Exiting.");
    return;
}
print("✓ Runtime initialized");
print("");

// Create VM with initial resources
let vm = VMBuilder()
    .kernel(kernel_path)
    .initramfs(initramfs_path)
    .cmdline("console=ttyS0 root=/dev/vda1")
    .cpus(2)
    .memory(1024)  // Start with 1GB
    .virtio_blk(disk_path, false)
    .serial("tty")
    .build();

print("Creating VM with initial resources:");
print("  - CPUs: 2");
print("  - Memory: 1024 MB");
let handle = create_vm(vm);
print("✓ VM created: " + handle.id);
print("");

print("Starting VM...");
start_vm(handle);
print("✓ VM started");
print("");

// Wait for VM to be ready
print("Waiting for VM to stabilize...");
sleep(5000);
print("");

// Hot-add memory
print("💾 Hot-adding memory (1024 MB -> 2048 MB)...");
resize_memory(handle, 2048);
print("✓ Memory resized to 2048 MB");
print("");

sleep(3000);

// Hot-add more memory
print("💾 Hot-adding more memory (2048 MB -> 4096 MB)...");
resize_memory(handle, 4096);
print("✓ Memory resized to 4096 MB");
print("");

sleep(3000);

// Hot-add vCPUs
print("🔧 Hot-adding vCPUs (2 -> 4)...");
add_vcpu(handle, 2);
print("✓ Added 2 vCPUs (total: 4)");
print("");

sleep(3000);

// Hot-add more vCPUs
print("🔧 Hot-adding more vCPUs (4 -> 6)...");
add_vcpu(handle, 2);
print("✓ Added 2 vCPUs (total: 6)");
print("");

print("╔════════════════════════════════════════════════════════════╗");
print("║  Final Resources:                                          ║");
print("║    - CPUs: 6                                               ║");
print("║    - Memory: 4096 MB                                       ║");
print("╚════════════════════════════════════════════════════════════╝");
print("");

// Keep running for a bit
print("VM running with updated resources. Waiting 10 seconds...");
sleep(10000);

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