Fix symlinks from common rename
[auto-patcher.git] / auto_patcher
blob2cc57852f6510a134e82bd83c4f58cf87d2a0384
1 #!/bin/bash
3 # version: beta v2.5.7, patches 2013.01.25.
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; 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
27 # add patch outputs reroute
28 # 2012.06.15. CM9 patches updated
29 # 2012.06.16. (v0.4.6) AOKP patches updated, patch versions restructured
30 # 2012.06.17. (v0.5.0) patcher script reorganized, removed build.prop
31 # 2012.06.17. (v0.6.0) auto patcher script for pdroid and v6 patches
32 # 2012.06.19. (v0.6.1) add patches for CM7.2 Final
33 # 2012.06.20. (v0.6.2) updated CM 7.2 patches
34 # 2012.06.23. (v0.6.3) fix symlinks
35 # 2012.06.26. (v0.6.4) added 0618 patches for cm7 nightlies; removed 0619 cm7.2 final
36 # 2012.06.27. (v0.6.5) added new CM9 patches (20120626)
37 # 2012.06.27. (v0.6.6) added CM7 nightly patches (20120625) (finally!)
38 # 2012.07.02. (v0.7.0) Redid AOSP/AOKP; add Windows support (using Cygwin). Thanks kobik77!
39 # 2012.07.06. (v0.7.1) updated CM9 patches (20120706)
40 # 2012.07.08. (v0.7.2) updated AOKP (20120707) and CM9 (20120708) pdroid patches
41 # 2012.07.11. (v1.0.0) reworked script works on Linux, Mac OSX, and Windows (on Cygwin);
42 # added new ICS/JellyBean support for the new V6-Supercharger
43 # 2012.07.18. (v1.0.1) new CM7/CM9 Pdroid patches (20120718)
44 # 2012.07.23. (v1.0.2) updated CM9 patches (20120723)
45 # 2012.07.23. (v1.0.3) updated CM9 patches (20120724)
46 # 2012.08.06. (v1.5.0) updated smali/baksmali binaries to 1.33
47 # 2012.08.06. Added Pdroid/V6supercharger support for CM10 Jellybean!
48 # 2012.08.06. Improved PDroid stability and efficiency (Thanks CollegeDev)
49 # 2012.08.06. (v1.6.0) added aokp-jb patches for pdroid/v6supercharger, script update
50 # 2012.08.17. (v1.9.5) Added support for pdroid_addon, by CollegeDev.
51 # Added insecure boot.img mod
52 # Added support for aosp-jb (This needs testing, and may not be
53 # a wide patch due to large variation between AOSP roms.
54 # -Please report all experiences!
55 # Updated all v6supercharger patches w/new edits to ProcessList
56 # Major script overhaul in preparation for additional mods!
57 # 2012.08.20. (v1.9.6) Conditional support for Official cm10 and aokp-jb builds
58 # 2012.08.23. (v1.9.7) Final/confirmed pdroid support for Official CM10 and AOKP-JB
59 # 2012.09.04 (v1.9.8) Bundled cygwin-compatible tools (original was corrupt).
60 # For Cygwin, replaced h2b function with simple bash math
61 # conversion (req'd for compat w/ Cygwin tools).
62 # 2012.08.26. (v2.0.0) Added .apk patching
63 # Added 3G Dongle support for Nexus 7
64 # TabletUI support for Nexus 7 (maybe more)
65 # Google Voice support for Nexus 7
66 # mods are 3gdongle, tabletUI, voice, or nexus7suite (for all)
67 # Added provisional file copy menu to widen patching base
68 # 2012.09.11. (v2.0.1) Cygwin support fixes
69 # 2012.09.11. (v2.0.2) Bugfix for 3gdongle (all)
70 # 2012.09.16. (v2.1.0) Significant restructuring of core operations; added edit text file
71 # fix for "method-cap" errors in pdroid.
72 # 2012.09.20. (v2.1.1) Fix for Phone FC's; OSX support finalized; ContentResolver resolved
73 # 2012.09.28. (v2.1.2) Hotfix for failed pdroid patching. Logging improvements.
74 # 2012.10.07. (v2.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. (v2.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. (v2.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. (v2.1.7.5) Fix FC on phone with CM10 pdroid patches
84 # 2012.10.19. (v2.1.9) Testing CM10 support for PDroid Extension by CollegeDev.
85 # 2012.10.22. (v2.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.26. (v2.3.0) Final (LTS) version. Stable for now until upstream breaks any patches
89 # Thanks to KicknGuitar for the OSX testing!
90 # 2012.11.01. (v2.3.1) Fix for pdroid RIL deletion for older roms
91 # Better Build query logic and help messages
92 # 2012.11.02. (v2.3.5) Add Evervolv support for all mods- Thanks to new maintainer poncik!
93 # Fix method cap for pdroid/pd2.0 for AOKP and kangs
94 # 2012.11.06. (v2.4.0) Update pd2.0 patches to latest pd2.0 release- all except Evervolv
95 # add GsmService to smali.txt; improve rom probe
96 # 2012.11.08. (v2.4.2) Add Evervolv support for pd2.0; Device-specific scripts
97 # Will display error if using a mod on an inapplicable rom
98 # 2012.11.09. (v2.4.5) Error catch for random method issues, fix pd2.0 Evervolv and aosp-jb
99 # 2012.11.17. (v2.4.6) Fix pdroid for CM7. FINALLY!.
100 # 2012.11.17. (v2.4.7) Internal debugging improvements
101 # 2013.01.13. (v2.5.0) Added 'openpdroid' mod --Open-Source Pdroid framework
102 # BIG Improvements to help and error-catch. Try ./auto_patcher -h
103 # 2013.01.15. (v2.5.1) OpenPDroid support for Evervolv and ParanoidAndroid, hotfix for Mms
104 # 2013.01.15. (v2.5.2) Fix for cygwin temp files (kobik), provisional telephony files added
105 # 2013.01.16. (v2.5.3) SlimRom support. Rearrange structure for ApG.
106 # 2013.01.23. (v2.5.4) Fix small bugs related to rearrange
107 # 2013.01.25. (v2.5.7) 4.2 TabletUI support [cm,aokp,aosp,Evervolv]
109 # SHELL PROGRAMMING NOTES:
110 # [ -s FILE ]
111 # [[ -n STRING ]] or [[ -z STRING ]]
113 VERSION="v2.5.7"
114 CURRENT_ANDROID="4.2.1"
116 # pass variables to children
117 set -a
119 printusage_help () {
120 PID="openpdroid,3gdongle" # default example
121 brief_help
124 brief_help () {
125 echo " usage: ./auto_patcher <ROM> <MODS> <ROMTYPE>"
126 echo " example: ./auto_patcher CM10.zip $PID cm"
127 echo ""
128 echo "Use ./auto_patcher -h for the help menu"
129 echo ""
130 cleanup
131 exit
134 usage_help () {
135 PID=mods
136 mods_display
137 echo "Multiple MODS can be combined by separating them with a comma and NO SPACES!!!"
138 echo ""
139 echo " usage: ./auto_patcher <ROM> <MODS> <ROMTYPE>"
140 echo ""
141 echo " example: ./auto_patcher CM10.zip openpdroid,3gdongle cm"
142 echo ""
143 usage_more_help
146 usage_more_help () {
147 echo "* press 'q' available patches or CTRL-c to quit the help menu *"
150 display_help () {
151 cd patches
152 echo ""
153 echo "AVAILABLE PATCHES"
154 echo ""
155 echo "*******************************************************************"
156 echo "Note: There are ONLY 4 ROMTYPES: [pa, cm, aokp, and aosp]."
157 echo ""
158 find -H * \( -name aokp -o -name cm -o -name aosp -o -name pa -o -name all -o -name generic \) -prune | column -c 85 | more -d -7
159 echo ""
160 echo "!!! Do not put Android Version number on command line !!!!"
161 echo "*****************************************************************"
162 echo "Specific auto_patcher script options"
163 echo " ./auto_patcher -h mods Lists mods"
164 echo " -h <MODNAME> Specific <MOD> info"
165 echo " -h advanced Advanced usage+lists all available patch dates"
166 echo ""
167 cleanup
168 exit
171 romtype_help () {
172 # From rom_error (bad romtype) and date_error (patch date w/o specifying romtype)
173 echo ""
174 echo "!!!There are only four supported ROMTYPES: 1) cm 2) aokp 3) aosp 4) pa"
175 echo ""
176 echo "Find out which of the above ROMTYPES your rom is based on and try again!"
177 echo "**********************************************"
178 echo ""
179 cleanup
180 brief_help
183 advanced_help () {
184 echo ""
185 echo "The Auto-Patcher Advanced Usage"
186 echo "*********************************"
187 echo ""
188 echo "For those of you determined to try everything..."
189 echo ""
190 echo "You can also try adding a patch date as a final argument."
191 echo ""
192 echo " ./auto_patcher <ROM> <MOD> <ROMTYPE> <DATE>"
193 echo ""
194 echo "Please note: There are only four ROMTYPES: {pa, aokp, aosp and cm}"
195 echo " -The various appellations after that are for internal use only!"
196 echo ""
197 echo "The Auto-Patcher has a pretty great rom probe, specifying the date is"
198 echo " deprecated...you don't need to, we promise."
199 echo ""
202 cleanup () {
203 [[ -n $ROOT ]] && \rm -rf "$ROOT"/tmp*
206 garbage () {
207 unset GARBAGE
208 GARBAGE=($(find $@ -name "*.orig")) && rm -rf ${GARBAGE[@]} && echo "GARBAGE= ${GARBAGE[@]}" >> "$LOG"
209 GARBAGE=($(find $@ -name "*.rej")) && rm -rf ${GARBAGE[@]} && echo "GARBAGE= ${GARBAGE[@]}" >> "$LOG"
213 print_error () {
214 # General purpose error. Used for errors in main, as opposed to probe or set-up.
215 echo ""
216 printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
217 printtask "!!! error: $@"
218 printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
219 echo ""
220 brief_help
223 smali_error () {
224 # Smali binary error. Problem often goes away on second run.
225 echo ""
226 printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
227 printtask "!!! error: $@"
228 printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
229 echo ""
230 if [[ `grep 'method index is too large' "$LOG"` != "" ]]; then
231 printtask "!!! Problem with method cap !!!"
232 printtask "... attempting fix ..."
233 . patches/.common/framework.jar.sh 2>&1 > ${JAR}.log
234 printtask "... rebuild framework.jar ..."
235 java -Xmx512M -jar smali.jar -a $API tmp/${FILE}.out/smali -o tmp/${FILE}.out/classes.dex >> "$LOG" 2>> "$LOG"
236 [ -f tmp/${FILE}.out/classes.dex ] || print_error "Could not resolve method cap issue- please submit log"
237 else
238 printtask "!!! This error is generally due to an unknown bug in the smali binary!"
239 echo "In our experience, it goes away if you run the patcher a second time."
240 echo " Please run the Auto-Patcher with the same command once again."
241 echo ""
242 echo "If after the third attempt the same thing happens,"
243 echo " please upload your logfile to our support thread!"
244 echo ""
245 cleanup
246 exit
250 mismatch_error () {
251 # Function that exposes attempts to apply a mod to an upsupported Android version
252 echo ""
253 printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
254 printtask "!!! error: $@"
255 printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
256 echo ""
257 cleanup
258 brief_help
261 date_error () {
262 # Remind users to include a romtype if they are going to specify a patch date
263 echo ""
264 printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
265 printtask "!!! error: $@"
266 printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
267 echo ""
268 echo "Please put a ROMTYPE before the patch date and run again!!"
269 romtype_help
272 rom_error () {
273 # For when an unsupported romtype is manually entered on the command-line
274 echo ""
275 printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
276 printtask "!!! error: $@"
277 printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
278 romtype_help
281 conflict_error () {
282 # For conflicting packages
283 echo ""
284 printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
285 printtask "!!! error: $@"
286 printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
287 cleanup
288 PID=mods
289 mods_info
292 misspatch_error () {
293 printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
294 printtask "!! error: $@ "
295 printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
296 echo ""
297 printtask "!! The Auto-Patcher could not find its patches and/or tools!"
298 echo ""
299 echo "* Did you run ./batch.sh first?"
300 echo ""
301 echo "The patch_patches.tgz and patch_tools.tgz need to be in this directory,"
302 echo " one way or another..."
303 echo ""
304 cleanup
305 brief_help
308 misstools_error () {
309 printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
310 printtask "!! error: $@ "
311 printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
312 printtask "Your set-up is missing a dependency."
313 echo "We require JDK, tar, patch and cpio."
314 echo ""
315 echo ""
316 echo "Each distro is different"
317 echo "but ensure you have them all installed and try again."
318 echo ""
319 cleanup
320 brief_help
323 printtask () {
324 echo "" >> "$LOG"
325 echo -e $@ | tee -a "$LOG"
328 copyfailed () {
329 echo ""
330 printtask "File for which patching failed:"
331 printtask "---------------------------------------------"
332 echo ""
333 printtask "${FAIL##*/}"
334 echo ""
335 echo "---------------------------------------------"
336 echo ""
337 echo "We can try to add a premade copy of those files."
338 echo "This sometimes works."
339 echo ""
340 printtask "You only need to report this if running the program gives you problems!!!"
341 echo ""
342 echo ""
343 askconsent && return 0 || return 1
346 askconsent () {
347 sleep 1
348 echo -n "Do you want to proceed ? (YES/no) "
349 read PROCEED
350 echo "askconsent final = ${PROCEED}: for $FAIL" >> "$LOG"
351 case $PROCEED in
352 [Yy] | [Yy][Ee][Ss])
353 return 0; # successful:
354 echo "*** You (or ApG) have elected to use a prepatched copy of $FAIL ***" >> "$LOG"
357 return 1; # error
358 echo "*** You have declined to insert $FAIL by not answering 'yes' at the prompt. ***" >> "$LOG"
360 esac
363 replace_updater () {
365 # Function for devices that use SDcard installs. Also to be tested for CM7
366 # since CM7 seems to use an updater-script that wipes EMMC. Testing
368 \rm $UPDATER_LOC/$UPDS
369 \cp patches/.common/$UPDATER_LOC/$UPDS $UPDATER_LOC
370 echo "run_program(\"/sbin/busybox\", \"umount\", \"/system\");" >> UPDATE.txt
371 cat UPDATE.txt >> $UPDATER_LOC/$UPDS
372 echo ""
373 echo "New updater-script being used:" >> "$LOG"
374 echo "-----------------------------" >> "$LOG"
375 cat $UPDATER_LOC/$UPDS >> "$LOG"
378 abspath () {
379 case `uname -s` in
380 CYGWIN*)
381 echo $(cygpath -ua "$1") | sed 's:/$::g'
383 Darwin)
384 #[[ $(echo $1 | awk '/^\//') == $1 ]] && echo "$1" || echo "$PWD/$1"
385 [[ ${1:0:1} == "/" ]] && echo "$1" || echo "$PWD/$1"
387 Linux)
388 echo $(readlink -f "$1")
391 if [[ ${1:0:1} == "/" ]]; then
392 echo "$1"
393 elif [[ ${1:0:2} == "./" ]]; then
394 echo "$PWD/${1:2}"
395 else
396 echo "$PWD/$1"
399 esac
402 mods_display () {
403 echo ""
404 echo "The Auto-Patcher $VERSION"
405 echo "*******************************"
406 echo ""
407 echo "The available mods are"
408 echo "****************************************************"
409 echo "* pdroid openpdroid voice v6supercharger *"
410 echo "* voice insecure secure external_internal *"
411 echo "* tabletUI 3gdongle pd2.0 *"
412 echo "****************************************************"
413 echo ""
414 echo "To find out more about any mod, simply enter:"
415 echo " ./auto_patcher -h <MODNAME>"
416 echo ""
417 cleanup
420 mods_info () {
421 # Defaults
422 DEPENDENCY="None"
423 DEV="Unknown"
424 CEILING=$CURRENT_ANDROID
425 ROM_CONFLICTS="None"
426 case $PID in
427 mods)
428 mods_display
429 exit
431 auto*)
432 echo ""
433 echo "**************************************"
434 echo "The Auto-Patcher ate a kitten once..."
435 echo "**************************************"
436 echo ""
437 printusage_help
439 advanced)
440 cd patches/
441 (advanced_help && find -H * -name 201*) | more -d
442 echo ""
443 brief_help
445 voice)
446 FLOOR=4.0.0
447 DESC="Use on wifi-only tablets to have the software register as 'voice-enabled'. Allows VOIP like Google Voice to function."
448 DESC="bongostl's setup method here: http://forum.xda-developers.com/showthread.php?t=1823701"
449 DEV="bongostl"
452 v6supercharger)
453 FLOOR=4.0.0
454 CEILING=4.2.1
455 DESC="A memory-management tool. It requires smali edits for Android roms 4.0+. The Auto-Patcher applies all 50+ edits recommended by the dev."
456 DEV="Zeppelinrox"
457 DEPENDENCY="The V6 Supercharger Script. Get it at http://forum.xda-developers.com/showthread.php?t=991276/"
459 pdroid)
460 FLOOR=2.3.7
461 CEILING=4.1.2
462 DESC="Security Software that safely manages permissions w/o causing FC's. No longer under development. Try the open-source expansion of this program, PDroid Manager. (It's the 'openpdroid' mod within the Auto-Patcher)."
463 DEV="Svyat"
464 DEPENDENCY="The Pdroid app. You can get it at http://forum.xda-developers.com/showthread.php?t=1923576"
466 pd2.0)
467 FLOOR=4.1.2
468 CEILING=4.1.2
469 DESC="An expansion of the PDroid application/framework."
470 DEV="CollegeDev"
471 DEPENDENCY="The free PDroid2.0 app available at http://forum.xda-developers.com/showthread.php?t=1923576"
473 openpdroid)
474 FLOOR=4.1.2
475 CEILING=4.2.1
476 DESC="A fork of the open-source PDroid and Pdroid2.0 framework."
477 DEV="FFU5y/wbedard/mateorod/Community"
478 DEPENDENCY="The PDroidManager app freely available from Google Play or http://forum.xda-developers.com/showthread.php?t=1994860"
480 opendroid)
481 echo ""
482 echo "No mod named 'opendroid'. You probably mean 'openpdroid'. Try again."
483 PID="mods"
484 mods_info
486 external_internal)
487 FLOOR=2.3.3
488 DESC="Swap internal and external storage."
490 insecure)
491 FLOOR=2.3.3
492 DESC="Make any boot.img insecure and allow remount as well as adb push/pull of system files. True root."
494 secure)
495 FLOOR=2.3.3
496 DESC="Make any boot.img secure."
498 3gdongle)
499 FLOOR=4.1.0
500 CEILING=4.2.1
501 DESC="Allows 3gdongles to work on wifi tablets w/o 3rd party applications."
502 DEV="trevd"
503 DEPENDENCY="None, but read the troubleshooting guide at http://forum.xda-developers.com/showthread.php?t=1798631"
505 tabletUI)
506 FLOOR=4.1.0
507 CEILING=4.2.1
508 ROM_CONFLICTS=(pa .aosp-slim)
509 DESC="Changes the user interface of Phones and Phablets to the popular Tablet UI."
510 DEV="barmullio"
513 FLOOR=?????
514 CEILING=?????
515 DESC="??? Unknown modtype ??? Check your spelling or the mod list!"
516 #DESC1="./auto_patcher -h mods"
517 esac
518 # strip decimals from android version (i.e. 2.3.7 becomes 237) so we can do math
519 NVID=${VID//[.]/}
520 COMPATF=${FLOOR//[.]/}
521 COMPATC=${CEILING//[.]/}
524 apk_check () {
525 if [ -f patches/$P/$A/$R/$B/$FILE ]; then
526 echo "... Patching $APK failed, but we will copy over a prepatched file ..."
527 \cp -av patches/$P/$A/$R/$B/$FILE $FILE >> "$LOG"
528 else
529 print_error "failed patching $APK!!!"
533 compat_check () {
534 for P in ${PID[@]}; do
535 mods_info
536 if [[ "$NVID" -lt "$COMPATF" ]]; then
537 mismatch_error "Your Android $VID rom is too old! The minimum for $P is \n !!! Android $FLOOR!"
539 if [[ "$NVID" -gt "$COMPATC" ]]; then
540 mismatch_error "Sorry, $P has not been made available for Android $VID yet!"
542 done
545 rom_compat_check () {
546 for P in ${PID[@]}; do
547 N=${#ROM_CONFLICTS[@]}
548 for ((i=0; i < $N; i++)); do
549 if [[ $RID == ${ROM_CONFLICTS[$i]} ]]; then
550 rom_compat_error
552 done
553 done
556 rom_compat_error () {
557 print_error "$RID is incompatible with $PID!!!"
560 extpath () {
561 case `uname -s` in
562 CYGWIN*)
563 echo $(cygpath -da "$1")
566 echo $(abspath "$1")
568 esac
571 #contains () { for e in "${@:2}"; do [[ "$e" = "$1" ]] && return 0; done; return 1; }
573 parse () { echo "${1%%_*}"; }
575 getbuildprop () {
576 if [ -f system/build.prop ]; then
577 result=(`grep $1 system/build.prop | tr -d '\r' | tr '=' ' '`)
578 echo "${result[1]}"
579 else
580 echo ""
585 aosp_mod_check() {
586 jar -xvf "$ROMX" system/framework/framework2.jar >> $LOG
587 if [ -s system/framework/framework2.jar ]; then
588 ever_check
589 else
590 AOSP=aosp
592 echo $AOSP
595 ever_check() {
596 MID=$(getbuildprop ro.build.romversion)
597 if [[ $MID == "Evervolv"* ]]; then
598 AOSP=".aosp-ever"
599 else
600 MID=$(getbuildprop ro.build.display.id)
601 if [[ "$MID" == "ev"* ]]; then
602 AOSP=".aosp-ever"
603 else
604 slim_check
607 echo $AOSP
610 slim_check() {
611 SLMID=$(getbuildprop ro.modversion)
612 SLMMID=$(getbuildprop updateme.name)
613 if [[ "$SLMMID" == "Slim"* ]] || [[ $SLMMID == "slim"* ]]; then
614 AOSP=".aosp-slim"
615 elif [[ "$SLMID" == "Slim"* ]] || [[ $SLMID == "slim"* ]]; then
616 AOSP=".aosp-slim"
617 else
618 if [[ "$ANDR" == "4.1" ]]; then
619 AOSP=.aosp-mod
620 else
621 AOSP="aosp"
626 pa_check() {
627 CMID=$(getbuildprop ro.modversion)
628 PAID=$(getbuildprop ro.pa.version)
629 MID=$(getbuildprop ro.build.display.id)
630 [[ "$MID" == "" ]] && MID=$(parse $(getbuildprop ro.build.display.id))
631 DID=$(getbuildprop ro.product.device)
632 [[ "$DID" == "" ]] && DID=$(getbuildprop ro.build.product)
633 if [[ -n $PAID ]]; then
634 RID=pa
635 else
636 case $CMID in
637 [Pp][Aa]*)
638 RID=pa;
641 RID=cm;
643 esac
647 romtype_set () {
648 case $RID in
650 ROMTYPE="cm"
652 aokp*)
653 ROMTYPE="aokp"
656 ROMTYPE="pa"
659 ROMTYPE="aosp"
661 esac
664 proberom () {
665 VID=$(getbuildprop ro.build.version.release)
666 if [ ${#VID} -lt 4 ]; then # in case of non-standard versioning in build.prop (defy!)
667 ANDR=$VID
668 else
669 ANDR="${VID%.*}" # Cut to 1 significant digit to cover specific range or release.
671 if [ "$ANDR" == "2.3" ]; then
672 FLAVR=gingerbread
673 API=10
674 RID=cm;
675 elif [ "$ANDR" == "4.0" ]; then
676 FLAVR=ics
677 API=15
678 MID=$(getbuildprop ro.rommanager.developerid)
679 [[ "$MID" == "" ]] && MID=$(parse $(getbuildprop ro.modversion))
680 DID=$(getbuildprop ro.product.device)
681 [[ "$DID" == "" ]] && DID=$(getbuildprop ro.build.product)
683 case "$MID" in
684 pa|cm9|cm|cyanogenmod*)
685 RID=cm;
687 aokp|teamkang)
688 RID=aokp;
690 aosp*)
691 RID=aosp;
694 echo "!!! $MID is not supported yet, trying aosp !!!"
695 RID=aosp
697 esac
698 elif [ "$ANDR" == "4.1" ]; then
699 FLAVR=jellybean
700 API=16
701 CMID=$(getbuildprop ro.cm.version)
702 if [[ -n $CMID ]]; then
703 pa_check
704 else
705 MID=$(getbuildprop ro.build.display.id)
706 [[ "$MID" == "" ]] && MID=$(parse $(getbuildprop ro.build.display.id))
707 DID=$(getbuildprop ro.product.device)
708 [[ "$DID" == "" ]] && DID=$(getbuildprop ro.build.product)
709 case "$MID" in
710 cm10|cm*|cyanogenmod*)
711 RID=cm;
713 aokp*|teamkang)
714 RID=aokp;
716 pa*)
717 RID=pa;
719 ev*)
720 RID=".aosp-ever";
723 RID=`aosp_mod_check`;
725 esac
727 elif [ "$ANDR" == "4.2" ]; then
728 FLAVR=jellybean
729 API=17
730 CMID=$(getbuildprop ro.cm.version)
731 MID=$(getbuildprop ro.build.display.id)
732 [[ "$MID" == "" ]] && MID=$(parse $(getbuildprop ro.build.display.id))
733 DID=$(getbuildprop ro.product.device)
734 [[ "$DID" == "" ]] && DID=$(getbuildprop ro.build.product)
736 case "$MID" in
737 cm10|cm*|cyanogenmod*)
738 RID=cm;
740 aokp*|teamkang)
741 RID=aokp;
743 pa*)
744 RID=pa;
746 ev*|Ever*)
747 RID=".aosp-ever";
750 RID=`ever_check`;
752 esac
753 else
754 print_error "unknown ROM version $VID"
756 romtype_set
757 echo "... result of ROM check: version= $VID rom= $ROMTYPE ..."
761 # Advanced build date query
762 # This should set the patch date automatically.
764 build_query() {
765 UTC=$(getbuildprop ro.build.date.utc)
767 # Difference in how OS X and Linux/Cygwin handles 'date' utility
768 case `uname -s` in
769 [Dd]arwin)
770 BDID=`date -r $UTC +%Y%m%d`
771 echo "#### Darwin Build Query used ####" >> "$LOG"
774 BDID=`date -d "1970-01-01 UTC $UTC seconds" +"%Y%m%d"`
775 echo "#### Standard Build Query ####" >> "$LOG"
777 esac
779 # If patch date is specified on CLI, use that patch date
780 if [ $# -gt 3 ]; then
781 BID=${4//,/ };
782 BDID=$BID
783 else
784 case $ANDR in
785 2.3)
786 # CM7 roms do not have UTC time stamp- so no build query.
787 BID=latest;
788 echo "*** CM7 roms default to latest patches. If using an older rom ***"
789 echo "*** specify patch date on command line. Use -h to see available patches ***"
792 for P in ${PID[@]}; do
793 [ -d patches/$P/$ANDR ] && A=$ANDR || A=all
794 [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
795 [ -d patches/$P/$A/$R/$BID ] && B=$BID || B=latest
796 PAR=patches/$P/$A/$R
797 PARB=patches/$P/$A/$R/$B
798 DATES=`ls patches/$P/$A/$R/ | grep '201*'`
799 # Intialize variable - High so as to determine lowest in first loop.
800 BID=20201231
801 # find earliest available patch date
802 for i in $DATES; do
803 if [[ $i -lt $BID ]]; then
804 BID=$i
806 done
807 # Check if earliest patches are from before build date. Else use earliest
809 # If older than earliest patch set, then find best match
810 if [[ $(($BID-$BDID)) -lt 0 ]]; then
811 for g in $DATES; do
812 # use any patch that matches $UTC
813 if [[ $(($BDID-$g)) -eq 0 ]]; then
814 BID=$g
815 break
816 # Set highest $BID w/o going over $UTC
817 elif [[ $(($BDID-$g)) -gt 0 ]] && [[ $g -gt $BID ]]; then
818 BID=$g
820 done
822 echo "... $PID patch date has been set to: $BID ..."
823 B=$BID
824 break
825 done
827 esac
831 proberil () {
832 RIL=$(getbuildprop ro.telephony.ril_class)
833 [[ -n $RIL ]] && printtask "... result of ROM check: RIL= $RIL"
836 rominfo () {
837 echo "# VER= $VERSION" >> "$LOG"
838 echo "# OS= $OSTYPE ${BIT}bit" >> "$LOG"
839 echo "# VID= $VID" >> "$LOG"
840 echo "# PID= ${PID[@]}" >> "$LOG"
841 echo "# MID= $MID" >> "$LOG"
842 echo "# RID= $RID" >> "$LOG"
843 echo "# ANDR= $ANDR" >> "$LOG"
844 echo "# BDID= $BDID" >> "$LOG"
845 echo "# DID= $DID" >> "$LOG"
846 echo "# BID= $BID" >> "$LOG"
847 echo "Patches are $PID/$ANDR/$RID/$BID" >> "$LOG"
851 if [[ $1 == -h ]]; then
852 if [ $# -gt 1 ]; then
853 PID=$2
854 mods_info
855 echo ""
856 echo "The Auto-Patcher $VERSION by Mateorod and Pastime1971"
857 echo "*************************************************"
858 echo ""
859 echo "Mod Name: $PID"
860 echo ""
861 echo " $DESC"
862 if [[ -z $DESC1 ]]; then # test to see is there is a second DESC line
863 echo "$DESC1"
865 if [[ $((DESC2)) -gt 0 ]]; then
866 echo "$DESC2"
868 echo ""
869 echo "Developer: $DEV"
870 echo "Dependancies: $DEPENDENCY"
871 echo ""
872 echo "Works on ROMs from Android $FLOOR -> $CEILING"
873 echo
874 brief_help
875 exit
879 [[ $# -lt 1 || -z $1 ]] && PID="mods" && mods_info
880 [[ $1 == "-h" ]] && usage_help | less && display_help
882 echo ""
883 echo "The Auto-Patcher $VERSION"
884 echo ""
885 # by pastime1971 and mateorod
887 ROOT=$(abspath `dirname "$0"`)
888 TOOL="$ROOT/patch_tools.tgz"
889 PTCH="$ROOT/patch_patches.tgz"
890 cleanup
891 # set temporaray directory
892 DATE=`date +%Y%m%d%H%M%S`
893 TEMP=$ROOT/tmp$DATE
894 TMP=$ROOT/tmp$DATE
895 TMPDIR=$ROOT/tmp$DATE
896 mkdir "$TEMP"
897 ROM=$(abspath "$1")
898 ROMX=$(extpath "$1")
900 if [[ $OSTYPE == "cygwin" ]]; then
901 EXE=".exe"
902 PGFORM="0x"
905 LOG=$(abspath ./log$DATE.txt)
906 #echo $ROOT $TOOL $ROM $TEMP "$LOG"
907 echo "ROOT= $ROOT" >> "$LOG"
908 echo "TOOL= $TOOL" >> "$LOG"
909 echo "ROM= $ROM" >> "$LOG"
910 echo "ROMX= $ROMX" >> "$LOG"
912 \rm -f $RESTORE.zip $UPDATE.zip
914 CHK=$(getconf LONG_BIT)
915 if [[ $CHK = "64" ]]; then
916 BIT=64
917 else [[ $BIT = "32" ]];
918 BIT=32
922 printtask "### verify tools ###"
923 which patch >> "$LOG" || misstools_error "Missing \"patch\" utility."
924 which tar >> "$LOG" || misstools_error "Missing \"tar\" utility"
925 which jar >> "$LOG" || misstools_error "Missing \"Java Development Kit\" (JDK)"
926 which java >> "$LOG" || misstools_error "Missing \"Java Development Kit\" (JDK)"
927 which cpio >> "$LOG" || misstools_error "Missing \"cpio\" utility"
928 [ -f "$TOOL" ] || misspatch_error "patch_tool.tgz is missing"
929 [ -f "$PTCH" ] || misspatch_error "patch_patches.tgz is missing"
930 printtask "### verify ROM ###"
931 [ -f "$ROM" ] || print_error "Cannot find $1!! Make sure you have the location and name right"
932 echo "### running $0 $@" >> "$LOG"
933 cd "$TEMP"
935 PATH="$TEMP:$PATH"
936 export PATH
937 echo "PATH= $PATH" >> "$LOG"
939 printtask "### unzip patch tools ###"
940 tar xzf "$TOOL" > /dev/null || print_error "failed extracting tools !!!"
942 # Get OS specific tools
943 case `uname -s` in
944 CYGWIN*)
945 \mv cygwin/aapt.exe .
946 \mv cygwin/*.exe boot/
947 \rm -rf cygwin # why? If you want to we can keep/add it to mac/linux, though.
949 [Dd]arwin)
950 \mv mac/aapt .
952 [Ll]inux)
953 \mv linux/aapt .
956 \mv linux/aapt .
958 esac
959 # Ensure aapt permissions - possibly to one day be expanded to all tools in a function.
960 chmod a+x aapt$EXE
962 printtask "### unzip patch patches ###"
963 tar xzf "$PTCH" > /dev/null || print_error "failed extracting patches !!!"
965 # we need build.prop for both RIL probing and ROM probing
966 jar -xvf "$ROMX" system/build.prop 2>> "$LOG" || print_error "failed extracting build.prop from $ROMX !!!"
968 # rom + ril type
969 proberil
970 printtask "### probe rom type ###"
971 proberom
973 # rom type
974 if [ $# -gt 1 ]; then
975 PIT=$2
976 if [[ "$PIT" = "-h" ]]; then
977 display_help
980 if [ $# -gt 2 ]; then
981 RID=$3
982 case "$RID" in
983 cm10|cm*|cyanogenmod*)
985 aokp*|teamkang)
986 RID=aokp;
988 pa*)
989 RID=pa;
991 .aosp-ever)
992 RID=".aosp-ever";
994 .aosp-slim)
995 RID=".aosp-slim"
997 aosp*)
998 case "ANDR" in
999 4.1)
1000 RID=`aosp_mod_check`;
1003 RID=`ever_check`
1005 esac
1007 201*)
1008 date_error "You must specify ROMTYPE if designating a patch date!!"
1011 rom_error "$RID is not a valid ROMTYPE!!"
1013 esac
1014 romtype_set
1015 echo "... Using $ROMTYPE patches ..."
1016 else
1017 echo "... Using results of ROM check ..."
1020 # patch type
1021 if [ $# -gt 1 ]; then
1022 PIL=${2//,/ } # If more than one mod, make string separated by spaces
1023 else
1024 PIL="openpdroid"
1025 echo "No mods selected. Defaulting to openpdroid, since that's what you probably want..."
1028 # Ensure only 1 Pdroid mod at a time; PDroid first, so provisional use
1029 # does not overwrite other mods...
1031 PID=(${PIL// / }) # turn string of requested mods into array
1032 COPYPID=("${PID[@]}") # working copy to iterate over
1033 MODS_NUMBER=${#COPYPID[@]} # number of elements (mods)
1035 if [[ $MODS_NUMBER > 1 ]]; then # cause if only one mod, no point
1036 INDEX=0 # 'pd2.0' didn't work with unset; regex barred multiple 'pdroid'
1037 for p in ${COPYPID[@]}; do
1038 if [ "$p" = "pdroid" ] || [ "$p" = "pd2.0" ] ||[ "$p" = "openpdroid" ]; then
1039 SPID[0]=$p #set pdroid first
1040 unset COPYPID["$INDEX"]
1041 break
1042 else
1043 INDEX=$((INDEX + 1)) # if !pdroid, set as 1, 2...
1045 done
1046 for p in ${COPYPID[@]}; do
1047 if [ "$p" = "pdroid" ] || [ $p = "pd2.0" ] ||[ $p = "openpdroid" ]; then
1048 conflict_error "Those mods conflict! Only one pdroid mod at a time!"
1049 else
1050 i=${#SPID[@]}
1051 SPID[i]=$p
1052 i+=1
1054 done
1055 else
1056 SPID=${COPYPID[@]}
1058 for P in ${PIL[@]}; do
1059 [[ ${PID[@]} =~ $P ]] || PID=(${PID[@]} $P)
1060 done
1062 # Check if selected mods are available for rom's Android version.
1063 compat_check
1065 # Make sure mod and rom don't conflict (e.g. pa and tabletUI)
1066 if [[ $ROM_CONFLICTS != "None" ]]; then
1067 rom_compat_check
1070 # Build date
1071 if [ $# -gt 3 ]; then # if a date is specified on command line...
1073 # Set date to match argument and ensure that patches with that date exist
1074 BID=$4
1075 [ -d patches/$P/$ANDR/$RID/$BID ] || print_error "No $P patches for $RID on $BID!! Try the help menu!!"
1077 # Run build_query- this overrides the above but gets useful info
1078 build_query
1079 # Reset patch date to argument
1080 BID=$4
1081 echo "... $PID patch date set manually to: $BID ..."
1082 else
1083 build_query # if no date specified, use one from build_query
1086 # echo ROM info
1087 rominfo
1089 if [[ ( "$PID" == "-h" || "$RID" == "-h" || "$BID" == "-h" ) ]]; then
1090 #Although this placement may be perfect for romfilter. although it throws the scheme...
1091 display_help
1093 #Begin cycle
1095 for P in ${PID[@]}; do
1096 [ -d patches/$P ] || print_error "$P is not available"
1097 [ -d patches/$P/$ANDR ] && A=$ANDR || A=all
1098 [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
1099 [ -d patches/$P/$A/$R/$BID ] && B=$BID || B=latest
1101 unset FILES
1102 [ -f patches/$P/files.txt ] && FILES=$(cat patches/$P/files.txt)
1103 [ -f patches/$P/$A/files.txt ] && FILES=$(cat patches/$P/$A/files.txt)
1104 [ -f patches/$P/$A/$R/files.txt ] && FILES=$(cat patches/$P/$A/$R/files.txt)
1105 [ -f patches/$P/$A/$R/$B/files.txt ] && FILES=$(cat patches/$P/$A/$R/$B/files.txt)
1106 for FILE in ${FILES[@]}; do
1107 case "$FILE" in
1108 +system/*)
1109 [[ ${CID[@]} =~ ${FILE:1} ]] && print_error "${FILE:1} is already being copied" || CID=(${CID[@]} ${FILE:1})
1111 -system/*)
1112 [[ ${EID[@]} =~ ${FILE:1} ]] || EID=(${EID[@]} ${FILE:1})
1114 system/*.jar)
1115 [[ ${JID[@]} =~ $FILE ]] || JID=(${JID[@]} $FILE)
1116 echo "set_perm(0, 0, 0644, \"/$FILE\");" >> UPDATE.txt
1118 ~system/*apk) # apps to decompile with smali instead of apktool
1119 [[ ${JID[@]} =~ ${FILE:1} ]] || JID=(${JID[@]} ${FILE:1})
1120 echo "set_perm(0, 0, 0644, \"/$FILE\");" >> UPDATE.txt
1122 system/*.apk)
1123 [[ ${AID[@]} =~ $FILE ]] || AID=(${AID[@]} $FILE)
1124 echo "set_perm(0, 0, 0644, \"/$FILE\");" >> UPDATE.txt
1126 *.img)
1127 [[ ${IID[@]} =~ $FILE ]] || IID=(${IID[@]} $FILE)
1129 system/*)
1130 [[ ${TID[@]} =~ $FILE ]] || TID=(${TID[@]} $FILE)
1132 esac
1133 done
1134 done
1135 echo "### DELETE FILES= ${EID[@]}" >> "$LOG"
1136 echo "### COPY FILES= ${CID[@]}" >> "$LOG"
1137 echo "### JAR FILES= ${JID[@]}" >> "$LOG"
1138 echo "### APK FILES= ${AID[@]}" >> "$LOG"
1139 echo "### IMG FILES= ${IID[@]}" >> "$LOG"
1140 echo "### TXT FILES= ${TID[@]}" >> "$LOG"
1142 # extract system files
1143 printtask "### unzip rom ###"
1144 jar -xvf "$ROMX" ${JID[@]} ${AID[@]} ${IID[@]} ${TID[@]} 2>> "$LOG" || print_error "failed extracting ${JID[@]} ${AID[@]} ${IID[@]} from $ROMX !!!"
1147 # Update/Restore.zip scripts and prep
1148 printtask "### create updater script ###"
1149 UPDATER_LOC=META-INF/com/google/android
1150 UPDS=updater-script
1151 UPDATE="update-$ROMTYPE-$DID-$BDID-${PIL// /-}"
1152 RESTORE="restore-$ROMTYPE-$DID-$BDID"
1154 jar -xvf "$ROMX" $UPDATER_LOC/$UPDS $UPDATER_LOC/update-binary
1155 jar -xvf "$ROMX" $UPDATER_LOC/update-binary-installer 1> /dev/null 2> /dev/null
1157 # Determine Installer- Aroma or ClockworkMod
1158 if [ -f $UPDATER_LOC/update-binary-installer ]; then
1159 printtask "... using Aroma installer ..."
1160 \mv $UPDATER_LOC/update-binary-installer $UPDATER_LOC/update-binary
1161 else
1162 printtask "... using Clockworkmod installer ..."
1165 # Create updater scripts
1166 cat patches/.common/updater-message.txt >> $UPDS
1167 cat $UPDATER_LOC/$UPDS | grep ^mount | grep "/system" | head -n 1 >> $UPDS
1168 [ -s $UPDS ] || echo "run_program(\"/sbin/busybox\", \"mount\", \"/system\");" >> $UPDS
1169 # restore the original
1170 for FILE in ${EID[@]}; do
1171 jar -xvf "$ROMX" $FILE 2> /dev/null || printtask "!!! warning $FILE cannot be restored !!!"
1172 done
1173 # replace with the original or delete it
1174 for FILE in ${CID[@]}; do
1175 jar -xvf "$ROMX" $FILE 2> /dev/null || echo "delete(\"/$FILE\");" >> $UPDS
1176 done
1177 echo "package_extract_dir(\"system\", \"/system\");" >> $UPDS
1178 # permission management for files that need specific perms
1179 cat $UPDATER_LOC/$UPDS | grep set_perm | grep "/system" >> $UPDS
1180 for P in ${PID[@]}; do
1181 if [ -s patches/$P/$A/set_perm.txt ]; then
1182 echo "...setting permissions..."
1183 cat patches/$P/$A/set_perm.txt >> $UPDS
1185 done
1186 cat $UPDATER_LOC/$UPDS | grep ^unmount | grep "/system" | head -n 1 >> $UPDS
1187 [[ ${IID[@]} =~ boot.img ]] && cat $UPDATER_LOC/$UPDS | grep "boot.img" >> $UPDS
1188 echo "ui_print(\" .\");" >> $UPDS
1189 echo "ui_print(\" .\");" >> $UPDS
1190 echo "ui_print(\" done!\");" >> $UPDS
1191 cat $UPDS > $UPDATER_LOC/$UPDS
1192 echo "# generated $UPDS" >> "$LOG"
1193 cat $UPDS >> "$LOG"
1195 printtask "### create restore.zip ###"
1196 jar -cvf tmp.zip ${JID[@]} ${AID[@]} ${IID[@]} META-INF > /dev/null 2>> "$LOG"
1197 java -jar signapk.jar updatecert.pem updatekey.pk8 tmp.zip signed.zip 2>> "$LOG"
1198 \mv signed.zip $RESTORE.zip
1199 \rm -f tmp.zip
1202 # Begin patching process
1204 # decompile system files
1205 printtask "### decompile system files ###"
1206 for FILE in ${JID[@]}; do
1207 JAR=${FILE##*/}
1208 printtask "... decompile $JAR ..."
1209 java -jar baksmali.jar -b -a $API -o tmp/${FILE}.out/smali ${FILE} >> "$LOG" 2>> "$LOG"
1210 if [ $FILE == "system/framework/framework.jar" ]; then
1211 printtask "... extract preloaded-classes ..."
1212 jar -xvf ${FILE} preloaded-classes >> "$LOG" 2>> "$LOG"
1213 \mv preloaded-classes tmp/${FILE}.out/
1215 done
1217 # install framework-files for APK...
1218 if [ ${#AID[@]} -gt 0 ]; then
1219 jar -xvf "$ROMX" system/framework/framework-res.apk >> "$LOG"
1220 printtask "... installing framework files ..."
1221 java -jar apktool.jar if system/framework/framework-res.apk
1222 # Decompile APKs
1223 for FILE in ${AID[@]}; do
1224 APK=${FILE##*/}
1225 if [ -f $FILE ]; then
1226 printtask "... decompile $APK ..."
1227 java -jar apktool.jar d -f $FILE tmp/$FILE.out
1229 done
1232 # Unpack image files {boot.img only so far...}
1233 for FILE in ${IID[@]}; do
1234 printtask "### unpacking $FILE ###"
1235 case "$FILE" in
1236 boot.img)
1237 if [ -f boot.img ]; then
1238 cd boot
1239 ./unpackbootimg$EXE -i ../boot.img >> "$LOG" 2>> "$LOG"
1240 mkdir ramdisk
1241 cd ramdisk
1242 gzip -dc ../boot.img-ramdisk.gz | cpio -i >> "$LOG" 2>> "$LOG"
1243 cd ../../
1244 else
1245 print_error "A mod needs to patch boot.img! This rom does not contains boot.img."
1249 print_error "cannot process $FILE"
1251 esac
1252 done
1254 # apply system modification
1256 for P in ${PID[@]}; do
1257 [ -d patches/$P/$ANDR ] && A=$ANDR || A=all
1258 [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
1259 [ -d patches/$P/$A/$R/$BID ] && B=$BID || B=latest
1260 PAR=patches/$P/$A/$R
1261 PARB=patches/$P/$A/$R/$B
1263 unset FILES COPY DELS JARS APKS IMGS TXTS
1264 [ -f patches/$P/files.txt ] && FILES=$(cat patches/$P/files.txt)
1265 [ -f patches/$P/$A/files.txt ] && FILES=$(cat patches/$P/$A/files.txt)
1266 [ -f patches/$P/$A/$R/files.txt ] && FILES=$(cat patches/$P/$A/$R/files.txt)
1267 [ -f patches/$P/$A/$R/$B/files.txt ] && FILES=$(cat patches/$P/$A/$R/$B/files.txt)
1268 for FILE in ${FILES[@]}; do
1269 case $FILE in
1270 +system/*)
1271 COPY=(${COPY[@]} ${FILE:1})
1273 -system/*)
1274 DELS=(${DELS[@]} ${FILE:1})
1276 system/*.jar)
1277 JARS=(${JARS[@]} $FILE)
1279 ~system/*)
1280 JARS=(${JARS[@]} ${FILE:1})
1282 system/*.apk)
1283 APKS=(${APKS[@]} $FILE)
1285 *.img)
1286 IMGS=(${IMGS[@]} $FILE)
1288 system/*)
1289 TXTS=(${TXTS[@]} $FILE)
1291 esac
1292 done
1294 [[ -n ${COPY[@]} ]] && echo "### copy patch $P/$A/$ROMTYPE/$B ###"
1295 echo "### copy patch $P/$A/$R/$B ###" >> "$LOG"
1296 for FILE in ${COPY[@]}; do
1297 printtask "... copying $FILE ..."
1298 DEST=${FILE%/*}
1299 mkdir -p ./$DEST
1300 if [ -e patches/$P/$A/$R/$B/$FILE ]; then
1301 [ -e $FILE ] && printtask "... overwriting $FILE from $P ..."
1302 \cp -av patches/$P/$A/$R/$B/$FILE ./$DEST >> "$LOG"
1303 else
1304 print_error "$FILE is missing in the patch. Please upload your logfile to the support thread!"
1306 done
1308 # unconditional smali copying- For particularly troublesome files
1309 [ -f patches/$P/smali.txt ] && SMALI=$(cat patches/$P/smali.txt)
1310 [ -f patches/$P/$A/smali.txt ] && SMALI=$(cat patches/$P/$A/smali.txt)
1311 [ -f patches/$P/$A/$R/smali.txt ] && SMALI=$(cat patches/$P/$A/$R/smali.txt)
1312 [ -f patches/$P/$A/$R/$B/smali.txt ] && SMALI=$(cat patches/$P/$A/$R/$B/smali.txt)
1313 MOVE=${SMALI##*/}
1314 for SMALI in ${SMALI[@]}; do
1315 if [ -f patches/$P/$A/$R/.provisionals/$SMALI ]; then
1316 printtask "... overwriting $MOVE ..."
1317 \cp -a patches/$P/$ANDR/$R/.provisionals/$SMALI tmp/$SMALI >> "$LOG"
1318 elif [ -f patches/$P/$A/.provisionals/$SMALI ]; then
1319 printtask "... overwriting $MOVE ..."
1320 \cp -a patches/$P/$ANDR/.provisionals/$SMALI tmp/$SMALI >> "$LOG"
1322 done
1324 [[ -n ${DELS[@]} ]] && echo "### delete patch $P/$A/$ROMTYPE/$B ###"
1325 echo "### delete patch $P/$A/$R/$B ###" >> "$LOG"
1326 for FILE in ${DELS[@]}; do
1327 printtask "... deleting $FILE ..."
1328 \rm -rf ./$FILE
1329 done
1330 [[ -n ${JARS[@]} ]] && echo "### jar patch $P/$A/$ROMTYPE/$B ###"
1331 echo "### jar patch $P/$A/$R/$B ###" >> "$LOG"
1332 for FILE in ${JARS[@]}; do
1333 JAR=${FILE##*/}
1334 if [[ $FILE == *".jar" ]]; then # allows for *.apks to be decompiled by baksmali
1335 NAME=${JAR%.jar}
1336 else
1337 NAME=$JAR # TODO: Maybe rename this, since it now encompases apks at times
1339 printtask "... apply $NAME.patch ..."
1340 patch -Nls -p1 -d tmp/${FILE}.out < $PARB/${NAME}.patch 2>&1 > ${JAR}.log
1341 cat ${JAR}.log | tee -a "$LOG"
1342 case "$NAME" in
1343 framework)
1344 # patch preloaded-classes only if a patch exists
1345 if [ -f $PARB/preloaded.patch ]; then
1346 printtask "... apply preloaded.patch ..."
1347 patch -Nls -p1 -d tmp/${FILE}.out < $PARB/preloaded.patch 2>&1 > preloaded.log
1348 cat preloaded.log | tee -a "$LOG"
1349 [[ `grep FAILED preloaded.log` != "" ]] && print_error "failed preloaded.patch"
1353 echo -n "" # do nothing else
1355 esac
1356 # substitute failed smali with provisional (a prepatched smali file)
1357 unset FAILS
1358 FAILS=(`grep FAILED ${JAR}.log | sed -e 's/.*smali\///' | sed -e 's/\.rej//'`)
1359 if [[ -n ${FAILS[@]} ]]; then
1360 printtask "!!! PATCHING FAILED FOR ${FAILS[@]} !!!"
1361 echo "### replace ${FAILS[@]} ###" > ${JAR}.log
1362 for FAIL in ${FAILS[@]}; do
1363 PROVISIONAL="patches/$P/$ANDR/.provisionals/${FILE}.out/smali/${FAIL}"
1364 echo "!!! patch error for $FAIL !!!" >> "$LOG"
1365 #cat tmp/$FILE.out/smali/${FAIL}.rej >> "$LOG" # FOR DEBUGGING
1366 [[ ${SID[@]} =~ $FAIL ]] && print_error "$FAIL was already replaced before"
1367 [ -f "$PROVISIONAL" ] || print_error "failed $FAIL in $JAR cannot be remedied"
1368 copyfailed
1369 printtask "... replace $FAIL ..."
1370 \rm -rf tmp/${FILE}.out/smali/${FAIL} tmp/${FILE}.out/smali/${FAIL}.orig tmp/${FILE}.out/smali/${FAIL}.rej
1371 \cp -av "$PROVISIONAL" tmp/${FILE}.out/smali/${FAIL} >> "$LOG"
1372 SID=(${SID[@]} $FAIL)
1373 done
1375 [[ `grep FAILED ${JAR}.log` != "" ]] && print_error "failed $NAME.patch"
1376 garbage tmp/${FILE}.out
1378 # run special $JAR.sh scripts if any JAR needs special attention
1379 if [ -f patches/$P/$A/$R/$B/${JAR}.sh ]; then
1380 printtask "... apply $JAR.sh ..."
1381 . patches/$P/$A/$R/$B/${JAR}.sh 2>&1 > ${JAR}.log
1382 cat ${JAR}.log | tee -a "$LOG"
1384 done
1386 [[ -n ${APKS[@]} ]] && echo "### apk patch $P/$A/$ROMTYPE/$B ###"
1387 echo "### apk patch $P/$A/$R/$B ###" >> "$LOG"
1388 for FILE in ${APKS[@]}; do
1389 APK=${FILE##*/}
1390 if [ -d "tmp/${FILE}.out" ] ; then
1391 printtask "... apply $APK.patch ..."
1392 patch -Nls -p2 -d tmp/${FILE}.out < $PARB/${APK}.patch 2>&1 >> ${APK}.log
1393 cat ${APK}.log | tee -a "$LOG"
1394 [[ `grep FAILED ${APK}.log` != "" ]] && apk_check
1395 garbage tmp/${FILE}.out
1396 else
1397 echo "No $APK in your ROM. Not able to patch this file."
1398 echo "## No $APK in ROM ###" >> "$LOG"
1400 done
1401 [[ -n ${IMGS[@]} ]] && echo "### img patch $P/$A/$ROMTYPE/$B ###"
1402 echo "### img patch $P/$A/$R/$B ###" >> "$LOG"
1403 for FILE in ${IMGS[@]}; do
1404 IMG=${FILE##*/}
1405 printtask "... applying $IMG.patch ..."
1406 patch -Nls -p1 -d boot/ramdisk < $PARB/${IMG}.patch >> "$LOG" || print_error "failed patching $FILE"
1407 done
1409 # Text Editing Functions --this can really do just about anything. Needs a working restore!
1410 [[ -n ${TXTS[@]} ]] && echo "### text patch $P/$A/$ROMTYPE/$B ###"
1411 echo "### text patch $P/$A/$R/$B ###" >> "$LOG"
1412 for FILE in ${TXTS[@]}; do
1413 TXT=${FILE##*/}
1414 if [[ -f $PARB/${TXT}.patch ]]; then
1415 printtask "... apply $TXT.patch ..."
1416 patch -Nls -p2 -d system/ < $PARB/${TXT}.patch 2>&1 > ${TXT}.log
1417 #cat ${TXT}.log | tee -a "$LOG"
1418 [[ `grep FAILED ${TXT}.log` != "" ]] && print_error "failed $TXT.patch"
1420 if [[ -f $PAR/${TXT}.awk ]]; then
1421 printtask "... apply $TXT.awk ..."
1422 awk -f $PAR/${TXT}.awk $FILE > text || print_error "failed $TXT.awk"
1423 mv text $FILE
1425 if [[ -f $PAR/${TXT}.sed ]]; then
1426 printtask "... apply $TXT.sed ..."
1427 sed -f $PAR/${TXT}.sed $FILE > text || print_error "failed $TXT.sed"
1428 mv text $FILE
1430 if [[ -f $PAR/${TXT}.sh ]]; then
1431 printtask "... apply $TXT.sh ..."
1432 . $PAR/${TXT}.sh 2>&1 >> ${TXT}.log || print_error "failed $TXT.sh"
1434 garbage system/
1435 done
1436 done
1438 # Device specific scripts- Placement is up-for-review
1439 if [ -f patches/.devices/$DID/$DID.sh ]; then
1440 . patches/.devices/$DID/$DID.sh
1443 # Replace updater-script for CM7- it formats system -Testing
1444 if [ "$ANDR" == "gingerbread" ]; then
1445 replace_updater
1448 # Rebuilding system files
1450 [[ -n ${JID[@]} ]] && printtask "### rebuild jar ###"
1451 for FILE in ${JID[@]}; do
1452 JAR=${FILE##*/}
1453 printtask "... rebuild $JAR ..."
1454 java -Xmx512M -jar smali.jar -a $API tmp/${FILE}.out/smali -o tmp/${FILE}.out/classes.dex >> "$LOG" 2>> "$LOG"
1455 [ -f tmp/${FILE}.out/classes.dex ] || smali_error "failed compiling $JAR !!!"
1456 jar -ufv ${FILE} -C tmp/${FILE}.out/ classes.dex >> "$LOG" 2>> "$LOG"
1457 if [ $FILE == "system/framework/framework.jar" ]; then
1458 jar -ufv ${FILE} -C tmp/${FILE}.out/ preloaded-classes >> "$LOG" 2>> "$LOG"
1460 done
1462 [[ -n ${AID[@]} ]] && printtask "### rebuild apk ###"
1463 for FILE in ${AID[@]}; do
1464 APK=${FILE##*/}
1465 if [ -d tmp/${FILE}.out ]; then
1466 printtask "... rebuild $APK ..."
1467 java -jar apktool.jar b tmp/${FILE}.out >> "$LOG" 2>> "$LOG" || print_error "failed compiling $FILE !!!"
1468 jar -uvf $FILE -C tmp/${FILE}.out/build/apk/ . > /dev/null 2>> "$LOG"
1470 done
1472 [[ -n ${IID[@]} ]] && printtask "### rebuild img ###"
1473 for FILE in ${IID[@]}; do
1474 printtask "### rebuild $FILE ###"
1475 case $FILE in
1476 boot.img)
1477 # Rebuilding boot.img
1478 cd boot
1479 printtask "... rebuilding ramdisk ..."
1480 ./mkbootfs$EXE ./ramdisk | gzip > ramdisk-new.gz
1482 printtask "... composing boot.img ..."
1483 BASE=$(cat boot.img-base)
1484 # hex to decimal in cygwin prefixing "0x" w/ C arithmetic
1485 PGSIZE=$((${PGFORM}$(cat boot.img-pagesize)))
1486 CMDLINE=$(cat boot.img-cmdline)
1487 echo "cmdline = no_console_suspend=1 console=null $CMDLINE" >> "$LOG"
1488 echo "pagesize = $PGSIZE" >> "$LOG"
1489 echo "base = $BASE" >> "$LOG"
1490 printtask "... rebuilding boot.img ..."
1491 ./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 || print_error "failed building boot.img"
1492 cd ..
1495 print_error "cannot process $FILE"
1497 esac
1498 done
1500 printtask "### create $UPDATE.zip ###"
1501 jar -cfv tmp.zip ${IID[@]} ${JID[@]} ${AID[@]} ${CID[@]} ${TID[@]} META-INF >> "$LOG" 2>> "$LOG"
1502 java -jar signapk.jar updatecert.pem updatekey.pk8 tmp.zip $UPDATE.zip 2>> "$LOG"
1504 \mv $RESTORE.zip $UPDATE.zip ..
1505 echo "### finished ###"
1506 echo "flash $UPDATE.zip to install the patched files"
1507 echo "flash $RESTORE.zip to restore the original system"
1509 # add announcement- optional.
1510 for P in ${PID[@]}; do
1511 [ -d patches/$P/$ANDR ] && A=$ANDR || A=all
1512 [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
1513 [ -d patches/$P/$A/$R/$BID ] && B=$BID || B=latest
1514 if [ -f patches/$P/$A/$R/$B/README.txt ]; then
1515 cat patches/$P/$A/$R/$B/README.txt
1516 elif [ -f patches/$P/$A/$R/README.txt ]; then
1517 cat patches/$P/$A/$R/README.txt
1518 elif [ -f patches/$P/$A/README.txt ]; then
1519 cat patches/$P/$A/README.txt
1521 done
1523 cd ..
1524 \rm -rf "$TEMP"