#!/bin/bash

# Function to find BRIEF.md with backwards compatibility
# Prefers $WORKING_DIR/BRIEF.md but falls back to root BRIEF.md
find_brief_file() {
    # Use environment variable if set, otherwise default to "docs"
    local working_dir="${CLAUDEFSD_WORKING_DIR:-docs}"

    if [ -f "$working_dir/BRIEF.md" ]; then
        echo "$working_dir/BRIEF.md"
    elif [ -f "BRIEF.md" ]; then
        echo "BRIEF.md"
    else
        return 1
    fi
}

# Generic function to find any file in working dir or root
# Usage: find_project_file "PLAN.md"
# Returns: path to file (prefers $WORKING_DIR/file, falls back to ./file)
find_project_file() {
    local filename="$1"
    local working_dir="${CLAUDEFSD_WORKING_DIR:-docs}"

    if [ -f "$working_dir/$filename" ]; then
        echo "$working_dir/$filename"
    elif [ -f "$filename" ]; then
        echo "$filename"
    else
        return 1
    fi
}

# If called directly, just output the path
if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
    find_brief_file
fi