#!/bin/bash
# AskFile - Ask ollie about the current file in acme
#
# Usage: Middle-click AskFile in the tag
# Sends: "Describe and explain this file: <filename>"

set -euo pipefail

if [[ -z "${winid:-}" ]]; then
    echo "AskFile must be run from within acme" >&2
    exit 1
fi

# Get the window name (filename) from the tag
filename=$(9p read acme/$winid/tag | awk '{print $1}')

if [[ -z "$filename" || "$filename" == *"+Errors" || "$filename" == "+*" ]]; then
    echo "No file associated with this window" >&2
    exit 1
fi

# Ensure acme session/agent exists
eval $(acme-ollie-ensure)

# Build and send the prompt
prompt="Describe and explain this file: $filename"
echo "$prompt" | o "$OLLIE_SESSION/$OLLIE_AGENT" prompt
