1
\$\begingroup\$

I'm new to bash scripting and need some help improving a script I made for working with reverse-engineering Android applications.

I wrote comments in the script itself to explain what I want to do. Some of the code/functions are missing/broken, but I hope that you can help me out with it.

#!/bin/bash

# Goal: Have a working folder with a source rom and work from there.
# process would be: unzip relevant apks, decompile, add translations, recompile and build, sign apks. In the end put the new apks in a lang-pack.zip
# In the future, i'd like to do more clever paths, so i don't write absolute pathnames to the different functions. 
# So that i can "install" my script whereever i want and the functions still work.
#
#
# I'd like to have the script start with a menu, where user can select to:
# 1. select which input zip file to work with
# 2. ON/OFF options for: -"Decompile" (default on)
#            -"Fix sources" part of script (default on)
#            -"add translations" (default on)
#            -"recompile" (default on)
#            -"Re-sign files" (default on)
#            -"create flashable zip" (default on)   
# 3. exit


echo "This script is designed to create language packs for patchrom MIUI ICS Desire by Marange"
echo ""
echo "Created by 1982Strand :) - Inspired by XavierJohn, TommyTomatoe and Marange."
echo ""

# Make sure we are root
if [ `id | sed -e 's/(.*//'` != "uid=0" ]; then
  echo "Sorry, you need super user privileges to run this script."
  exit 1
fi


##### Functions

# Want the user to select which zip to work with and show the selected one one the main menu
# Keep version number for later use. the rom zips are named MIUI-x.x.x-y.yy.yy.zip and the version number needed is the y.yy.yy
function select_zip
{
echo ""
echo "[--- Select ROM zip to work with ----]"
echo ""

PS3="Enter a number or 'q' to quit: "

fileList=$(find . -maxdepth 1 -type f)

select fileName in $fileList; do

    if [ -n "$fileName" ]; then

        # then return to main menu, wait for user to make an input, but don't know how to do it :(

    fi

    break

done
}

# Unzip apks from source zip into "apk_in" directory
# Needs refining
# Has to pull every apk from the zip, then remove unwanted afterwards. (Or simply just pull the needed ones, perhaps by making a seperate file with a list of the files needed..?)

function unzip_apks
{
echo ""
echo "[--- Unzipping apks ----]"
echo ""
# clear apk_in and apk_out directory
rm -r ./apk_in
rm -r ./apk_out
# now unzip the apks from the file specified in select_zip.
unzip -j -u *.zip *.apk -d ./apk_in # the *.zip part needs to use the file specified

echo ""
echo "[--- Removing unneeded apks ---]"
echo ""
cd /root/buildtool/apk_in

rm Google*.apk
rm ChromeBookmarksSyncAdapter.apk
rm DefaultContainerService.apk
rm LatinIME.apk
rm MediaProvider.apk
rm PicoTts.apk
rm Provision.apk
rm UserDictionaryProvider.apk
rm Vending.apk
rm VisualizationWallpapers.apk
rm WAPPushManager.apk
rm WeatherProvider.apk
rm SuperMarket.apk
rm LatinImeDictionaryPack.apk
rm SharedStorageBackup.apk
rm Talk.apk
rm Gmail.apk

cd ..
}


function decompile_apks
{
echo ""
echo "[--- Installing frameworks ---]"
echo ""
apktool if ./apk_in/framework-res.apk
apktool if ./if_frameworks/2.apk
apktool if ./if_frameworks/3.apk
apktool if ./if_frameworks/4.apk
apktool if ./if_frameworks/5.apk
apktool if ./apk_in/framework-miui-res.apk
echo ""
echo "[--- Decompiling apks ---]"
echo ""
cd /root/buildtool/apk_in

for file in `dir -d *` ; do
echo "Decompiling $file " # for log: 2>&1 | tee -a /root/buildtool/log/decompile_log.txt
apktool -q d -f $file /root/buildtool/apk_in/decompiled/$file # for log: 2>&1 | tee -a /root/buildtool/log/decompile_log.txt
done
cd /root/buildtool
}


function fix_src
{
echo ""
echo "[--- Fix MIUI sources ---]"
echo ""
echo ""
echo "Fixing Email.apk"
echo ""

patch -i src_fix/Email/ids.diff apk_in/decompiled/Email.apk/res/values/ids.xml
patch -i src_fix/Email/public.diff apk_in/decompiled/Email.apk/res/values/public.xml
patch -i src_fix/Email/sw600dp-styles.diff apk_in/decompiled/Email.apk/res/values-sw600dp/styles.xml
patch -i src_fix/Email/sw800dp-port.diff apk_in/decompiled/Email.apk/res/values-sw800dp-port/styles.xml

echo ""
echo "Fixing framework-miui-res.apk"
echo ""

patch -i src_fix/framework-miui-res/apktool.diff apk_in/decompiled/framework-miui-res.apk/apktool.yml

echo ""
echo "Fixing MiuiCompass.apk"
echo ""

patch -i src_fix/MiuiCompass/apktool.diff apk_in/decompiled/MiuiCompass.apk/apktool.yml

#echo ""
#echo "Fixing Gmail.apk"
#echo ""
#
#patch -i src_fix/Gmail/AndroidManifest.diff apk_in/Gmail/AndroidManifest.xml

echo ""
echo "Fixing Mms.apk"
echo ""

patch -i src_fix/Mms/bools.diff apk_in/decompiled/Mms.apk/res/values/bools.xml
}


function add_translation
{
echo ""
echo "[--- Adding Danish Translations ---]"
echo ""
cd /root/buildtool/translations/ma-xml-4.0-danish/device/bravo
cp -r * /root/buildtool/apk_in/decompiled

cd /root/buildtool/apk_in/decompiled/framework-res.apk/res
rm -r values-da

cd /root/buildtool/translations/ma-xml-4.0-danish/main
cp -r * /root/buildtool/apk_in/decompiled

cd /root/buildtool
}


function recompile_apks
{
echo ""
echo "[--- Recompile apks ---]"
echo ""
cd /root/buildtool/apk_in/decompiled
for b in `find * -maxdepth 0 -type d`; do
echo "Recompiling $b" # for log: 2>&1 | tee -a /root/buildtool/log/recompile_log.txt
apktool -q b -f $b /root/buildtool/apk_out/$b # for log: 2>&1 | tee -a /root/buildtool/log/recompile_log.txt
done
cd /root/buildtool
}


function sign_apks
{
echo ""
echo "[--- Re-sign apks ---]"
echo ""
for apk in `find apk_out/ -name "*.apk"`
do
echo "Signing $apk..."
java -jar ./signing/signapk.jar ./signing/testkey.x509.pem ./signing/testkey.pk8 $apk $apk.signed
zipalign 4 $apk.signed $apk.signed.aligned
mv $apk.signed.aligned $apk
rm $apk.signed
done
}

# When creating flashable zip, the script should name the finished zip with 
# the version number from the rom zip in select_zip function. 
function create_zip
{
echo ""
echo "[--- Creating flashable zip ---]"
echo ""
cd /root/buildtool/flashable
mkdir ./system
cd ./system
mkdir ./app
mkdir ./framework
cd /root/buildtool

   for C in /root/buildtool/apk_out/*; 
   do
    cp -f $C /root/buildtool/flashable/system/app
    done

mv -f /root/buildtool/flashable/system/app/framework-res.apk /root/buildtool/flashable/system/framework
mv -f /root/buildtool/flashable/system/app/framework-miui-res.apk /root/buildtool/flashable/system/framework
cp -f /root/buildtool/flashable/template.zip /root/buildtool/flashable/flashable.zip

7za a -tzip /root/buildtool/flashable/flashable.zip /root/buildtool/flashable/system -mx3

rm -r /root/buildtool/flashable/system
cd /root/buildtool
}

# When rom zip is selected and all on/off options are set, run the script.

function run_script
{
echo ""
echo "[--- Running script ----]"
echo ""
    unzip_apks
    # if all options ON:
    if [ $decompile -eq 1 ] && [ $fix_src -eq 1 ] && [ $translation -eq 1 ] && [ $recompile -eq 1 ] && [ $sign -eq 1 ] && [ $flashable -eq 1 ]; then
    decompile_apks
    fix_src 
    add_translation
    recompile_apks
    sign_apks
    create_zip
    fi

}


# ON/OFF functions

function dec_onoff() {
    [ $decompile -eq 1 ] && decompile=0 || decompile=1
}

function fix_onoff() {
    [ $source_fix -eq 1 ] && source_fix=0 || source_fix=1
}

function tra_onoff() {
    [ $translation -eq 1 ] && translation=0 || translation=1
}

function rec_onoff() {
    [ $recompile -eq 1 ] && recompile=0 || recompile=1
}

function sig_onoff() {
    [ $sign -eq 1 ] && sign=0 || sign=1
}

function fla_onoff() {
    [ $flashable -eq 1 ] && flashable=0 || flashable=1
}

##### From prevoius script, needs to be incorporated at some point

###################################################
#
# Track processing time
#TIMES=$SECONDS

# use apktool 1.4.9

#echo ""
#echo "[--- Using apktool 1.4.9 and MIUI aapt ---]"
#echo ""
#cp ./apktool/1.4.9/apktool.jar /usr/local/bin
#cp ./apktool/1.4.9/aapt /usr/local/bin



#TIMEE=$SECONDS
#DIFF=$(echo "$TIMEE-$TIMES" | bc)

#printf ""%dh:%dm:%ds"\n" $(($DIFF/3600)) $(($DIFF%3600/60)) $(($DIFF%60))
#
###########################################

#Next part inspired by XJ's tool, not sure if commands are correct...

decompile=1
source_fix=1
translation=1
recompile=1
sign=1
flashable=1


while true; do
    [ $decompile -eq 0 ] && tmpdecompile="OFF" || tmpdecompile="ON"
    [ $source_fix -eq 0 ] && tmpsource_fix="OFF" || tmpsource_fix="ON"
    [ $translation -eq 0 ] && tmptranslation="OFF" || tmptranslation="ON"
    [ $recompile -eq 0 ] && tmprecompile="OFF" || tmprecompile="ON"
    [ $sign -eq 0 ] && tmpsign="OFF" || tmpsign="ON"
    [ $flashable -eq 0 ] && tmpflashable="OFF" || tmpflashable="ON"


    clear
    echo "--------------------------------------------------------------"
    echo "                   Dans Build Tool for MIUI"
    echo "                      -by Dan Strand-"
    echo "--------------------------------------------------------------"
    echo "SETTINGS:"
    echo "       Decompile apks         : $tmpdecompile"
    echo "       Fix sources            : $tmpsource_fix"
    echo "       Add translations       : $tmptranslation"
    echo "       Recompile apks         : $tmprecompile"
    echo "       Re-sign apks           : $tmpsign"
    echo "       Create flashable zip   : $flashable"
    echo "-------------------------------------------------------------"
    echo "TOGGLES:"
    echo "   1   Decompile apks (ON/OFF)"
    echo "   2   Fix sources (ON/OFF)"
    echo "   3   Add translations (ON/OFF)"
    echo "   4   Recompile apks (ON/OFF)"
    echo "   5   Re-sign apks (APK OUT) (ON/OFF)"
    echo "   6   Create flashable zip (ON/OFF)"
    echo "--------------------------------------------------------------"
    echo "ACTIONS:"
    echo "   7   Select source ROM"
    echo "   8   Run script"
    echo "   9   Exit"
    echo "--------------------------------------------------------------"
    echo "CURRENT ROM:"
    echo
    echo -n "CHOOSE OPTION:"
    read menunr
    [ $menunr -eq 1 ] && dec_onoff
    [ $menunr -eq 2 ] && fix_onoff
    [ $menunr -eq 3 ] && tra_onoff
    [ $menunr -eq 4 ] && rec_onoff
    [ $menunr -eq 5 ] && sig_onoff
    [ $menunr -eq 6 ] && fla_onoff
    [ $menunr -eq 7 ] && select_zip
    [ $menunr -eq 8 ] && run_script
    [ $menunr -eq 9 ] && quit
\$\endgroup\$
  • 1
    \$\begingroup\$ The convention in cr.se is that the script in question is added inline. This protects us against the changes in the external hosting parties. \$\endgroup\$ – rahul Aug 7 '12 at 3:10
2
\$\begingroup\$

Good first effort, here are a few general comments.

  • You can replace multiple echos with one cat. that is

    echo "one"
    echo "two"
    

    is equivalent to

    cat <<EOF
    one
    two
    EOF
    

    and is much more cleaner.

  • you can replace

    `id | sed -e 's/(.*//'` != "uid=0" 
    

    with

    `$(id -g) -eq 0`
    
  • You can avoid

    cd xxx
    ...
    cd ..
    

    by doing

    (cd xxx
    ...
    )
    

Also, as a general advice, try to use a consistent formatting.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.