1#!/bin/bash
2
3
4# DEFINES
5LUNCH_TYPE=generic-eng
6
7# GET SCRIPT LOCATION
8DIR=`pwd`
9BRANCH=(`cd $(dirname ${BASH_SOURCE[0]})/../../.. && pwd`)
10cd $DIR
11
12
13# Usage info
14show_help() {
15  echo "
16    Usage: ${0##*/} [HELP] [DEVICE]
17    Quickly switch to a specified device
18
19    -h, -?, --help      display this help message
20    <blank>             list currently attached devices
21    DEVICE              system switches to first device that
22                        matches this term
23
24    Example:
25      ./sdv             prints all connected devices
26      ./sdv angler      switches to first angler
27      ./sdv ang         switches to first angler device
28      ./sdv vol         switches to volantis
29      ./sdv 6P          switches to Nexus 6P
30      ./sdv 8X          switches to first matching device
31                        (eg. 8XV5T15725000936)
32  "
33  echo
34}
35
36# help message
37if [[ ( $1 == "--help" ) || ( $1 == "-h" ) || ( $1 == "-?" ) ]]; then
38  show_help
39  return
40fi
41
42# if adb is not available, try to set it up
43if [ ! `which adb` ]; then
44  echo "\"adb\" not setup. Using branch \"$BRANCH\" and lunch type \"$LUNCH_TYPE\""
45  DIR=`pwd`
46  cd $BRANCH
47  . build/envsetup.sh > /dev/null
48  lunch $LUNCH_TYPE > /dev/null
49  cd $DIR
50fi
51
52# get devices...
53if [ $# -eq 0 ]; then
54  adb devices -l
55  echo "Currently set to \"$ANDROID_SERIAL\""
56# ...or switch to specified device
57else
58  STR=(`adb devices -l | grep "$1"`)
59  if [ ${#STR[@]} -gt 0 ]; then
60    export ANDROID_SERIAL="$STR"
61    echo "Switched to device \"$ANDROID_SERIAL\""
62  else
63    echo "Device \"$1\" not found"
64  fi
65fi
66
67