use anyhow::Result;
use std::path::Path;
pub const PROTOCOL_CONTENT: &str = include_str!("../templates/protocol.md");
pub const TEMPLATE_PLAN: &str = include_str!("../templates/plan.md");
pub const CONFIG_TEMPLATE: &str = include_str!("../templates/cryo.toml");
pub const README_TEMPLATE: &str = include_str!("../templates/README.md");
pub fn protocol_filename(agent_cmd: &str) -> &'static str {
let executable = agent_cmd
.split_whitespace()
.next()
.unwrap_or("")
.rsplit('/')
.next()
.unwrap_or("");
if executable.to_lowercase().contains("claude") {
"CLAUDE.md"
} else {
"AGENTS.md"
}
}
pub fn write_protocol_file(dir: &Path, filename: &str) -> Result<bool> {
let path = dir.join(filename);
if path.exists() {
return Ok(false);
}
std::fs::write(path, PROTOCOL_CONTENT)?;
Ok(true)
}
pub fn find_protocol_file(dir: &Path) -> Option<&'static str> {
if dir.join("CLAUDE.md").exists() {
Some("CLAUDE.md")
} else if dir.join("AGENTS.md").exists() {
Some("AGENTS.md")
} else {
None
}
}
pub fn write_template_plan(dir: &Path) -> Result<bool> {
let path = dir.join("plan.md");
if path.exists() {
return Ok(false);
}
std::fs::write(path, TEMPLATE_PLAN)?;
Ok(true)
}
pub fn write_config_file(dir: &Path, agent_cmd: &str) -> Result<bool> {
let path = dir.join("cryo.toml");
if path.exists() {
return Ok(false);
}
let content = CONFIG_TEMPLATE.replace("{{agent}}", agent_cmd);
std::fs::write(path, content)?;
Ok(true)
}
pub fn write_readme(dir: &Path) -> Result<bool> {
let path = dir.join("README.md");
if path.exists() {
return Ok(false);
}
let project_name = dir
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("cryochamber-project");
let content = README_TEMPLATE.replace("{{project_name}}", project_name);
std::fs::write(path, content)?;
Ok(true)
}