#!/bin/bash
# run-writable.sh  -  is a directory one an unattended run can write, and the
# way a shell script writes there.
#
# Hooks, the runner and the office run some shell scripts outside the run's
# sandbox, on paths below the unattended run root that the run can change. A
# shell `>`, `>>`, `mv` or `mkdir -p` there follows a link the run planted at
# the name or at any directory on the way. Such a write goes through
# scripts/run-write.mjs instead, which pins the directory and refuses a link
# below the run root (lib/safe-write.mjs).
#
#   ma_under_run_root <dir>   dir, or the nearest directory above it that
#                             exists, is the run root or below it: by its
#                             spelling in any case, or by its real path (both
#                             real paths come from the kernel, so their
#                             spellings agree). A relative dir is taken from
#                             $PWD; a directory that exists but cannot be
#                             entered counts as below the root
#   ma_run_writable <dir>     MULTI_AGENT_UNATTENDED=1, or ma_under_run_root
#   ma_run_write <cmd> <path> [args]
#                             node scripts/run-write.mjs <cmd> <path> [args]
#   ma_run_write_why <file>   the refusal reason in the run-write stderr kept
#                             in file, in words
#
# Sourced by bash scripts only: it uses [[ ]] and shopt nocasematch.
#
# Usage:
#   . "$(dirname "$0")/../lib/run-writable.sh"
#   if ma_run_writable "$dir"; then printf '%s\n' "$line" | ma_run_write append "$file"; fi

_MA_RW_HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
_MA_RW_ROOT_REAL=""

ma_under_run_root() {
  local d="$1" root="${MA_UNATTENDED_RUN_ROOT:-$HOME/.multi-agent-unattended}" real up hit=1 was=0
  # A relative path is compared and walked from the working directory, so the
  # walk below always ends at "/".
  case "$d" in /*) ;; *) d="$PWD/$d" ;; esac
  shopt -q nocasematch && was=1
  shopt -s nocasematch
  [[ "$d/" == "$root/"* ]] && hit=0
  [ "$was" -eq 1 ] || shopt -u nocasematch
  [ "$hit" -eq 0 ] && return 0
  if [ -z "$_MA_RW_ROOT_REAL" ]; then
    _MA_RW_ROOT_REAL=$(cd -P -- "$root" 2>/dev/null && pwd -P) || _MA_RW_ROOT_REAL="-"
  fi
  [ "$_MA_RW_ROOT_REAL" = "-" ] && return 1
  while [ -n "$d" ] && [ ! -d "$d" ]; do
    up="${d%/*}"
    [ "$up" = "$d" ] && break
    d="$up"
  done
  d="${d:-/}"
  # A directory that exists but cannot be entered may be one the run made
  # unsearchable to hide where it leads: treat it as the run's.
  real=$(cd -P -- "$d" 2>/dev/null && pwd -P) || return 0
  [[ "$real/" == "$_MA_RW_ROOT_REAL/"* ]]
}

ma_run_writable() {
  [ "${MULTI_AGENT_UNATTENDED:-0}" = "1" ] && return 0
  ma_under_run_root "$1"
}

ma_run_write() {
  local script="$_MA_RW_HERE/../scripts/run-write.mjs"
  [ -f "$script" ] || script="$HOME/.claude/scripts/run-write.mjs"
  node "$script" "$@"
}

# ma_run_write_why <stderr file>: the refusal run-write.mjs wrote there (exit
# 3), in words; a generic line when it named none.
ma_run_write_why() {
  local why
  why=$(sed -n 's/^run-write: refused (\([a-z-]*\)).*/\1/p' "$1" 2>/dev/null | head -n 1)
  case "$why" in
    link) printf 'a link stands at it or on the way to it' ;;
    too-large) printf 'it is larger than run-write reads' ;;
    not-a-directory) printf 'something on its path is not a directory' ;;
    not-a-regular-file) printf 'it is not a regular file' ;;
    exists) printf 'something stands at the name and could not be removed' ;;
    outside) printf 'it resolves outside the directory it is spelled in' ;;
    *) printf 'run-write could not complete the write' ;;
  esac
}
