use std::path::Path;
use anyhow::{Context, Result};
use super::Installed;
pub const SETTINGS: &str = ".clinerules/hooks/PreToolUse";
const SCRIPT: &str = r#"#!/bin/sh
# Written by `ralon hook install`. Safe to delete; safe to regenerate.
#
# Refuses any tool call that names a path agent.lock protects. `exec` hands the
# request straight to ralon, so its JSON and its exit code are Cline's answer.
exec ralon hook check
"#;
pub fn install(root: &Path, dry_run: bool) -> Result<Installed> {
let path = root.join(SETTINGS);
let replaced = path.is_file();
if dry_run {
print!("{SCRIPT}");
return Ok(Installed { path, replaced });
}
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)
.with_context(|| format!("failed to create {}", parent.display()))?;
}
std::fs::write(&path, SCRIPT).with_context(|| format!("failed to write {}", path.display()))?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755))
.with_context(|| format!("failed to make {} executable", path.display()))?;
}
Ok(Installed { path, replaced })
}