Joseph Hwang | 7d423d9 | 2015-01-07 07:37:03 | [diff] [blame] | 1 | #!/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 Hwang | 9a34166 | 2015-02-05 05:42:23 | [diff] [blame] | 7 | # 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. |
| 11 | PROG="$(basename $0 .sh)" |
Joseph Hwang | 7d423d9 | 2015-01-07 07:37:03 | [diff] [blame] | 12 | |
| 13 | # A local die function to print the message and then exit |
| 14 | die() { |
| 15 | echo -e "$@" |
| 16 | exit 1 |
| 17 | } |
| 18 | |
Joseph Hwang | 9a34166 | 2015-02-05 05:42:23 | [diff] [blame] | 19 | # Read command flags |
| 20 | . /usr/share/misc/shflags |
| 21 | DEFINE_boolean kill false 'kill the existing webplot process' 'k' |
| 22 | |
| 23 | FLAGS_HELP="USAGE: $PROG [flags]" |
| 24 | |
| 25 | FLAGS "$@" || exit 1 |
| 26 | eval set -- "${FLAGS_ARGV}" |
| 27 | set -e |
| 28 | |
| 29 | get_webplot_process_status() { |
| 30 | echo $(ps a | egrep "python\s.+${PROG}" | grep -v grep) |
| 31 | } |
| 32 | |
| 33 | if [ "$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 |
| 44 | fi |
| 45 | |
Joseph Hwang | 7d423d9 | 2015-01-07 07:37:03 | [diff] [blame] | 46 | # Search the webplot directory. |
Joseph Hwang | 873765a | 2015-02-03 05:19:35 | [diff] [blame] | 47 | # Stop at the first found webplot directory. Priority is given to /usr/lib*. |
| 48 | DIRS="/usr/lib* /usr/local/lib*" |
| 49 | for 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 |
| 55 | done |
| 56 | |
Joseph Hwang | 7d423d9 | 2015-01-07 07:37:03 | [diff] [blame] | 57 | if [ -z "$PROG_DIR" ]; then |
| 58 | die "Fail to find the path of $PROG." |
| 59 | fi |
| 60 | |
| 61 | # Start webplot if not yet. |
Joseph Hwang | 9a34166 | 2015-02-05 05:42:23 | [diff] [blame] | 62 | if [ -n "$(get_webplot_process_status)" ]; then |
Joseph Hwang | 7d423d9 | 2015-01-07 07:37:03 | [diff] [blame] | 63 | echo "$PROG server has been started." |
| 64 | else |
| 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 Hwang | 9a34166 | 2015-02-05 05:42:23 | [diff] [blame] | 71 | python "${PROG_DIR}"/"${PROG}".py & |
Joseph Hwang | 7d423d9 | 2015-01-07 07:37:03 | [diff] [blame] | 72 | |
| 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 Hwang | 7d423d9 | 2015-01-07 07:37:03 | [diff] [blame] | 76 | |
Joseph Hwang | 9a34166 | 2015-02-05 05:42:23 | [diff] [blame] | 77 | # 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." |
| 80 | fi |