Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / tools / android / adb_remote_setup.sh
1 #!/bin/bash
2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 # URL from which the latest version of this script can be downloaded.
7 script_url="http://src.chromium.org/svn/trunk/src/tools/android/adb_remote_setup.sh"
8
9 # Replaces this file with the latest version of the script and runs it.
10 update-self() {
11   local script="${BASH_SOURCE[0]}"
12   local new_script="${script}.new"
13   local updater_script="${script}.updater"
14   curl -sSf -o "$new_script" "$script_url" || return
15   chmod +x "$new_script" || return
16
17   # Replace this file with the newly downloaded script.
18   cat > "$updater_script" << EOF
19 #!/bin/bash
20 if mv "$new_script" "$script"; then
21   rm -- "$updater_script"
22 else
23   echo "Note: script update failed."
24 fi
25 ADB_REMOTE_SETUP_NO_UPDATE=1 exec /bin/bash "$script" $@
26 EOF
27   exec /bin/bash "$updater_script" "$@"
28 }
29
30 if [[ "$ADB_REMOTE_SETUP_NO_UPDATE" -ne 1 ]]; then
31   update-self "$@" || echo 'Note: script update failed'
32 fi
33
34 if [[ $# -ne 1 && $# -ne 2 ]]; then
35   cat <<'EOF'
36 Usage: adb_remote_setup.sh REMOTE_HOST [REMOTE_ADB]
37
38 Configures adb on a remote machine to communicate with a device attached to the
39 local machine. This is useful for installing APKs, running tests, etc while
40 working remotely.
41
42 Arguments:
43   REMOTE_HOST  hostname of remote machine
44   REMOTE_ADB   path to adb on the remote machine (you can omit this if adb is in
45                the remote host's path)
46 EOF
47   exit 1
48 fi
49
50 remote_host="$1"
51 remote_adb="${2:-adb}"
52
53 # Ensure adb is in the local machine's path.
54 if ! which adb >/dev/null; then
55   echo "error: adb must be in your local machine's path."
56   exit 1
57 fi
58
59 if which kinit >/dev/null; then
60   # Allow ssh to succeed without typing your password multiple times.
61   kinit -R || kinit
62 fi
63
64 # Kill the adb server on the remote host.
65 ssh "$remote_host" "$remote_adb kill-server"
66
67 # Start the adb server locally.
68 adb start-server
69
70 # Forward various ports from the remote host to the local host:
71 #   5037: adb
72 #   8001: http server
73 #   9031: sync server
74 #   10000: net unittests
75 #   10201: net unittests
76 ssh -C \
77     -R 5037:localhost:5037 \
78     -L 8001:localhost:8001 \
79     -L 9031:localhost:9031 \
80     -R 10000:localhost:10000 \
81     -R 10201:localhost:10201 \
82     "$remote_host"