# Common setup for the BASH environment on a zoox vehicle
# This should be source'd from .bashrc

if [ -z "${ZOOX_WORKSPACE_ROOT}" ]; then
    # In case ZOOX_WORKSPACE_ROOT wasn't explicitly set, find it based on the
    # location of this file.
    script_directory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
    export ZOOX_WORKSPACE_ROOT="$(cd $script_directory/../../ && pwd)"
fi

# EDITOR variable:  all zoox vehicles should run 'vim' by default
export EDITOR="vim"

# Configure ROS to look for the master server on the Main AI machine.
export ROS_MASTER_URI="${ROS_MASTER_URI:-http://main-ai:11311}"
# Configure ROS to provide DNS-like services for nodes.
if [ -f /etc/zoox/static_ip ] ; then
    export ROS_IP="$(tr --delete '[:space:]' < /etc/zoox/static_ip)"
fi

# Set up workspace and bazel tools
export ZOOX_MACHINE_ROLE="$(tr --delete '[:space:]' < /etc/zoox/role)"
source "$ZOOX_WORKSPACE_ROOT/scripts/shell/common.sh"

# Don't spawn AT/a11y helper daemons
# see: https://forums.gentoo.org/viewtopic-t-947210-start-0.html
export NO_AT_BRIDGE=1

# Enable cgroup and priority setting in roslaunch
export ROSLAUNCH_RT="true"

# TTY Flow Control Keybindings
# See: http://unix.stackexchange.com/a/12108/118730
# And: `man stty`

# Only update stty settings if the terminal is interactive
# gnu.org/software/bash/manual/html_node/Is-this-Shell-Interactive_003f.html
case $- in
  *i*)
    # Disable Ctrl-s for terminal flow control 'on'
    # (if enabled, Ctrl-s will freeze all input/output on the tty)
    stty -ixon
    # Disable Ctrl-q for terminal flow control 'off'
    # (if enabled, Ctrl-q will unfreeze after Ctrl-s)
    stty -ixoff
    # Allow any key to deactivate flow control, if it is on.
    stty -ixany
    # Set the PS1 Prompt only if TERM is set (it fails hard otherwise)
    if [ -v TERM ]; then
        source "$ZOOX_WORKSPACE_ROOT/scripts/vehicle/terminal_prompt.sh"
    fi

    # Load aliases
    source "$ZOOX_WORKSPACE_ROOT/scripts/vehicle/alias.sh"
    ;;
esac

# Load functions
source "$ZOOX_WORKSPACE_ROOT/scripts/vehicle/function.sh"

export PYTHONPATH="${ZOOX_WORKSPACE_ROOT}"
