use anyhow::Result;
use std::fs;
use std::path::Path;
use super::paths;
const SESSION_START_MD: &str = include_str!("../../../../resources/claude/session-start.md");
const SESSION_UPDATE_MD: &str = include_str!("../../../../resources/claude/session-update.md");
const SESSION_NOTE_MD: &str = include_str!("../../../../resources/claude/session-note.md");
const SESSION_END_MD: &str = include_str!("../../../../resources/claude/session-end.md");
const PATINA_REVIEW_MD: &str = include_str!("../../../../resources/claude/patina-review.md");
const WRAPPER_START: &str = "#!/bin/bash\nexec patina session start \"$@\"\n";
const WRAPPER_UPDATE: &str = "#!/bin/bash\nexec patina session update \"$@\"\n";
const WRAPPER_NOTE: &str = "#!/bin/bash\nexec patina session note \"$@\"\n";
const WRAPPER_END: &str = "#!/bin/bash\nexec patina session end \"$@\"\n";
pub fn create_session_scripts(project_path: &Path) -> Result<()> {
let commands_path = paths::get_commands_path(project_path);
let bin_path = paths::get_bin_path(project_path);
fs::create_dir_all(&commands_path)?;
fs::create_dir_all(&bin_path)?;
write_script(&bin_path.join("session-start.sh"), WRAPPER_START)?;
write_script(&bin_path.join("session-update.sh"), WRAPPER_UPDATE)?;
write_script(&bin_path.join("session-note.sh"), WRAPPER_NOTE)?;
write_script(&bin_path.join("session-end.sh"), WRAPPER_END)?;
fs::write(commands_path.join("session-start.md"), SESSION_START_MD)?;
fs::write(commands_path.join("session-update.md"), SESSION_UPDATE_MD)?;
fs::write(commands_path.join("session-note.md"), SESSION_NOTE_MD)?;
fs::write(commands_path.join("session-end.md"), SESSION_END_MD)?;
fs::write(commands_path.join("patina-review.md"), PATINA_REVIEW_MD)?;
Ok(())
}
fn write_script(path: &Path, content: &str) -> Result<()> {
fs::write(path, content)?;
make_executable(path)?;
Ok(())
}
#[cfg(unix)]
fn make_executable(path: &Path) -> Result<()> {
use std::os::unix::fs::PermissionsExt;
let mut perms = fs::metadata(path)?.permissions();
perms.set_mode(0o755);
fs::set_permissions(path, perms)?;
Ok(())
}
#[cfg(not(unix))]
fn make_executable(_path: &Path) -> Result<()> {
Ok(())
}