Skip to content

Instantly share code, notes, and snippets.

@prurigro
Last active August 29, 2015 14:09
Show Gist options
  • Save prurigro/0212451a714cd362aa47 to your computer and use it in GitHub Desktop.
Save prurigro/0212451a714cd362aa47 to your computer and use it in GitHub Desktop.
(Android+Raccoon) Tracks previously installed apps and uses adb to install new ones
#!/usr/bin/env bash
# USER VARIABLES
config_dir="$HOME/.config/Raccoon"
inst_pkglist_file="$config_dir/installed.txt"
blacklist_file="$config_dir/blacklist.txt"
pkg_width=38
ver_width=15
stat_width=30
# SCRIPT VARIABLES
inst_pkglist=$(<"$inst_pkglist_file")
script_name="${0//*\/}"
# FUNCTIONS
function help {
printf '%s\n\n' "USAGE: $script_name [OPTION(S)]"
printf '%s\n' "OPTIONS:"
printf ' %s\n' "-i|--install: install new packages while being listed"
printf ' %s\n\n' "-v|--verbose: display a heading and installed packages"
printf ' %s\n' "-h|--help: show this help and exit"
exit 0
}
function error {
printf '%s\n' "ERROR: $1" >&2
exit 1
}
function pretty_print {
local c_1 c_2 c_3
[[ -t 1 ]] && if [ "$4" = HEADING ]; then
c_1=$'\e[1;37m' c_2=$'\e[1;37m' c_3=$'\e[1;37m' c_c=$'\e[0m'
else
c_1=$'\e[1;33m' c_2=$'\e[1;34m' c_3=$'\e['"$4"'m' c_c=$'\e[0m'
fi
printf "%-${pkg_width}s%${ver_width}s%${stat_width}s\n" \
"$c_1$1" "$c_2$2" "$c_3$3$c_c"
}
# COMMANDLINE ARGUMENTS
install=0 verbose=0
while [[ -n "$1" ]]; do
case "$1" in
--help|-h)
help
;;
--install|-i)
install=1
;;
--verbose|-v)
verbose=1
;;
esac
shift
done
# CREATE CONFIG IF IT DOESN'T EXIST
if [[ ! -f "$inst_pkglist_file" ]]; then
touch "$inst_pkglist_file" >/dev/null 2>&1 \
|| error "couldn't create $inst_pkglist_file"
elif [[ ! -w "$inst_pkglist_file" ]]; then
error "can't write to $inst_pkglist_file"
fi
# LIST/install PACKAGES
[[ "$verbose" = 1 ]] \
&& pretty_print 'PACKAGE' 'VERSION' 'STATUS' 'HEADING'
while read -r; do
pkgname="${REPLY/*\//}"
pkgname="${pkgname/-*}"
pkgver="${REPLY/*\/*-/}"
pkgver="${pkgver/\.apk/}"
apk="$pkgname-$pkgver.apk"
if [[ ! -f "$blacklist_file" ]] || ! egrep -q "^$pkgname$" "$blacklist_file"; then
if [[ "$inst_pkglist" =~ $apk ]]; then
[[ "$verbose" = 1 ]] && \
pretty_print "$pkgname" "$pkgver" 'installed' '1;30'
else
if [[ "$install" = 1 ]]; then
if [[ $(adb devices) =~ device\ *$ ]]; then
if egrep -q '^ *Success' < <(adb install -r "$REPLY" 2>&1); then
pretty_print "$pkgname" "$pkgver" 'install success' '1;32'
printf '%s\n' "$apk" >> "$inst_pkglist_file"
else
pretty_print "$pkgname" "$pkgver" 'install failed' '1;31'
fi
else
error 'device not attached'
fi
else
pretty_print "$pkgname" "$pkgver" 'new package' '1;37'
fi
fi
fi
done < <(find "$config_dir/archives/default" -iname '*.apk')
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment