# This is part of a multi-lingual interface to access the robot's name.
# Any changes made to this file should be reflected in every other language's
# implementation of this library.

export ZOOX_ROBOT_NAME_FILE="/etc/robot_name"

function trim() {
  tr --delete '[[:space:]]'
}

function robot_name() {
  if [ -f "$ZOOX_ROBOT_NAME_FILE" ] && [ -n "${ZOOX_ROBOT_NAME:-}" ]; then
    echo Failed to lookup robot name. >&2
    echo $ZOOX_ROBOT_NAME_FILE exists and \$ZOOX_ROBOT_NAME is set. >&2
    return 1
  fi

  if [ -f "$ZOOX_ROBOT_NAME_FILE" ]; then
    local robot_name_content
    robot_name_content="$(tr --delete '[[:space:]]' < "$ZOOX_ROBOT_NAME_FILE")"
    readonly robot_name_content
    if [ -z "$robot_name_content" ]; then
      echo Failed to lookup robot name. >&2
      echo $ZOOX_ROBOT_NAME_FILE is empty. >&2
      return 1
    fi

    trim <<< "$robot_name_content"
    return 0
  fi

  if [ -n "${ZOOX_ROBOT_NAME:-}" ]; then
    trim <<< "$ZOOX_ROBOT_NAME"
    return 0
  fi

  echo Failed to lookup robot name. >&2
  echo $ZOOX_ROBOT_NAME_FILE does not exist and \$ZOOX_ROBOT_NAME is not set. >&2
  return 1
}
export -f robot_name
