Skip to content

Instantly share code, notes, and snippets.

@ktnr74
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktnr74/911c2e122832919c4a86 to your computer and use it in GitHub Desktop.
Save ktnr74/911c2e122832919c4a86 to your computer and use it in GitHub Desktop.
#!/bin/bash
AbortIfExecutableMissing () {
BIN=($@)
MISSINGBIN=$(for B in ${BIN[@]}; do [ "$(which $B 2>/dev/null)-" == "-" ] && echo $B; done)
if [ "${MISSINGBIN}-" != "-" ]
then
echo -e "Can't find the following executables: "$MISSINGBIN
exit 1
fi
return 0
}
AbortIfLibraryMissing () {
DEP=($@)
MISSINGDEP=$(for D in ${DEP[@]}; do dpkg-query -s $D 1>/dev/null 2>/dev/null ; [ "$?" != "0" ] && echo $D; done)
if [ "${MISSINGDEP}-" != "-" ]
then
echo -e "You need to run the following commands:\n\tsudo apt-get update\n\tsudo apt-get install "$MISSINGDEP
exit 1
fi
return 0
}
GetGoogleSourceFile () {
FILENAME=$(basename "$1")
TARGZ=$(echo $(dirname "$1")".tar.gz" | sed -e 's/\/%2B\//\/+\//' -e 's/\/+\//\/+archive\//')
wget -qO - $TARGZ | tar xzOf - $FILENAME | cat
}
GetLatestRepoXmlURL () {
REPOCONSTANTSURL="https://android.googlesource.com/platform/tools/base/+/master/sdklib/src/main/java/com/android/sdklib/repository/SdkRepoConstants.java"
VARS=(NS_LATEST_VERSION URL_GOOGLE_SDK_SITE URL_FILENAME_PATTERN)
VARPATTERN=$(echo -n "\\(";for V in ${VARS[@]}; do echo -n $V; echo -n "\\|"; done; echo -n "DONE\\)")
REPOCONSTANTS=$(GetGoogleSourceFile $REPOCONSTANTSURL)
eval $(echo "$REPOCONSTANTS" | tr '\n;' ' \n' | sed -e "s/${VARPATTERN}/\n\1/g" -e "s/%1\$d/\$${VARS[0]}/" | tr -d ' ' | sed -ne "/${VARPATTERN}=/p")
eval echo \$${VARS[1]}\$${VARS[2]}
}
GetLatestPackageURL () {
PKG=${1:-platform-tool}
XML=$(GetLatestRepoXmlURL)
echo -n $(dirname $XML)"/"
wget -qO - $XML | xml2 | grep '/sdk:'"$PKG/" | grep "linux\.zip$" | cut -d= -f2 | tail -n 1
}
InstallUniversalAndroidUdevRule () {
RULE="/etc/udev/rules.d/${1:-90}-android.rules"
if [ "0$(id -u)" == "00" ]
then
echo 'ACTION=="add", SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ENV{ID_USB_INTERFACES}=="*:ff420?:*", MODE="0666", GROUP="plugdev", SYMLINK+="android/$env{ID_SERIAL_SHORT}"' > $RULE
udevadm control --reload-rules
udevadm trigger --action=add --subsystem-match=usb
return 0
else
echo "No permission to add universal udev rule!"
return 1
fi
}
UpdateAdbUsbIni () {
INIFILE=${1:-$HOME}/.android/adb_usb.ini
mkdir -p $(dirname $INIFILE)
DEVICES=$(find -L /sys/bus/usb/devices -maxdepth 2 -path "*/modalias" -printf "%h\t" -exec cat {} \; | awk -F: '/icFFisc42ip0/ {print $1}')
echo -e "\nRegistering Vendor IDs for the following ADB devices:"
for D in $DEVICES; do echo -e "\tDEVPATH=$D Serial=$(cat $D/serial) VendorID=0x$(cat $D/idVendor) ($(cat $D/manufacturer))"; echo "0x$(cat $D/idVendor)" >> $INIFILE; done
VIDS=$(grep ^0x....$ $INIFILE | sort -u)
echo "$VIDS" > $INIFILE
return 0
}
DownloadAdbBinary () {
ADBPATH="${1:-.}/adb"
if [ -w $(dirname $ADBPATH) ]
then
echo -e "\nSaving adb to $ADBPATH\n"
wget -qO - $(GetLatestPackageURL "platform-tool") | funzip 2> /dev/null 1> $ADBPATH
TYPE=$(file $ADBPATH | grep "ELF 32-bit LSB shared object, Intel 80386")
if [ "$TYPE-" == "-" ]
then
file $ADBPATH
rm $ADBPATH
return 1
else
chmod 755 $ADBPATH
$ADBPATH devices
return 0
fi
else
echo "Can't write to $ADBPATH"
return 1
fi
}
AbortIfExecutableMissing "wget tar unzip gzip funzip sed tr xml2 find file awk xargs dirname"
AbortIfLibraryMissing "libncurses5:i386 libstdc++6:i386 zlib1g:i386"
InstallUniversalAndroidUdevRule
UpdateAdbUsbIni
DownloadAdbBinary $1
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment