#!/bin/sh
# ollie-session-here: start a session in the given directory
# Works from both Dolphin (passes %f) and terminal (uses $PWD).
#
# Session is always "default". Agent name is the base directory name.
# Session/agent creation is idempotent.

TARGET="$1"
[ -f "$TARGET" ] && TARGET="$(dirname "$TARGET")"
[ -z "$TARGET" ] && TARGET="$PWD"
TARGET="$(cd "$TARGET" 2>/dev/null && pwd)" || TARGET="$PWD"

# Agent name is base directory name
AGENT_NAME="$(basename "$TARGET")"

# Create default session (idempotent) and named agent
printf 'name=default\ncwd=%s\n' "$TARGET" | ollie-9p write session/new
printf 'name=%s\n' "$AGENT_NAME" | ollie-9p write session/default/agent/new

if [ -t 1 ]; then
    # Terminal: print session info
    echo "Session: default"
    echo "Agent: $AGENT_NAME"
    echo "CWD: $TARGET"
else
    # Dolphin/non-interactive: desktop notification
    notify-send -t 3000 "Ollie" "Agent '$AGENT_NAME' started in $TARGET"
fi
