#!/bin/sh
# ollie-session-here: start a session in the given directory
# Works from both Dolphin (passes %f) and terminal (uses $PWD).
TARGET="$1"
[ -f "$TARGET" ] && TARGET="$(dirname "$TARGET")"
[ -z "$TARGET" ] && TARGET="$PWD"
TARGET="$(cd "$TARGET" 2>/dev/null && pwd)" || TARGET="$PWD"

RESULT=$(dbus-send --session --dest=org.ollie.SessionManager --type=method_call --print-reply \
    /org/ollie/SessionManager org.ollie.SessionManager.CreateSession \
    string:"$TARGET" string:"" string:"" string:"default" string:"" string:"" 2>&1)

SESSION_ID=$(echo "$RESULT" | grep 'string "' | head -1 | sed 's/.*string "\(.*\)"/\1/')

if [ -t 1 ]; then
    # Terminal: print session ID for further use
    echo "$SESSION_ID"
else
    # Dolphin/non-interactive: desktop notification
    notify-send -t 3000 "Ollie" "Session started in $TARGET"
fi
