#!/usr/bin/env herodo
// Complete Device Hot-Plug Demo
// Demonstrates adding and removing devices at runtime

// ============================================================================
// 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";
let extra_disk = "/path/to/extra-disk.img";  // For hot-plug demo
// ============================================================================

print("╔════════════════════════════════════════════════════════════╗");
print("║  Device Hot-Plug Demo                                     ║");
print("╚════════════════════════════════════════════════════════════╝");
print("");

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

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

// Create basic VM
let vm = VMBuilder()
    .kernel(kernel_path)
    .initramfs(initramfs_path)
    .cmdline("console=ttyS0 root=/dev/vda1")
    .cpus(2)
    .memory(2048)
    .virtio_blk(disk_path, false)
    .serial("tty")
    .build();

print("Creating VM...");
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-plug a disk
print("🔌 Hot-plugging disk device...");
let disk = DeviceBuilder("hotplug-disk1")
    .disk("/tmp/hotplug-disk.img", false);
let disk_id = add_device(handle, disk);
print("✓ Disk hot-plugged: " + disk_id);
print("");

sleep(2000);

// Hot-plug a network device
print("🔌 Hot-plugging network device...");
let net = DeviceBuilder("hotplug-net1")
    .net("tap-hotplug");
let net_id = add_device(handle, net);
print("✓ Network hot-plugged: " + net_id);
print("");

sleep(2000);

// Hot-unplug the disk
print("🔌 Hot-unplugging disk device...");
remove_device(handle, disk_id);
print("✓ Disk hot-unplugged");
print("");

sleep(2000);

// Hot-unplug the network
print("🔌 Hot-unplugging network device...");
remove_device(handle, net_id);
print("✓ Network hot-unplugged");
print("");

print("╔════════════════════════════════════════════════════════════╗");
print("║  Device Hot-Plug Demo Complete                            ║");
print("╚════════════════════════════════════════════════════════════╝");
print("");

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