Bump to 2.2.15
[auto-patcher.git] / auto_patcher
blob2383c5cfd5adf64aa51eb22423379d1cbc257f7a
1 #!/bin/bash
3 # version: beta v2.2.15, patches 2012.010.20.
5 # PDroid created by svyat. Official PDroid thread is
6 # http://forum.xda-developers.com/showthread.php?t=1357056
8 # The Auto-Patcher created by pastime1971 and mateorod
9 # Thread and support for the Auto-Patcher can be found at
10 # http://forum.xda-developers.com/showthread.php?p=27589651#post27589651
12 # ChangeLog
13 # 2012.05.00. (v0.1)
14 # ...........
15 # 2012.05.12. (v0.2) allows user to specify a different version of patches
16 # 2012.05.13. (v0.3.0) force "patch -N", remove temp upon failure, generate log output
17 # 2012.05.13. (v0.3.1) revised the generation of updater-script (SGS2)
18 # 2012.05.15. added AOSP patches
19 # 2012.05.15. added crespo-aosp patches, added device-specific patching if patch exists
20 # 2012.05.15. remove restore.zip upon failure
21 # 2012.05.15. (v0.4.1) added CM7 (GB) patches
22 # 2012.05.18. (v0.4.2) added checking for tool commands
23 # 2012.05.21. (v0.4.3) apply device-specific patches only when it is specified
24 # 2012.05.26. added task title to log
25 # 2012.05.31. (v0.4.4) CM9 patches updated, crespo-aosp added
26 # 2012.06.04. (v0.4.5) added checking tools and rom, added aroma installer support, add patch outputs reroute
27 # 2012.06.15. CM9 patches updated
28 # 2012.06.16. (v0.4.6) AOKP patches updated, patch versions restructured
29 # 2012.06.17. (v0.5.0) patcher script reorganized, removed build.prop
30 # 2012.06.17. (v0.6.0) auto patcher script for pdroid and v6 patches
31 # 2012.06.19. (v0.6.1) add patches for CM7.2 Final
32 # 2012.06.20. (v0.6.2) updated CM 7.2 patches
33 # 2012.06.23. (v0.6.3) fix symlinks
34 # 2012.06.26. (v0.6.4) added 0618 patches for cm7 nightlies; removed 0619 cm7.2 final
35 # 2012.06.27. (v0.6.5) added new CM9 patches (20120626)
36 # 2012.06.27. (v0.6.6) added CM7 nightly patches (20120625) (finally!)
37 # 2012.07.02. (v0.7.0) Redid AOSP/AOKP; add Windows support (using Cygwin). Thanks kobik77!
38 # 2012.07.06. (v0.7.1) updated CM9 patches (20120706)
39 # 2012.07.08. (v0.7.2) updated AOKP (20120707) and CM9 (20120708) pdroid patches
40 # 2012.07.11. (v1.0.0) reworked script works on Linux, Mac OSX, and Windows (on Cygwin);
41 # added new ICS/JellyBean support for the new V6-Supercharger
42 # 2012.07.18. (v1.0.1) new CM7/CM9 Pdroid patches (20120718)
43 # 2012.07.23. (v1.0.2) updated CM9 patches (20120723)
44 # 2012.07.23. (v1.0.3) updated CM9 patches (20120724)
45 # 2012.08.06. (v1.5.0) updated smali/baksmali binaries to 1.33
46 # 2012.08.06. Added Pdroid/V6supercharger support for CM10 Jellybean!
47 # 2012.08.06. Improved PDroid stability and efficiency (Thanks CollegeDev)
48 # 2012.08.06. (v1.6.0) added aokp-jb patches for pdroid/v6supercharger, script update
49 # 2012.08.17. (v1.9.5) Added support for pdroid_addon, by CollegeDev.
50 # Added insecure boot.img mod
51 # Added support for aosp-jb (This needs testing, and may not be
52 # a wide patch due to large variation between AOSP roms.
53 # -Please report all experiences!
54 # Updated all v6supercharger patches w/new edits to ProcessList
55 # Major script overhaul in preparation for additional mods!
56 # 2012.08.20. (1.9.6) Conditional support for Official cm10 and aokp-jb builds
57 # 2012.08.23. (1.9.7) Final/confirmed pdroid support for Official CM10 and AOKP-JB
58 # 2012.09.04 (1.9.8) Bundled cygwin-compatible tools (original was corrupt).
59 # For Cygwin, replaced h2b function with simple bash math
60 # conversion (req'd for compat w/ Cygwin tools).
61 # 2012.08.26. (2.0.0) Added .apk patching
62 # Added 3G Dongle support for Nexus 7
63 # TabletUI support for Nexus 7 (maybe more)
64 # Google Voice support for Nexus 7
65 # mods are 3gdongle, tabletUI, voice, or nexus7suite (for all)
66 # Added provisional file copy menu to widen patching base
68 # 2012.09.11. (2.0.1) Cygwin support fixes
69 # 2012.09.11. (2.0.2) Bugfix for 3gdongle (all)
70 # 2012.09.16. (2.1.0) Significant restructuring of core operations; added edit text file
71 # fix for "method-cap" errors in pdroid.
72 # 2012.09.20. (2.1.1) Fix for Phone FC's; OSX support finalized; ContentResolver resolved
73 # 2012.09.28. (2.1.2) Hotfix for failed pdroid patching. Logging improvements.
74 # 2012.10.07. (2.1.5) Final pdroid support for CM7.2 Official Releases
75 # Added aosp-mod patches for pdroid patching of AOSP kangs
76 # Switched to Chainfire's (f)aapt for speed/space reasons
77 # 2012.10.13. (2.1.6) New PDroid patches for aokp-jb and cm10 4.1.2
78 # Revert (f)aapt- temporary due to reported issues
79 # 2012.10.15. (2.17) New (again) CM10 pdroid patches.
80 # Move files to be copied into $p/$R/$B
81 # -This required a bunch of file moving-report broken symlinks.
82 # Some error check on the old RIL delete functions (thanks bundrik)
83 # 2012.10.16. (2.1.7.5) Fix FC on phone with CM10 pdroid patches
84 # 2012.10.19. (2.1.9) Testing CM10 support for PDroid Extension by CollegeDev.
85 # 2012.10.22. (2.2.0) aosp/aokp and cm10 support for pdroid extension by CollegeDev
86 # MAJOR reworking of patches and improved query. only 4 mod types:
87 # cm, aokp, aosp, and pa. See our OP for details.
88 # 2012.10.23. (2.2.0.1) Some fixes for the breakage due to update.
89 # Adjust aapt permissions and add fix_aapt.sh script.
90 # Fix for aosp probing and mod/jb determining. Test Update.
91 # 2012.10.23. (2.2.0.2) Add the '-h' menu back, bump version for log purposes.
92 # 2012.10.24. (2.2.0.3) Fixes for -h menu- Mms.apk as a copy file; aapt permission fix
93 # 2012.10.24. (2.2.0.4) Change Mms.apk to copy file for now, for non-phone devices.
94 # 2012.10.24. (2.2.0.5) Small fixes as we are almost to 2.1.1 stability (I hope)
95 # 2012.10.24. (2.2.1) Final merge of restructure. Fixes for OSX helper scripts
96 # Rename 'extension' to 'pd2.0'
97 # 2012.10.25. (2.2.15) Fix major issue with ICS pdroid patching.
98 # Attempts to bring OSX in-line with other platforms
99 # Improvements to help, usage and error messages.
101 # SHELL PROGRAMMING NOTES:
102 # [ -s FILE ]
103 # [[ -n STRING ]] or [[ -z STRING ]]
105 VERSION="Auto-Patcher v2.2.15"
107 echo ""
108 echo "The $VERSION"
109 echo ""
110 # by pastime1971 and mateorod
113 # pass variables to children
114 set -a
116 # function for converting hex to binary- for testing
117 #function h2b { echo "obase=2; ibase=16; $( echo "$*" | sed -e 's/0x//g' -e 's/\([a-z]\)/\u\1/g' )" | bc; }
119 printusage () {
120 echo " The $VERSION"
121 echo ""
122 echo "usage: $0 [ROMNAME].zip [MODS] [ROMTYPE] [PATCHDATE]"
123 echo ""
124 echo "[MODS] can be pdroid, extension, v6supercharger, tabletUI, etc..."
125 echo "MODS can be combined by separating them with a comma and NO SPACES!!!"
126 echo ""
127 echo "[ROMTYPE] can be cm, aokp, aosp or pa [recommended]"
128 echo "[PATCHDATE] will select older patch versions, e.g 2012103 [optional]"
129 echo ""
130 echo "try \"$0 ROM.ZIP -h\" to see what is available"
131 echo ""
132 exit
135 debug () {
136 echo "! please refer to $LOG for any debugging"
137 echo ""
138 echo "! patching failed."
139 echo "! it may be possible to patch $ROM using other patch version"
140 echo ""
141 cleanup
142 printusage
145 cleanup () {
146 [ -d "$TEMP" ] && \rm -rf "$TEMP"
148 printerror () {
149 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
150 echo "!!! error: $@"
151 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
152 echo ""
153 debug
156 romerror () {
157 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
158 echo "!!! error: $@"
159 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
160 echo ""
161 echo "!!!There are only four supported ROMTYPES:"
162 echo "1) cm 2) aokp 3) aosp 4) pa"
163 echo ""
164 echo "Find out which of the above ROMTYPES your rom is based on and try again!"
165 echo ""
166 echo "**********************************************"
167 cleanup
168 printusage
171 misspatch () {
172 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
173 echo "!! error: $@ "
174 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
175 echo ""
176 echo "!! The Auto-Patcher could not find its patches and/or tools!"
177 echo ""
178 echo "* Did you run ./batch.sh first?"
179 echo ""
180 echo "The patch_patches.tgz and patch_tools.tgz need to be in this directory,"
181 echo " one way or another..."
182 echo ""
183 cleanup
184 printusage
187 misstools () {
188 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
189 echo "Your set-up is missing a dependency."
190 echo "We require JDK, patch and cpio."
191 echo ""
192 echo ""
193 echo "Each distro is different"
194 echo "but you must ensure you have them all installed and try again."
195 echo ""
196 cleanup
197 printusage
200 printtask () {
201 echo "" >> "$LOG"
202 echo $@ | tee -a "$LOG"
205 copyfailed () {
206 echo ""
207 echo "File for which patching failed:"
208 echo "---------------------------------------------"
209 echo ""
210 echo "${FAIL##*/}"
211 echo ""
212 echo "---------------------------------------------"
213 echo ""
214 echo "We can try to add a premade copy of those files."
215 echo "This sometimes works."
216 echo ""
217 echo "But IT MAY NOT WORK!!!"
218 echo ""
219 echo ""
220 askconsent && return 0 || return 1
223 askconsent () {
224 sleep 1
225 echo -n "Do you want to proceed ? (YES/no) "
226 read PROCEED
227 echo "askconsent final = ${PROCEED}: for $FAIL" >> "$LOG"
228 case $PROCEED in
229 [Yy] | [Yy][Ee][Ss])
230 return 0; # successful:
233 return 1; # error
235 esac
238 abspath () {
239 case `uname -s` in
240 CYGWIN*)
241 echo $(cygpath -ua "$1")
243 Darwin)
244 #[[ $(echo $1 | awk '/^\//') == $1 ]] && echo "$1" || echo "$PWD/$1"
245 [[ ${1:0:1} == "/" ]] && echo "$1" || echo "$PWD/$1"
247 Linux)
248 echo $(readlink -f "$1")
251 if [[ ${1:0:1} == "/" ]]; then
252 echo "$1"
253 elif [[ ${1:0:2} == "./" ]]; then
254 echo "$PWD/${1:2}"
255 else
256 echo "$PWD/$1"
259 esac
262 extpath () {
263 case `uname -s` in
264 CYGWIN*)
265 echo $(cygpath -da "$1")
268 echo $(abspath "$1")
270 esac
273 #contains () { for e in "${@:2}"; do [[ "$e" = "$1" ]] && return 0; done; return 1; }
275 parse () { echo "${1%%_*}"; }
277 getbuildprop () {
278 if [ -f system/build.prop ]; then
279 result=(`grep $1 system/build.prop | tr -d '\r' | tr '=' ' '`)
280 echo "${result[1]}"
281 else
282 echo ""
287 aosp_mod_check() {
288 jar -xvf "$ROMX" system/framework/framework2.jar >> $LOG
289 if [ -s system/framework/framework2.jar ]; then
290 AOSP=aosp-mod
291 else
292 AOSP=aosp-jb
294 echo $AOSP
297 proberom () {
298 VID=$(getbuildprop ro.build.version.release)
300 if [ "${VID%.*}" == "2.3" ]; then
301 ANDR=gingerbread
302 RID=cm;
303 elif [ "${VID%.*}" == "4.0" ]; then
304 ANDR=ics
305 MID=$(getbuildprop ro.rommanager.developerid)
306 [[ "$MID" == "" ]] && MID=$(parse $(getbuildprop ro.modversion))
307 DID=$(getbuildprop ro.product.device)
308 [[ "$DID" == "" ]] && DID=$(getbuildprop ro.build.product)
310 case "$MID" in
311 pa|cm9|cm|cyanogenmod*)
312 RID=cm;
314 aokp|teamkang)
315 RID=aokp;
318 RID=aosp;
319 echo "!!! $MID is not supported yet, trying generic latest !!!"
321 esac
322 elif [ "${VID%.*}" == "4.1" ]; then
323 ANDR=jellybean
324 MID=$(getbuildprop ro.build.display.id)
325 [[ "$MID" == "" ]] && MID=$(parse $(getbuildprop ro.build.display.id))
326 DID=$(getbuildprop ro.product.device)
327 [[ "$DID" == "" ]] && DID=$(getbuildprop ro.build.product)
329 case "$MID" in
330 cm10|cm*|cyanogenmod*)
331 RID=cm;
333 aokp*|teamkang)
334 RID=aokp;
336 pa*)
337 RID=pa;
340 RID=`aosp_mod_check`;
342 esac
343 else
344 printerror "unknown ROM version $VID"
346 echo "... result of ROM check: version= $ANDR rom= $RID ..."
349 # NEW # Advanced build date query
350 # This should set the patch date automatically.
352 buildquery() {
353 UTC=$(getbuildprop ro.build.date.utc)
354 case `uname -s` in
355 [Dd]arwin)
356 BDID=`date -r $UTC +%Y%m%d`
357 echo "#### Darwin Build Query used ####" >> "$LOG"
360 BDID=`date -d "1970-01-01 UTC $UTC seconds" +"%Y%m%d"`
361 echo "#### Standard Build Query ####" >> "$LOG"
363 esac
365 if [ $# -gt 3 ]; then
366 BID=${4//,/ };
367 elif
368 [[ ${#PID[@]} -gt 1 ]]; then
369 BID=latest;
370 else
371 for P in ${PID[@]}; do
372 [ -d patches/$P/$ANDR ] && A=$ANDR || A=all
373 [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
374 [ -d patches/$P/$A/$R/$BID ] && B=$BID || B=latest
375 PAR=patches/$P/$A/$R
376 PARB=patches/$P/$A/$R/$B
377 DATES=`ls patches/$P/$A/$R/ | grep '201*'`
378 # attempt to set builds before extant patches to earliest
379 for i in $DATES; do
380 if [[ $i -lt $BID ]]; then
381 BID=$i
383 if [[ $(($BID-$BDID)) -lt 0 ]]; then
384 BID=$i
385 else
386 for g in $DATES; do
387 if [[ $(($BDID-$g)) -eq 0 ]]; then
388 BID=$g
389 break
390 elif [[ $(($BDID-$g)) -gt 0 ]] && [[ $g -gt $BID ]]; then
391 BID=$g
393 done
395 done
396 echo "... $PID patch date has been set to: $BID..."
397 B=$BID
398 done
402 proberil () {
403 RIL=$(getbuildprop ro.telephony.ril_class)
404 [[ -n $RIL ]] && printtask "... result of ROM check: RIL= $RIL"
407 garbage () {
408 unset GARBAGE
409 GARBAGE=($(find $@ -name "*.orig")) && rm -rf ${GARBAGE[@]} && echo "GARBAGE= ${GARBAGE[@]}" >> "$LOG"
410 GARBAGE=($(find $@ -name "*.rej")) && rm -rf ${GARBAGE[@]} && echo "GARBAGE= ${GARBAGE[@]}" >> "$LOG"
413 rominfo () {
414 echo "# VER= $VERSION" >> "$LOG"
415 echo "# OS= $OSTYPE ${BIT}bit" >> "$LOG"
416 echo "# VID= $VID" >> "$LOG"
417 echo "# PID= ${PID[@]}" >> "$LOG"
418 echo "# MID= $MID" >> "$LOG"
419 echo "# RID= $RID" >> "$LOG"
420 echo "# ANDR= $ANDR" >> "$LOG"
421 echo "# BDID= $BDID" >> "$LOG"
422 echo "# DID= $DID" >> "$LOG"
423 echo "# BID= $BID" >> "$LOG"
427 [[ $# -lt 1 || $1 == "-h" || -z $1 ]] && printusage
429 ROOT=$(abspath `dirname "$0"`)
430 TOOL="$ROOT/patch_tools.tgz"
431 PTCH="$ROOT/patch_patches.tgz"
432 ROM=$(abspath "$1")
433 ROMX=$(extpath "$1")
435 if [[ $OSTYPE == "cygwin" ]]; then
436 EXE=".exe"
437 PGFORM="0x"
440 DATE=`date +%Y%m%d%H%M%S`
441 TEMP=$(abspath ./tmp$DATE)
442 LOG=$(abspath ./log$DATE.txt)
443 #echo $ROOT $TOOL $ROM $TEMP "$LOG"
444 echo "ROOT= $ROOT" >> "$LOG"
445 echo "TOOL= $TOOL" >> "$LOG"
446 echo "ROM= $ROM" >> "$LOG"
447 echo "ROMX= $ROMX" >> "$LOG"
449 \rm -f restore.zip update.zip
451 CHK=$(getconf LONG_BIT)
452 if [[ $CHK = "64" ]]; then
453 BIT=64
454 else [[ $BIT = "32" ]];
455 BIT=32
459 printtask "### verify tools ###"
460 which patch >> "$LOG" || misstools "Missing \"patch diff command\""
461 which tar >> "$LOG" || misstools "Missing \"tar archive command\""
462 which jar >> "$LOG" || misstools "Missing \"jar (JDK)\""
463 which java >> "$LOG" || misstools "Missing \"java (JRE)\""
464 which cpio >> "$LOG" || misstools "Missing \"cpio archive command\""
465 [ -f "$TOOL" ] || misspatch "patch tool is missing"
466 [ -f "$PTCH" ] || misspatch "patch patches are missing"
467 printtask "### verify ROM ###"
468 [ -f "$ROM" ] || printerror "Cannot find $ROM"
469 echo "### running $0 $@" >> "$LOG"
470 mkdir "$TEMP"
471 cd "$TEMP"
473 PATH="$TEMP:$PATH"
474 export PATH
475 echo "PATH= $PATH" >> "$LOG"
477 printtask "### unzip patch tools ###"
478 tar xvzf "$TOOL" > /dev/null || printerror "failed extracting tools !!!"
480 # Get OS specific tools
481 case `uname -s` in
482 CYGWIN*)
483 \mv cygwin/aapt.exe .
484 \mv cygwin/*.exe boot/
485 \rm -rf cygwin # why? If you want to we can keep/add it to mac/linux, though.
487 [Dd]arwin)
488 \mv mac/aapt .
490 [Ll]inux)
491 \mv linux/aapt .
494 \mv linux/aapt .
496 esac
497 # Ensure aapt permissions - possibly to one day be expanded to all tools in a function.
498 chmod a+x aapt$EXE
500 printtask "### unzip patch patches ###"
501 tar xvzf "$PTCH" > /dev/null || printerror "failed extracting patches !!!"
503 # we need build.prop for both RIL probing and ROM probing
504 jar -xvf "$ROMX" system/build.prop 2>> "$LOG" || printerror "failed extracting build.prop from $ROMX !!!"
506 # rom + ril type
507 proberil
508 printtask "### probe rom type ###"
509 proberom
511 # rom type
512 if [ $# -gt 2 ]; then
513 RID=$3
514 case "$RID" in
515 cm10|cm*|cyanogenmod*)
517 aokp*|teamkang)
518 RID=aokp;
520 pa*)
521 RID=pa;
523 aosp*)
524 RID=`aosp_mod_check`;
527 romerror "$RID is not a valid ROMTYPE!!"
529 esac
530 echo "... Using $RID patches ..."
531 else
532 echo "... Using results of ROM check ..."
535 # patch type
536 if [ $# -gt 1 ]; then
537 PIL=${2//,/ }
538 else
539 #PIL=`ls patches/`
540 PIL="pdroid v6supercharger"
541 echo "No mods selected. Defaulting to the v6 and Pdroid, just because we can."
543 # a hack to make sure pdroid is the first mod
544 [[ ${PIL[@]} =~ pdroid ]] && PID=(pdroid)
545 for P in ${PIL[@]}; do
546 [[ ${PID[@]} =~ $P ]] || PID=(${PID[@]} $P)
547 done
549 # Build date
550 if [ $# -gt 3 ]; then
551 BID=$4
552 # even here, we should try and create a function that matches as close to the above BID as possible
553 echo "... $PID patch date set manually to $BID ..."
554 else
555 buildquery
558 # echo ROM info
559 rominfo
561 if [[ ( "$PID" == "-h" || "$RID" == "-h" || "$BID" == "-h" ) ]]; then
562 case `uname -s` in
563 [Dd]arwin)
564 LIST="find . -name '*.sh' 'common' 'provisionals' '*.txt' '*.awk' -prune"
567 LIST="ls --ignore="common" --ignore="*.sh" --ignore "provisionals" --ignore="*.txt" --ignore "*.awk""
569 esac
570 echo "### supported patch candidates ###"
571 MODS=$(ls --ignore "extension" patches)
572 #echo ${MODS[@]}
574 for M in ${MODS[@]}; do
575 AND=$($LIST patches/$M)
576 #echo $M: ${AND[@]}
577 for A in ${AND[@]}; do
578 RID=$($LIST patches/$M/$A)
579 #echo $M: $A: ${RID[@]}
580 for R in ${RID[@]}; do
581 BID=$($LIST -v patches/$M/$A/$R)
582 echo $M: $A: $R: ${BID[@]}
583 done
584 done
585 done
586 echo ""
587 echo "*****************************************************************"
588 echo ""
589 echo "Note: There are ONLY 4 ROMTYPES: [pa, cm, aokp, and aosp]."
590 echo "The Auto-Patcher will automatically slot if you need aosp-jb or aosp-mod"
591 echo ""
593 else
594 for P in ${PID[@]}; do
595 [ -d patches/$P ] || printerror "$P is not available"
596 [ -d patches/$P/$ANDR ] && R=$ANDR || A=all
597 [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
598 #[ -d patches/$P/$A/$R/$BID ] && B=$BID || B=latest
600 unset FILES
601 [ -f patches/$P/files.txt ] && FILES=$(cat patches/$P/files.txt)
602 [ -f patches/$P/$A/files.txt ] && FILES=$(cat patches/$P/$A/files.txt)
603 [ -f patches/$P/$A/$R/files.txt ] && FILES=$(cat patches/$P/$A/$R/files.txt)
604 for FILE in ${FILES[@]}; do
605 case "$FILE" in
606 +system/*)
607 [[ ${CID[@]} =~ ${FILE:1} ]] && printerror "${FILE:1} is already being copied" || CID=(${CID[@]} ${FILE:1})
609 -system/*)
610 [[ ${EID[@]} =~ ${FILE:1} ]] || EID=(${EID[@]} ${FILE:1})
612 system/*.jar)
613 [[ ${JID[@]} =~ $FILE ]] || JID=(${JID[@]} $FILE)
615 system/*.apk)
616 [[ ${AID[@]} =~ $FILE ]] || AID=(${AID[@]} $FILE)
618 *.img)
619 [[ ${IID[@]} =~ $FILE ]] || IID=(${IID[@]} $FILE)
621 system/*)
622 [[ ${TID[@]} =~ $FILE ]] || TID=(${TID[@]} $FILE)
624 esac
625 done
626 done
627 echo "### DELETE FILES= ${EID[@]}" >> "$LOG"
628 echo "### COPY FILES= ${CID[@]}" >> "$LOG"
629 echo "### JAR FILES= ${JID[@]}" >> "$LOG"
630 echo "### APK FILES= ${AID[@]}" >> "$LOG"
631 echo "### IMG FILES= ${IID[@]}" >> "$LOG"
632 echo "### TXT FILES= ${TID[@]}" >> "$LOG"
634 printtask "### unzip rom ###"
635 # extract system files
636 jar -xvf "$ROMX" ${JID[@]} ${AID[@]} ${IID[@]} ${TID[@]} 2>> "$LOG" || printerror "failed extracting ${JID[@]} ${AID[@]} ${IID[@]} from $ROMX !!!"
637 #\cp -a system/ backup # only for debugging purpose
639 printtask "### create updater script ###"
640 jar -xvf "$ROMX" META-INF/com/google/android/updater-script META-INF/com/google/android/update-binary
641 jar -xvf "$ROMX" META-INF/com/google/android/update-binary-installer 1> /dev/null 2> /dev/null
642 if [ -f META-INF/com/google/android/update-binary-installer ]; then
643 printtask "... using Aroma installer ..."
644 \mv META-INF/com/google/android/update-binary-installer META-INF/com/google/android/update-binary
645 else
646 printtask "... using Clockworkmod installer ..."
648 if [ -f META-INF/com/google/android/update-binary ]; then
649 #echo "# original updater-script" >> "$LOG"
650 #cat META-INF/com/google/android/updater-script >> "$LOG"
652 cat META-INF/com/google/android/updater-script | grep ^mount | grep "/system" | head -n 1 > updater-script
653 [ -s updater-script ] || echo "run_program(\"/sbin/busybox\", \"mount\", \"/system\");" > updater-script
655 for FILE in ${EID[@]}; do
656 # restore the original
657 jar -xvf "$ROMX" $FILE 2> /dev/null || printtask "!!! warning $FILE cannot be restored !!!"
658 done
659 for FILE in ${CID[@]}; do
660 # replace with the original or delete it
661 jar -xvf "$ROMX" $FILE 2> /dev/null || echo "delete(\"/$FILE\");" >> updater-script
662 done
664 echo "package_extract_dir(\"system\", \"/system\");" >> updater-script
665 # add permission management
666 cat META-INF/com/google/android/updater-script | grep set_perm | grep "/system" >> updater-script
667 # TODO: remove if we don't need it any more
668 for P in ${PID[@]}; do
669 if [ -s patches/$P/$A/set_perm.txt ]; then
670 echo "...adding permissions..."
671 cat patches/$P/$A/set_perm.txt >> updater-script
673 done
675 cat META-INF/com/google/android/updater-script | grep ^unmount | grep "/system" | head -n 1 >> updater-script
677 [[ ${IID[@]} =~ boot.img ]] && cat META-INF/com/google/android/updater-script | grep "boot.img" >> updater-script
679 cat updater-script > META-INF/com/google/android/updater-script
681 echo "# generated updater-script" >> "$LOG"
682 cat updater-script >> "$LOG"
683 else
684 # this is not enough any more
685 jar -xvf update-template.zip META-INF/com/google/android/update-binary META-INF/com/google/android/updater-script
688 printtask "### create restore.zip ###"
689 jar -cfv tmp.zip ${JID[@]} ${AID[@]} ${IID[@]} META-INF > /dev/null 2>> "$LOG"
690 java -jar signapk.jar updatecert.pem updatekey.pk8 tmp.zip signed.zip 2>> "$LOG"
691 \mv signed.zip restore.zip
692 \rm -f tmp.zip
694 printtask "### decompile system files ###"
696 # decompile system files
697 for FILE in ${JID[@]}; do
698 JAR=${FILE##*/}
699 printtask "... decompile $JAR ..."
700 java -jar baksmali.jar -b -a 15 -o tmp/${FILE}.out/smali ${FILE} >> "$LOG" 2>> "$LOG"
701 if [ $FILE == "system/framework/framework.jar" ]; then
702 printtask "... extract preloaded-classes ..."
703 jar -xvf ${FILE} preloaded-classes >> "$LOG" 2>> "$LOG"
704 \mv preloaded-classes tmp/${FILE}.out/
706 done
707 for FILE in ${AID[@]}; do
708 APK=${FILE##*/}
709 printtask "... decompile $APK ..."
710 if [ $FILE == "system/framework/framework-res.apk" ]; then
711 printtask "... installing framework files ..."
712 java -jar apktool.jar if $FILE
714 java -jar apktool.jar d -f $FILE tmp/$FILE.out
716 done
717 for FILE in ${IID[@]}; do
718 printtask "### unpacking $FILE ###"
719 case "$FILE" in
720 boot.img)
721 if [ -f boot.img ]; then
722 cd boot
723 ./unpackbootimg$EXE -i ../boot.img >> "$LOG" 2>> "$LOG"
724 mkdir ramdisk
725 cd ramdisk
726 gzip -dc ../boot.img-ramdisk.gz | cpio -i >> "$LOG" 2>> "$LOG"
727 cd ../../
728 else
729 printerror "A mod needs to patch boot.img! This rom does not contains boot.img."
733 printerror "cannot process $FILE"
735 esac
736 done
739 # apply system modification
741 for P in ${PID[@]}; do
742 [ -d patches/$P/$ANDR ] && A=$ANDR || A=all
743 [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
744 [ -d patches/$P/$A/$R/$BID ] && B=$BID || B=latest
745 PAR=patches/$P/$A/$R
746 PARB=patches/$P/$A/$R/$B
748 unset FILES COPY DELS JARS APKS IMGS TXTS
749 [ -f patches/$P/files.txt ] && FILES=$(cat patches/$P/files.txt)
750 [ -f patches/$P/$A/files.txt ] && FILES=$(cat patches/$P/$A/files.txt)
751 [ -f patches/$P/$A/$R/files.txt ] && FILES=$(cat patches/$P/$A/$R/files.txt)
752 for FILE in ${FILES[@]}; do
753 case $FILE in
754 +system/*)
755 COPY=(${COPY[@]} ${FILE:1})
757 -system/*)
758 DELS=(${DELS[@]} ${FILE:1})
760 system/*.jar)
761 JARS=(${JARS[@]} $FILE)
763 system/*.apk)
764 APKS=(${APKS[@]} $FILE)
766 *.img)
767 IMGS=(${IMGS[@]} $FILE)
769 system/*)
770 TXTS=(${TXTS[@]} $FILE)
772 esac
773 done
775 [[ -n ${COPY[@]} ]] && printtask "### copy patch $P/$A/$R/$B ###"
776 for FILE in ${COPY[@]}; do
777 printtask "... copying $FILE ..."
778 DEST=${FILE%/*}
779 mkdir -p ./$DEST
780 if [ -e patches/$P/$A/$R/$B/$FILE ]; then
781 [ -e $FILE ] && printtask "... overwriting $FILE from $P ..."
782 \cp -av patches/$P/$A/$R/$B/$FILE ./$DEST >> "$LOG"
783 else
784 printerror "$FILE is missing in the patch"
786 done
788 # unconditional smali copying
789 [ -f patches/$P/smali.txt ] && SMALI=$(cat patches/$P/smali.txt)
790 [ -f patches/$P/$A/smali.txt ] && SMALI=$(cat patches/$P/$A/smali.txt)
791 [ -f patches/$P/$A/$R/smali.txt ] && SMALI=$(cat patches/$P/$A/$R/smali.txt)
792 [ -f patches/$P/$A/$R/$B/smali.txt ] && SMALI=$(cat patches/$P/$A/$R/$B/smali.txt)
793 MOVE=${SMALI##*/}
794 for SMALI in ${SMALI[@]}; do
795 printtask "... overwriting $MOVE ..."
796 \cp -av patches/$P/$ANDR/provisionals/$SMALI tmp/$SMALI >> "$LOG"
797 done
800 [[ -n ${DELS[@]} ]] && printtask "### delete patch $P/$A/$R/$B ###"
801 for FILE in ${DELS[@]}; do
802 printtask "... deleting $FILE ..."
803 \rm -rf ./$FILE
804 done
806 [[ -n ${JARS[@]} ]] && printtask "### jar patch $P/$A/$R/$B ###"
807 for FILE in ${JARS[@]}; do
808 JAR=${FILE##*/}
809 NAME=${JAR%.jar}
810 printtask "... apply $NAME.patch ..."
811 patch -Nls -p1 -d tmp/${FILE}.out < $PARB/${NAME}.patch 2>&1 > ${JAR}.log
812 cat ${JAR}.log | tee -a "$LOG"
813 case "$NAME" in
814 framework)
815 # patch preloaded-classes only if a patch exists
816 if [ -f $PARB/preloaded.patch ]; then
817 printtask "... apply preloaded.patch ..."
818 patch -Nls -p1 -d tmp/${FILE}.out < $PARB/preloaded.patch 2>&1 > preloaded.log
819 cat preloaded.log | tee -a "$LOG"
820 [[ `grep FAILED preloaded.log` != "" ]] && printerror "failed preloaded.patch"
824 echo -n "" # do nothing else
826 esac
827 # substitute failed smali with premade jar
828 unset FAILS
829 FAILS=(`grep FAILED ${JAR}.log | sed -e 's/.*smali\///' | sed -e 's/\.rej//'`)
830 if [[ -n ${FAILS[@]} ]]; then
831 printtask "!!! PATCHING FAILED FOR ${FAILS[@]} !!!"
832 echo "### replace ${FAILS[@]} ###" > ${JAR}.log
833 for FAIL in ${FAILS[@]}; do
834 echo "!!! patch error for $FAIL !!!" >> "$LOG"
835 #cat tmp/$FILE.out/smali/${FAIL}.rej >> "$LOG"
836 [[ ${SID[@]} =~ $FAIL ]] && printerror "$FAIL was already replaced before"
837 [ -f patches/$P/$ANDR/provisionals/${FILE}.out/smali/${FAIL} ] || printerror "failed $FAIL in $JAR cannot be remedied"
838 copyfailed || printerror "failed $NAME.patch"
839 printtask "... replace $FAIL ..."
840 \rm -rf tmp/${FILE}.out/smali/${FAIL} tmp/${FILE}.out/smali/${FAIL}.orig tmp/${FILE}.out/smali/${FAIL}.rej
841 \cp -av patches/$P/$ANDR/provisionals/${FILE}.out/smali/${FAIL} tmp/${FILE}.out/smali/${FAIL} >> "$LOG" || echo "FAILED to cp $FAIL" >> ${JAR}.log
842 SID=(${SID[@]} $FAIL)
843 done
845 [[ `grep FAILED ${JAR}.log` != "" ]] && printerror "failed $NAME.patch"
846 garbage tmp/${FILE}.out
847 if [ -f patches/$P/$A/$R/$B/{JAR}.sh ]; then
848 printtask "... apply $JAR.sh ..."
849 . patches/$P/$A/$R/$B/{JAR}.sh 2>&1 > ${JAR}.log
850 cat ${JAR}.log | tee -a "$LOG"
852 done
854 [[ -n ${APKS[@]} ]] && printtask "### apk patch $P/$R/$B ###"
855 for FILE in ${APKS[@]}; do
856 APK=${FILE##*/}
857 if [ -d "tmp/${FILE}.out" ] ; then
858 printtask "... apply $APK.patch ..."
859 patch -Nls -p2 -d tmp/${FILE}.out < $PARB/${APK}.patch 2>&1 >> ${APK}.log
860 cat ${APK}.log | tee -a "$LOG"
861 [[ `grep FAILED ${APK}.log` != "" ]] && printerror "failed $APK.patch"
862 garbage tmp/${FILE}.out
863 else
864 echo "No $APK in your ROM. Not able to patch this file."
865 echo "## No $APK in ROM ###" >> "$LOG"
867 done
868 [[ -n ${IMGS[@]} ]] && printtask "### img patch $P/$A/$R/$B ###"
869 for FILE in ${IMGS[@]}; do
870 IMG=${FILE##*/}
871 #NAME=${IMG%.img}
872 echo "... applying $IMG.patch ..."
873 patch -Nls -p1 -d boot/ramdisk < $PARB/${IMG}.patch >> "$LOG" || printerror "failed patching $FILE"
874 # Special Instructions- married to boot.img patching by necessity, not design. When
875 # added to it's own section, it only runs for one mod. WIP.
876 #if [ -f patches/$P/$R/special_instructions.sh ]; then
877 # . patches/$P/$R/special_instructions.sh
878 #elif [ -f patches/$P/special_instruction.sh ]; then
879 # . patches/$P/special_instructions.sh
881 done
883 [[ -n ${TXTS[@]} ]] && printtask "### text patch $P/$A/$R/$B ###"
884 for FILE in ${TXTS[@]}; do
885 TXT=${FILE##*/}
886 if [[ -f $PARB/${TXT}.patch ]]; then
887 printtask "... apply $TXT.patch ..."
888 patch -Nls -p2 -d system/ < $PARB/${TXT}.patch 2>&1 > ${TXT}.log
889 #cat ${TXT}.log | tee -a "$LOG"
890 [[ `grep FAILED ${TXT}.log` != "" ]] && printerror "failed $TXT.patch"
892 if [[ -f $PAR/${TXT}.awk ]]; then
893 printtask "... apply $TXT.awk ..."
894 awk -f $PAR/${TXT}.awk $FILE > text || printerror "failed $TXT.awk"
895 mv text $FILE
897 if [[ -f $PAR/${TXT}.sed ]]; then
898 printtask "... apply $TXT.sed ..."
899 sed -f $PAR/${TXT}.sed $FILE > text || printerror "failed $TXT.sed"
900 mv text $FILE
902 if [[ -f $PAR/${TXT}.sh ]]; then
903 printtask "... apply $TXT.sh ..."
904 . $PAR/${TXT}.sh 2>&1 >> ${TXT}.log || printerror "failed $TXT.sh"
906 garbage system/
907 done
908 done
910 # Rebuilding system files
912 [[ -n ${JID[@]} ]] && printtask "### rebuild jar ###"
913 for FILE in ${JID[@]}; do
914 JAR=${FILE##*/}
915 printtask "... rebuild $JAR ..."
916 java -Xmx512M -jar smali.jar -a 15 tmp/${FILE}.out/smali -o tmp/${FILE}.out/classes.dex >> "$LOG" 2>> "$LOG"
917 [ -f tmp/${FILE}.out/classes.dex ] || printerror "failed compiling $JAR !!!"
918 jar -ufv ${FILE} -C tmp/${FILE}.out/ classes.dex >> "$LOG" 2>> "$LOG"
919 if [ $FILE == "system/framework/framework.jar" ]; then
920 jar -ufv ${FILE} -C tmp/${FILE}.out/ preloaded-classes >> "$LOG" 2>> "$LOG"
922 done
924 [[ -n ${AID[@]} ]] && printtask "### rebuild apk ###"
925 for FILE in ${AID[@]}; do
926 APK=${FILE##*/}
927 printtask "... rebuild $APK ..."
928 java -jar apktool.jar b tmp/${FILE}.out >> "$LOG" 2>> "$LOG" || printerror "failed compiling $FILE !!!"
929 jar -ufv $FILE -C tmp/${FILE}.out/build/apk/ . > /dev/null 2>> "$LOG"
930 #\rm -r tmp/${FILE}.out
931 done
933 [[ -n ${IID[@]} ]] && printtask "### rebuild img ###"
934 for FILE in ${IID[@]}; do
935 printtask "### rebuild $FILE ###"
936 case $FILE in
937 boot.img)
938 # Rebuilding boot.img
939 cd boot
940 printtask "... rebuilding ramdisk ..."
941 ./mkbootfs$EXE ./ramdisk | gzip > ramdisk-new.gz
943 printtask "... composing boot.img ..."
944 BASE=$(cat boot.img-base)
945 # convert hex to decimal in cygwin by prefixing "0x" and C-style arithmetic
946 PGSIZE=$((${PGFORM}$(cat boot.img-pagesize)))
947 CMDLINE=$(cat boot.img-cmdline)
948 echo "cmdline = no_console_suspend=1 console=null $CMDLINE" >> "$LOG"
949 echo "pagesize = $PGSIZE" >> "$LOG"
950 echo "base = $BASE" >> "$LOG"
952 printtask "... rebuilding boot.img ..."
953 ./mkbootimg$EXE --kernel boot.img-zImage --ramdisk ramdisk-new.gz --cmdline "no_console_suspend=1 console=null $CMDLINE" --base "$BASE" --pagesize "$PGSIZE" -o ../boot.img || printerror "failed building boot.img"
954 cd ..
957 printerror "cannot process $FILE"
959 esac
960 done
962 printtask "### create update.zip ###"
963 jar -cfv tmp.zip ${IID[@]} ${JID[@]} ${AID[@]} ${CID[@]} ${TID[@]} META-INF >> "$LOG" 2>> "$LOG"
964 java -jar signapk.jar updatecert.pem updatekey.pk8 tmp.zip update.zip 2>> "$LOG"
965 #\rm -f tmp.zip
967 #echo "# miscellaneous log" >> "$LOG"
969 \mv restore.zip update.zip ..
970 echo "### finished ###"
971 echo "flash update.zip to install the modified"
972 echo "flash restore.zip to restore the original"
974 # add announcement
975 for P in ${PID[@]}; do
976 [ -d patches/$P/$ANDR ] && A=$ANDR || A=all
977 [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
978 if [ -f patches/$P/$A/$R/README.txt ]; then
979 cat patches/$P/$A/$R/README.txt
980 elif [ -f patches/$P/$A/README.txt ]; then
981 cat patches/$P/$A/README.txt
983 done
985 cd ..
986 \rm -rf "$TEMP"