//! Directory sharing examples for virtiofsd
//!
//! This example demonstrates practical directory sharing scenarios

// Check if virtiofsd is available
if !virtiofsd_available() {
    print("Error: virtiofsd is not installed");
    throw "virtiofsd not available";
}

print("=== Virtiofsd Directory Sharing Examples ===");

// Example 1: Sharing /tmp directory (always exists)
print("\n1. Sharing /tmp directory for development");

let dev_config = virtiofsd_config()
    .socket_path("/tmp/virtiofsd_dev.sock")
    .shared_dir_with_mount("/tmp", "/home/dev/shared")
    .readonly(false)
    .sandbox(false)
    .thread_pool_size(8)
    .build();

print("Development share configured");
print("  Host: " + dev_config.shared_dir_str);
print("  Guest: " + dev_config.guest_mount_str);
print("  Socket: " + dev_config.socket_path_str);

let daemon = start_virtiofsd_daemon(dev_config);
print("Development daemon started with PID: " + daemon.pid());
print("Daemon running: " + if daemon.is_running() { "true" } else { "false" });

daemon.stop();
print("Development daemon stopped");

// Example 2: Sharing /etc (read-only)
print("\n2. Sharing system configuration (read-only)");

let config_share = virtiofsd_config()
    .socket_path("/tmp/virtiofsd_config.sock")
    .shared_dir_with_mount("/etc", "/mnt/host-config")
    .readonly(true)
    .sandbox(true)
    .cache(true)
    .build();

print("Configuration share configured");
print("  Sharing /etc as read-only");
print("  Guest mount: " + config_share.guest_mount_str);
print("  Sandbox enabled: " + if config_share.sandbox_val { "true" } else { "false" });

let config_daemon = start_virtiofsd_daemon(config_share);
print("Config daemon started with PID: " + config_daemon.pid());
config_daemon.stop();
print("Config daemon stopped");

// Example 3: Sharing /var/log (read-only)
print("\n3. Sharing log files directory");

let log_config = virtiofsd_config()
    .socket_path("/tmp/virtiofsd_logs.sock")
    .shared_dir_with_mount("/var/log", "/mnt/host-logs")
    .readonly(true)
    .cache(true)
    .thread_pool_size(2)
    .build();

print("Log share configured");
print("  Host: " + log_config.shared_dir_str);
print("  Guest: " + log_config.guest_mount_str);

let log_daemon = start_virtiofsd_daemon(log_config);
print("Log daemon started with PID: " + log_daemon.pid());

// Get daemon info as map
let info = log_daemon.to_map();
print("Daemon info:");
for key in info.keys() {
    print("  " + key + ": " + info[key]);
}

log_daemon.stop();
print("Log daemon stopped");

// Example 4: Multiple daemons
print("\n4. Multiple directory sharing");

let shares = [
    #{ name: "tmp", host: "/tmp", guest: "/mnt/tmp", readonly: false },
    #{ name: "etc", host: "/etc", guest: "/mnt/etc", readonly: true },
    #{ name: "var", host: "/var", guest: "/mnt/var", readonly: true }
];

let running_daemons = [];

for share in shares {
    print("Starting share: " + share.name);
    
    let cfg = virtiofsd_config()
        .socket_path("/tmp/virtiofsd_multi_" + share.name + ".sock")
        .shared_dir_with_mount(share.host, share.guest)
        .readonly(share.readonly)
        .cache(true)
        .build();
    
    let d = start_virtiofsd_daemon(cfg);
    print("  Started PID: " + d.pid());
    running_daemons.push(d);
}

print("\nStarted " + running_daemons.len() + " daemons");

// Stop all daemons
print("\nStopping all daemons...");
for d in running_daemons {
    d.stop();
    print("  Stopped daemon");
}

print("\n=== Directory Sharing Examples Complete ===");
print("\nKey takeaways:");
print("- Use read-only mode for system directories");
print("- Disable sandbox for directories needing full access");
print("- Enable caching for better performance");
print("- Use multiple daemons for different directory types");
