#!/bin/sh
# ollie-session-here: start a session in the given directory
# Works from both Dolphin (passes %f) and terminal (uses $PWD).
#
# Session name is the base directory name (not full path).
# Session creation is idempotent — always write to session/new.

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

# Extract base directory name for session name
SESSION_NAME="$(basename "$TARGET")"

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

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