#!/bin/bash
# summary: Open a terminal window or run a command in it.
# starchaser terminal: opens the Terminal. Every part of Starchaser that opens a terminal window goes through here.
#
#   starchaser terminal [--class NAME] [--title TITLE] [--hold] [--directory FOLDER] [-o SETTING=VALUE]... [-e] [COMMAND...]
#   With no arguments, use the focused Files window's folder when available.
#
# The options are kitty's own (the ones Starchaser uses). With the screen reader off this is kitty, exactly as asked.
# kitty can't be read by a screen reader, so while the screen reader runs the window is xfce4-terminal instead, which
# can: the same class (window rules and the taskbar see "Terminal"), the same title, no menu bar or toolbar, the
# theme's colors and font (starchaser theme), and copy and paste on Ctrl+Shift+C and Ctrl+Shift+V. A kitty -o setting
# has no meaning there and is left out.
case "${1:-}" in -h|--help)
    sed -n '3,12p' "$0" | sed 's/^# \{0,1\}//'
    exit 0 ;;
esac
# Only the plain desktop launch follows Files. Callers running commands or supplying options keep their
# requested launch. NUL framing preserves folder names even when they end in a newline.
if [ $# -eq 0 ] && command -v python3 >/dev/null 2>&1 && command -v hyprctl >/dev/null 2>&1; then
    if IFS= read -r -d '' folder < <(python3 "$(dirname "$(readlink -f "$0")")/starchaser_terminal_folder.py"); then
        set -- --directory "$folder"
    fi
fi
# the screen reader is on: Orca runs for this account (the same test as Settings > Accessibility). Without
# xfce4-terminal (taken off by hand) kitty still opens: a terminal that can't be read beats none at all
if ! pgrep -x -u "$(id -u)" orca >/dev/null 2>&1 || ! command -v xfce4-terminal >/dev/null 2>&1; then
    if [ -f "${XDG_CONFIG_HOME:-$HOME/.config}/starchaser/permissions.json" ]; then
        exec starchaser app permissions command kitty -- kitty "$@"
    fi
    exec kitty "$@"
fi

class=starchaser-terminal
args=(--disable-server --hide-menubar --hide-toolbar)
while [ $# -gt 0 ]; do
    case "$1" in
        --class|--title|--directory|-o)
            [ $# -ge 2 ] || { echo "starchaser terminal: $1 needs a value after it" >&2; exit 2; }
            set -- "$1=$2" "${@:3}"
            continue ;;
        --class=*) class=${1#*=} ;;
        --title=*) args+=("--title=${1#*=}") ;;
        --directory=*) args+=("--working-directory=${1#*=}") ;;
        --hold) args+=(--hold) ;;
        -o=*) ;;
        -e) shift; break ;;
        --) shift; break ;;
        -*) echo "starchaser terminal: unknown option '$1'. Run 'starchaser terminal --help' for more help." >&2
            exit 2 ;;
        *) break ;;
    esac
    shift
done
[ $# -eq 0 ] || args+=(-x "$@")
# its own process, so the window has the class asked for (xfce4-terminal names its window after the program name it
# was started as) and opens with the theme read fresh; the greeting's ship scene stays still, so the screen reader
# reads it once instead of at every frame, and its Terminal line says "Screen reader", not the program's name
export STARCHASER_GREETING_STATIC=1 STARCHASER_SCREEN_READER_TERMINAL=1
if [ -f "${XDG_CONFIG_HOME:-$HOME/.config}/starchaser/permissions.json" ]; then
    exec starchaser app permissions command kitty -- bash -c 'exec -a "$0" xfce4-terminal "$@"' "$class" "${args[@]}"
fi
exec -a "$class" xfce4-terminal "${args[@]}"
