Skip to content

Instantly share code, notes, and snippets.

@5p0ng3b0b
Last active January 15, 2019 11:00
Show Gist options
  • Save 5p0ng3b0b/5c521a1b816172168708667bf4ce1b2f to your computer and use it in GitHub Desktop.
Save 5p0ng3b0b/5c521a1b816172168708667bf4ce1b2f to your computer and use it in GitHub Desktop.
Script for processing APK files from cron
#!/bin/sh
# 11/01/2019
# Drop Folder script for renaming APKs
# Read apk file from SRC folder and move it to TGT folder while changing filename to APKLABEL_APKVERSION.apk
# If an existing version of the APK exists in the target folder then script will remove it
# Define METHOD as "aapt" or "apktool" depending upon what is available on server
# Script is much slower using apktool method
# Variables
METHOD="aapt"
SRC="~/public_html/5p0ng3b0b/dropfolders/apk"
TGT="~/public_html/5p0ng3b0b/apk"
if [ -d "$SRC" ];then mkdir -p $SRC
if [ -d "$TGT" ]then mkdir -p $TGT
# Functions
get_apk_filename () {
if [ "$1" = "" ]; then return 1; fi
local A="$1"
case $METHOD in
"apktool")
local D=/tmp/apktool
rm -f -R $D
apktool d -q -f -s --force-manifest -o $D $A
local A=$(basename $A)
local V=$(cat $D/apktool.yml | grep "versionName" | sed -e "s/versionName: //")
local T=$(cat $D/res/values/strings.xml | grep 'string name="title"' | sed -e 's/.*">//' -e 's/<.*//')
rm -f -R $D<commands>
;;
"aapt")
local A=$(aapt dump badging $A | grep -e "application-label:" -e "VersionName")
local V=$(echo $A | sed -e "s/.*versionName='//" -e "s/' .*//")
local T=$(echo $A | sed -e "s/.*application-label:'//" -e "s/'.*//")
;;
esac
echo ${T}_$(echo $V).apk
}
# Begin script
for APK in $(ls "$SRC"/*.apk); do
APKNAME=$(get_apk_filename "$APK")
rm -f $TGT/$(echo APKNAME | sed "s/_.*//")_*.apk
mv "$APK" "$TGT"/$APKNAME
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment