use anyhow::Result;
use mimir_core::{scope, Mimir};
pub fn init(sync: bool) -> Result<()> {
let cwd = std::env::current_dir()?;
let root = scope::find_project_root(&cwd).unwrap_or(cwd);
let marker = root.join(".mimir");
let mut m = scope::read_mimir_marker(&root).unwrap_or_default();
let had_id = m.id.is_some();
m.id.get_or_insert_with(scope::new_id);
if sync {
m.sync = true;
}
let mut out = String::new();
if let Some(id) = &m.id {
out.push_str(&format!("id = \"{id}\"\n"));
}
out.push_str(&format!("sync = {}\n", m.sync));
std::fs::write(&marker, out)?;
let mimir = Mimir::open()?;
let _ = mimir.project_for_cwd(&root)?;
let key = scope::portable_key(&root).unwrap_or_default();
println!("project {}", scope::project_name(&root));
println!(
"marker {}{}",
marker.display(),
if had_id { "" } else { " (created)" }
);
println!("key {key}");
println!("sync {}", m.sync);
if m.sync {
println!(
"\nCommit `.mimir` so every clone shares this identity. This project's \
memories will replicate wherever sync is enabled (config.toml `[sync]`)."
);
} else {
println!("\nRe-run with --sync to replicate this project's memories.");
}
Ok(())
}