#!/bin/bash -p
###############################################################################
# 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-2017 by The BRLTTY Developers.
#
# BRLTTY comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
# GNU General Public License, as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any
# later version. Please see the file LICENSE-GPL for details.
#
# Web Page: http://brltty.com/
#
# This software is maintained by Dave Mielke <dave@mielke.cc>.
###############################################################################

set -e
readonly defaultInstance="/etc/brltty.conf"
readonly programName="${0##*/}"

programMessage() {
  local message="${1}"

  echo >&2 "${programName}: ${message}"
}

syntaxError() {
  local message="${1}"

  programMessage "${message}"
  exit 2
}

semanticError() {
  local message="${1}"

  programMessage "${message}"
  exit 3
}

readonly currentInstance="${BRLTTY_SYSTEMD_INSTANCE:-${defaultInstance}}"
[ -e "${currentInstance}" ] || semanticError "instance not found: ${currentInstance}"
[ -r "${currentInstance}" ] || semanticError "instance not readable: ${currentInstance}"

if [ -c "${currentInstance}" ]
then
  udevMaintained=false

  while read line
  do
    if [[ "${line}" =~ ^'N: ' ]]
    then
      udevMaintained=true
    elif [[ "${line}" =~ ^'E: '([^ =]+)=(.*) ]]
    then
      name="${BASH_REMATCH[1]}"
      value="${BASH_REMATCH[2]}"

      [[ "${name}" =~ ^'BRLTTY_' ]] || continue
      [ -z "${!name}" ] || continue

      export "${name}=${value}"
    fi
  done < <((udevadm info --name="${currentInstance}" --export 2>/dev/null))

  "${udevMaintained}" || semanticError "instance not maintained by udev: ${currentInstance}"
elif [ -f "${currentInstance}" ]
then
  export BRLTTY_CONFIGURATION_FILE="${currentInstance}"
else
  semanticError "unrecognized instance type: ${currentInstance}"
fi

set -- "${BRLTTY_EXECUTABLE_PATH:-brltty}" -E ${BRLTTY_EXECUTABLE_ARGUMENTS} "${@}"
programMessage "starting executable: ${*}"
exec "${@}"
exit "${?}"
