blob: a197316ec71860570f33629e53aa77763d736c20 [file] [log] [blame]
Joseph Hwang7d423d92015-01-07 07:37:031#!/bin/sh
2
3# Copyright 2015 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
Joseph Hwang9a341662015-02-05 05:42:237# This script may or may not come with a suffix.
8# If this script is installed with emerge, the suffix has been removed.
9# If this script is installed with scp, there still exists the suffix.
10# Try to remove the suffix any way.
11PROG="$(basename $0 .sh)"
Joseph Hwang7d423d92015-01-07 07:37:0312
13# A local die function to print the message and then exit
14die() {
15 echo -e "$@"
16 exit 1
17}
18
Joseph Hwang9a341662015-02-05 05:42:2319# Read command flags
20. /usr/share/misc/shflags
21DEFINE_boolean kill false 'kill the existing webplot process' 'k'
22
23FLAGS_HELP="USAGE: $PROG [flags]"
24
25FLAGS "$@" || exit 1
26eval set -- "${FLAGS_ARGV}"
27set -e
28
29get_webplot_process_status() {
30 echo $(ps a | egrep "python\s.+${PROG}" | grep -v grep)
31}
32
33if [ "$FLAGS_kill" = "$FLAGS_TRUE" ]; then
34 process=$(get_webplot_process_status)
35 if [ -z "$process" ]; then
36 echo 'No existing webplot process.'
37 else
38 for p in "$process"; do
39 echo killing $p
40 kill $(echo $p | awk '{print $1}')
41 done
42 fi
43 exit 0
44fi
45
Joseph Hwang7d423d92015-01-07 07:37:0346# Search the webplot directory.
Joseph Hwang873765a2015-02-03 05:19:3547# Stop at the first found webplot directory. Priority is given to /usr/lib*.
48DIRS="/usr/lib* /usr/local/lib*"
49for d in $DIRS; do
50 PROG_DIR="$(find $d -name $PROG -type d -print -quit)"
51 if [ -n "$PROG_DIR" ]; then
52 echo "Found webplot path in $PROG_DIR"
53 break
54 fi
55done
56
Joseph Hwang7d423d92015-01-07 07:37:0357if [ -z "$PROG_DIR" ]; then
58 die "Fail to find the path of $PROG."
59fi
60
61# Start webplot if not yet.
Joseph Hwang9a341662015-02-05 05:42:2362if [ -n "$(get_webplot_process_status)" ]; then
Joseph Hwang7d423d92015-01-07 07:37:0363 echo "$PROG server has been started."
64else
65 # Must run webplot as root as it needs to access system device nodes.
66 if [ $USER != root ]; then
67 die "Please run $PROG as root."
68 fi
69
70 echo "Start $PROG server..."
Joseph Hwang9a341662015-02-05 05:42:2371 python "${PROG_DIR}"/"${PROG}".py &
Joseph Hwang7d423d92015-01-07 07:37:0372
73 # Wait a while for the webplot server to get ready before launching
74 # a chrome tab to connect to it.
75 sleep 1
Joseph Hwang7d423d92015-01-07 07:37:0376
Joseph Hwang9a341662015-02-05 05:42:2377 # Tell the user to type URL in chrome as there is no reliable way to
78 # launch a chrome tab from command line in chrome os.
79 echo "Please type \"localhost\" in the browser."
80fi