Add the webplot shell script

This shell script launches a webplot python server if not yet,
and creates a chrome tab to connect to the webplot server.
This saves the user from the overhead of creating a new tab and
typing the URL.

Eventually, an ebuild file would be needed to install this shell
script to a bin path such as /usr/local/bin so that it is convenient
to run webplot on a chrome os device.

BUG=chromium:443539
TEST=Manually execute the webplot script as root.

Change-Id: I77c6e648a8948e594c3ef078d3943c724138f7d8
Reviewed-on: https://chromium-review.googlesource.com/239055
Reviewed-by: Charlie Mooney <charliemooney@chromium.org>
Commit-Queue: Shyh-In Hwang <josephsih@chromium.org>
Tested-by: Shyh-In Hwang <josephsih@chromium.org>
diff --git a/webplot b/webplot
new file mode 100755
index 0000000..3a66b24
--- /dev/null
+++ b/webplot
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+# Copyright 2015 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+PROG="$(basename $0)"
+CHROME_CMD="/opt/google/chrome/chrome"
+# For the time being, the chrome browser and the webplot server are
+# located on the same machine.
+CHROME_FLAGS="--user-data-dir=/home/chronos localhost"
+TMP_LOG="/tmp/${PROG}_chrome.log"
+
+# A local die function to print the message and then exit
+die() {
+  echo -e "$@"
+  exit 1
+}
+
+# Search the webplot directory.
+PROG_DIR="$(find /usr/local/lib* -name $PROG)"
+if [ -z "$PROG_DIR" ]; then
+  die "Fail to find the path of $PROG."
+fi
+
+# Start webplot if not yet.
+if [ -n "$(ps a | egrep "python\s+${PROG}" | grep -v grep)" ]; then
+  echo "$PROG server has been started."
+else
+  # Must run webplot as root as it needs to access system device nodes.
+  if [ $USER != root ]; then
+    die "Please run $PROG as root."
+  fi
+
+  echo "Start $PROG server..."
+  sudo python "${PROG_DIR}"/"${PROG}".py &
+
+  # Wait a while for the webplot server to get ready before launching
+  # a chrome tab to connect to it.
+  sleep 1
+fi
+
+# Note that the chrome must be launched as chronos.
+# Hence, it is OK that $USER is chronos if the webpot server has been started.
+echo "$USER: start to launch a new chrome tab and connect to $PROG server..."
+if [ $USER = chronos ]; then
+  exec "$CHROME_CMD" $CHROME_FLAGS > "$TMP_LOG" 2>&1
+elif [ $USER = root ]; then
+  exec su chronos -c "$CHROME_CMD $CHROME_FLAGS > $TMP_LOG 2>&1"
+else
+  echo "You should login as chronos or root."
+fi
+