#!/usr/bin/env bash
###############################################################################
# BRLTTY - A background process providing access to the console screen (when in
#          text mode) for a blind person using a refreshable braille display.
#
# Copyright (C) 1995-2026 by The BRLTTY Developers.
#
# BRLTTY comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
# GNU Lesser General Public License, as published by the Free Software
# Foundation; either version 2.1 of the License, or (at your option) any
# later version. Please see the file LICENSE-LGPL for details.
#
# Web Page: http://brltty.app/
#
# This software is maintained by Dave Mielke <dave@mielke.cc>.
###############################################################################

. "$(dirname "${0}")/brltty-prologue.sh"

usageSummary="\
Run a shell or terminal manager via the brltty-pty terminal emulator
and brltty using its Terminal Emulator screen driver.

Syntax: ${programName} [shell] [-option ...]

This script is especially useful on platforms that don't support a native screen driver.
This newer method may (should) be used in preference to the older method of
running brltty in conjunction with its provided patch to the screen program.

All of the arguments to this script are interpreted as brltty options,
with the following exceptions:
* If the first argument is -h then this usage summary is displayed.
* If the first argument doesn't begin with - (i.e. it isn't an option)
  then it's interpreted as the shell to run, and the rest of the arguments
  are interpreted as brltty options.

The shell may be specified via either its name or its path. It's also okay
to specify a terminal manager rather than a shell. For example:
* ${programName} screen
* ${programName} tmux

If the shell isn't specified on the command line then, in decreasing
precedence, one of the following is used:
1: If set, the \$BRLTTY_TERM_SHELL environment variable.
2: If set, the \$SHELL environment variable.
3: The default shell (sh).

Brltty is run as root so that it can access the braille device. If its
set-user-id bit is set and/or if this script is already running as root
then it's run directly. If neither, it's run via sudo. In this case, you
may wish to consider configuring sudo so that it won't prompt for the
user's password since the braille device wouldn't be operational yet.

If the copy of this script in its installed location is used then the
installed brltty is run. If, however, the copy in the top-level directory
of the source tree is used then brltty in the build tree (which needn't
be installed) is run.

"

shellCommand=""

[ "${#}" -eq 0 ] || {
   argument="${1}"

   [ "${argument}" = "-h" ] && {
      echo -n "${usageSummary}"
      exit 0
   }

   [ "${#argument}" -eq 0 ] || {
      [ "${argument:0:1}" = "-" ] || {
         shellCommand="${argument}"
         shift 1
      }
   }
}

[ -t 0 ] || semanticError "standard input isn't a terminal"
[ -t 1 ] || semanticError "standard output isn't a terminal"

[ -n "${shellCommand}" ] || shellCommand="${BRLTTY_TERM_SHELL}"
[ -n "${shellCommand}" ] || shellCommand="${SHELL}"
[ -n "${shellCommand}" ] || shellCommand="sh"

findHostCommand shellPath "${shellCommand}" || {
   semanticError "shell not found: ${shellCommand}"
}

screenDriver="em"
screenParameters=()

addScreenParameter() {
   local name="${1}"
   local value="${2}"

  screenParameters+=("-X${screenDriver}:${name}=${value}")
}

currentUser="$(id -u)"
currentGroup="$(id -g)"

addScreenParameter shell "${shellPath}"
addScreenParameter user "${currentUser}"
addScreenParameter group "${currentGroup}"
addScreenParameter directory "${PWD}"
addScreenParameter home "${HOME}"

needSiblingBrltty brlttyPath
SUDO="sudo --bell --preserve-env --preserve-env=USER,LOGNAME --"
[ -u "${brlttyPath}" ] && SUDO=""
[ "${currentUser}" -eq 0 ] && SUDO=""

exec ${SUDO} "${brlttyPath}" -n -z -x"${screenDriver}" "${screenParameters[@]}" "${@}"
exit "${?}"
