Add `remote_adb_setup` for remote work with local device.

This is based on Chromium's adb_remote_setup.sh modified by Tony at
https://github.com/domokit/devtools/issues/5 .

Fixes #18.

R=tonyg@chromium.org

Review URL: https://codereview.chromium.org/1179993006.

Cr-Mirrored-From: https://github.com/domokit/mojo
Cr-Mirrored-Commit: a19865b200b0db9b6f7b64346bc240117d1093c5
diff --git a/README.md b/README.md
index 2e06256..656dbd1 100644
--- a/README.md
+++ b/README.md
@@ -23,8 +23,10 @@
 
 The set of executable scripts is WIP. We currently offer:
 
- - **debugger** - allowing to send commands to the mojo:debugger app running in
-   the shell, allowing e.g. to interactively start and stop tracing
+ - **debugger** - supports interactive tracing of a running mojo shell and
+   symbolizing android stack traces
+ - **remote_adb_setup** - configures adb on a remote machine to communicate with
+   a device attached to the local machine
 
 ## Install
 
diff --git a/remote_adb_setup b/remote_adb_setup
new file mode 100755
index 0000000..d275ff6
--- /dev/null
+++ b/remote_adb_setup
@@ -0,0 +1,51 @@
+#!/bin/bash
+# Copyright 2015 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+if [[ $# -ne 1 && $# -ne 2 ]]; then
+  cat <<'EOF'
+Usage: remote_adb_setup REMOTE_HOST [REMOTE_ADB]
+
+Configures adb on a remote machine to communicate with a device attached to the
+local machine. This is useful for installing APKs, running tests, etc while
+working remotely.
+
+Arguments:
+  REMOTE_HOST  hostname of remote machine
+  REMOTE_ADB   path to adb on the remote machine (you can omit this if adb is in
+               the remote host's path)
+EOF
+  exit 1
+fi
+
+remote_host="$1"
+remote_adb="${2:-adb}"
+
+# Ensure adb is in the local machine's path.
+if ! which adb >/dev/null; then
+  echo "error: adb must be in your local machine's path."
+  exit 1
+fi
+
+if which kinit >/dev/null; then
+  # Allow ssh to succeed without typing your password multiple times.
+  kinit -R || kinit
+fi
+
+# Kill the adb server on the remote host.
+ssh "$remote_host" "$remote_adb kill-server"
+
+# Start the adb server locally.
+adb start-server
+
+# Forward various ports from the remote host to the local host:
+#   5037: adb
+# and from the local host to the remote host:
+#   9998: http server for Sky
+#   31840: http server for the local mojo: origin
+ssh -C \
+    -R 5037:127.0.0.1:5037 \
+    -L 9998:127.0.0.1:9998 \
+    -L 31840:127.0.0.1:31840 \
+    "$remote_host"