//! `cairn-editor [--addr=127.0.0.1:7000] [--store=PATH]` — serve the
//! projectional editor. Thin: arg parse, then [`cairn_editor::serve`].
//! `--store` (or `CAIRN_STORE`) gives a persistent `.cairn` project;
//! omit both for an ephemeral in-memory session.

fn flag(args: &[String], name: &str) -> Option<String> {
    let pfx = format!("--{name}=");
    args.iter().find_map(|a| a.strip_prefix(&pfx).map(str::to_string))
}

fn main() {
    let args: Vec<String> = std::env::args().skip(1).collect();
    let addr = flag(&args, "addr").unwrap_or_else(|| "127.0.0.1:7000".into());
    let store = flag(&args, "store")
        .or_else(|| std::env::var("CAIRN_STORE").ok());
    eprintln!(
        "cairn-editor on http://{addr} (store: {})",
        store.as_deref().unwrap_or("<in-memory>")
    );
    if let Err(e) = cairn_editor::serve(&addr, store.as_deref()) {
        eprintln!("cairn-editor: {e}");
        std::process::exit(1);
    }
}
