//! Fully hands-off KVM bake: from an OCI ref to a booted, exec-able microVM
//! with ZERO asset paths. Kernel, busybox, and agent are sourced automatically
//! from the bundled `supermachine-kernel` crate via `Image::bake_kvm_auto`.
//!
//! Usage:
//!   kvm_bake_auto <image_ref> <dest_dir>     e.g. kvm_bake_auto alpine /tmp/vm

#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
fn main() {
    use std::time::Duration;
    let args: Vec<String> = std::env::args().collect();
    let image_ref = args
        .get(1)
        .expect("usage: kvm_bake_auto <image_ref> <dest_dir>");
    let dest = args.get(2).expect("dest arg");

    eprintln!("=== bake_kvm_auto({image_ref}) — no kernel/agent/busybox paths ===");
    let image = supermachine::Image::bake_kvm_auto(image_ref, dest).expect("bake_kvm_auto");

    eprintln!("=== baked; starting VM ===");
    let vm = image
        .start(&supermachine::VmConfig::new())
        .expect("Image::start");
    std::thread::sleep(Duration::from_millis(6000));

    let out = vm
        .exec_builder()
        .argv(["/bin/cat", "/etc/os-release"])
        .output()
        .expect("exec cat");
    eprintln!(
        "=== IN-CONTAINER /etc/os-release: success={} contents={:?} ===",
        out.success(),
        String::from_utf8_lossy(&out.stdout).trim_end()
    );

    let w = vm
        .exec_builder()
        .argv([
            "/bin/sh",
            "-c",
            "echo sm-write-ok > /root/probe && cat /root/probe",
        ])
        .output()
        .expect("exec write");
    eprintln!(
        "=== IN-CONTAINER write test: success={} out={:?} ===",
        w.success(),
        String::from_utf8_lossy(&w.stdout).trim_end()
    );

    vm.stop().expect("stop");
    eprintln!("=== done ===");
}

#[cfg(not(all(target_os = "linux", target_arch = "x86_64")))]
fn main() {
    eprintln!("kvm_bake_auto is Linux/x86_64 only");
}
