#!/usr/bin/env bash
# UserPromptSubmit Hook example
# Purpose: tell the agent where the current LLM-Wiki project memory lives.
#
# This example does not append content automatically. It only injects a short
# reminder so the agent reads and updates the right files.
#
# Setup options:
# 1. Export LLM_WIKI_PROJECT_DIR=/absolute/path/to/LLM-Wiki/10-projects/my-project
# 2. Or create .llm-wiki-project in the repo root with the absolute path.

set -euo pipefail

input="$(cat)"

find_project_dir() {
  if [ -n "${LLM_WIKI_PROJECT_DIR:-}" ] && [ -d "$LLM_WIKI_PROJECT_DIR" ]; then
    printf '%s' "$LLM_WIKI_PROJECT_DIR"
    return 0
  fi

  local repo_root=""
  repo_root="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
  local pointer_file="${repo_root}/.llm-wiki-project"

  if [ -f "$pointer_file" ]; then
    local configured_path=""
    configured_path="$(head -n 1 "$pointer_file" | sed 's/[[:space:]]*$//')"
    if [ -n "$configured_path" ] && [ -d "$configured_path" ]; then
      printf '%s' "$configured_path"
      return 0
    fi
  fi

  return 1
}

project_dir="$(find_project_dir || true)"

if [ -z "$project_dir" ]; then
  exit 0
fi

readme_path="${project_dir}/README.md"
progress_path="${project_dir}/progress.md"

ctx="현재 작업의 장기기억은 LLM-Wiki 프로젝트 폴더에 있습니다.
- 프로젝트 폴더: ${project_dir}
- 먼저 읽을 파일: ${readme_path}, ${progress_path}
- 작업을 마치기 전 progress.md에 '오늘 한 일, 결정한 기준, 다음 에이전트가 먼저 읽을 파일'을 정리하세요.
- 민감정보나 원문 전체를 자동으로 저장하지 말고, 사람이 다시 이해할 수 있는 요약과 결정만 남기세요."

jq -n --arg ctx "$ctx" '{
  "hookSpecificOutput": {
    "hookEventName": "UserPromptSubmit",
    "additionalContext": $ctx
  }
}'

