function fakeros_from_bazel() {
  _initialize_pathlists

  local runfiles_dir="$1"
  _system_setup "$runfiles_dir"
  _package_setup "$runfiles_dir"

  _canonicalize_environment
}

function _remove_fakeros_entries() {
  echo "$@" | \
    tr ":" "\n" | \
    grep --extended-regexp --invert-match "\bfakeros\.runfiles\b" | \
    tr "[:space:]" ":"
}

function _initialize_pathlists() {
  export PATH="${PATH:-""}"
  export LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-""}"
  export PYTHONPATH="${PYTHONPATH:-""}"
  export CMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH:-""}"
  export ROS_PACKAGE_PATH="${ROS_PACKAGE_PATH:-""}"
}

function _clean_environment() {
  export PATH="$(_remove_fakeros_entries "$PATH")"
  export LD_LIBRARY_PATH="$(_remove_fakeros_entries "$LD_LIBRARY_PATH")"
  export PYTHONPATH="$(_remove_fakeros_entries "$PYTHONPATH")"

  export CMAKE_PREFIX_PATH=""
  export ROS_PACKAGE_PATH=""
}

function _canonicalize_pathlist() {
  # Remove leading, trailing, and repeated colons.
  # Remove repeated entries, keeping the first instance.
  echo -n "$@" | \
    sed --expression='s#^:##;s#:$##;s#:\+#:#g' | \
    awk -v RS=: '!a[$0]++' |
    tr "[:space:]" ":"
}

function _canonicalize_environment() {
  export PATH="$(_canonicalize_pathlist "$PATH")"
  export LD_LIBRARY_PATH="$(_canonicalize_pathlist "$LD_LIBRARY_PATH")"
  export PYTHONPATH="$(_canonicalize_pathlist "$PYTHONPATH")"
  export CMAKE_PREFIX_PATH="$(_canonicalize_pathlist "$CMAKE_PREFIX_PATH")"
  export ROS_PACKAGE_PATH="$(_canonicalize_pathlist "$ROS_PACKAGE_PATH")"
}

function _add_third_party_packages() {
  local package_dir="$1"

  # Use our custom versions of ROS binaries, libraries, and python packages.
  # NOTE: Ordering of this list is VERY important.
  # Entries are prepended to path lists, so the last entry in this list ends up
  # being the first in the path lists. rospkg MUST be available before roslib,
  # and roslib MUST be available before roslaunch and rostest.
  local -a third_party_packages
  third_party_packages=(capabilities nodelet_core/nodelet ros_comm/tools/rosbag roslaunch ros_comm/tools/rostest ros_comm/tools/rosmaster xacro roslib rospkg ros/tools/rosbash/scripts rospack)
  local third_party_package_paths=""
  local third_party_python_paths=""
  for package in ${third_party_packages[@]}; do
    local package_path="$package_dir/$package"
    local package_pythonpath="$package_path/src"

    if [ -d "$package_path" ]; then
      third_party_package_paths="$package_path:$third_party_package_paths"
    fi

    if [ -d "$package_pythonpath" ]; then
      third_party_python_paths="$package_pythonpath:$third_party_python_paths"
    fi
  done

  export PYTHONPATH="$third_party_python_paths:$PYTHONPATH"
  export ROS_PACKAGE_PATH="$third_party_package_paths:$ROS_PACKAGE_PATH"
}

function _system_setup() {
  local runfiles_dir="$1"

  local third_party_dir="$runfiles_dir/third_party/ros"

  _add_third_party_packages "$third_party_dir"

  export PATH="$PATH:$runfiles_dir/bin"
  export ROS_DISTRO=indigo
  export ROS_ROOT="$runfiles_dir"
  export ROS_HOME="$runfiles_dir"
  export ROS_TEST_RESULTS_DIR="$ROS_HOME/test_results"
  export ROS_MASTER_URI="${ROS_MASTER_URI:-http://localhost:11311}"
}

function _package_setup() {
  local runfiles_dir="$1"

  local -a package_path_roots
  package_path_roots=( $( \
    find "$runfiles_dir" \
        -maxdepth 1 -mindepth 1 -type d \
        -not -name _solib_local \
        -not -name external) )
  local ros_package_paths="$(
      echo ${package_path_roots[@]} | \
        tr "[:space:]" ":")"

  local python_paths="$(
    find "$runfiles_dir" \
        -maxdepth 1 -mindepth 1 -type d \
        -not -name _solib_local \
        -not -name base \
        -not -name external \
        -not -name third_party | \
      tr "[:space:]" ":")"

  local ros_msg_files="$(
      find ${package_path_roots[@]} -name '*.msg' -or -name '*.srv' | \
        tr "[:space:]" " ")"
  local ros_msg_paths=""
  if [ "$ros_msg_files" ]; then
    ros_msg_paths="$(
      echo "$ros_msg_files" | \
          xargs dirname | \
          xargs dirname | \
          sort | \
          uniq | \
        tr "[:space:]" ":")"
  fi

  export PYTHONPATH="$PYTHONPATH:$ros_msg_paths:$runfiles_dir:$python_paths"
  export ROS_PACKAGE_PATH="$ROS_PACKAGE_PATH:$ros_package_paths"
}
