Change 'extension' to 'pd2.0' in documentation
[auto-patcher.git] / auto_patcher
blob8b305c1158ec7119969617a2e0a1e0efeba8e036
1 #!/bin/bash
3 # version: beta v2.3.0, 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.36. (2.3.0) Final (LTS) version. Stable for now until upstream breaks any patches
89 # Thanks to KicknGuitar for the OSX testing!
91 # SHELL PROGRAMMING NOTES:
92 # [ -s FILE ]
93 # [[ -n STRING ]] or [[ -z STRING ]]
95 VERSION="Auto-Patcher v2.3.0"
97 echo ""
98 echo "The $VERSION"
99 echo ""
100 # by pastime1971 and mateorod
103 # pass variables to children
104 set -a
106 # function for converting hex to binary- for testing
107 #function h2b { echo "obase=2; ibase=16; $( echo "$*" | sed -e 's/0x//g' -e 's/\([a-z]\)/\u\1/g' )" | bc; }
109 printusage () {
110 echo " The $VERSION"
111 echo ""
112 echo "usage: $0 [ROMNAME].zip [MODS] [ROMTYPE] [PATCHDATE]"
113 echo ""
114 echo "[MODS] can be pdroid, pd2.0, v6supercharger, tabletUI, etc..."
115 echo "MODS can be combined by separating them with a comma and NO SPACES!!!"
116 echo ""
117 echo "[ROMTYPE] can be cm, aokp, aosp or pa [recommended]"
118 echo "[PATCHDATE] will select older patch versions, e.g 2012103 [optional]"
119 echo ""
120 echo "try \"$0 ROM.ZIP -h\" to see what is available"
121 echo ""
122 exit
125 debug () {
126 echo "! please refer to $LOG for any debugging"
127 echo ""
128 echo "! patching failed."
129 echo "! it may be possible to patch $ROM using other patch version"
130 echo ""
131 cleanup
132 printusage
135 cleanup () {
136 [ -d "$TEMP" ] && \rm -rf "$TEMP"
138 printerror () {
139 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
140 echo "!!! error: $@"
141 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
142 echo ""
143 debug
146 romerror () {
147 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
148 echo "!!! error: $@"
149 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
150 echo ""
151 echo "!!!There are only four supported ROMTYPES:"
152 echo "1) cm 2) aokp 3) aosp 4) pa"
153 echo ""
154 echo "Find out which of the above ROMTYPES your rom is based on and try again!"
155 echo ""
156 echo "**********************************************"
157 cleanup
158 printusage
161 misspatch () {
162 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
163 echo "!! error: $@ "
164 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
165 echo ""
166 echo "!! The Auto-Patcher could not find its patches and/or tools!"
167 echo ""
168 echo "* Did you run ./batch.sh first?"
169 echo ""
170 echo "The patch_patches.tgz and patch_tools.tgz need to be in this directory,"
171 echo " one way or another..."
172 echo ""
173 cleanup
174 printusage
177 misstools () {
178 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
179 echo "Your set-up is missing a dependency."
180 echo "We require JDK, patch and cpio."
181 echo ""
182 echo ""
183 echo "Each distro is different"
184 echo "but you must ensure you have them all installed and try again."
185 echo ""
186 cleanup
187 printusage
190 printtask () {
191 echo "" >> "$LOG"
192 echo $@ | tee -a "$LOG"
195 copyfailed () {
196 echo ""
197 echo "File for which patching failed:"
198 echo "---------------------------------------------"
199 echo ""
200 echo "${FAIL##*/}"
201 echo ""
202 echo "---------------------------------------------"
203 echo ""
204 echo "We can try to add a premade copy of those files."
205 echo "This sometimes works."
206 echo ""
207 echo "But IT MAY NOT WORK!!!"
208 echo ""
209 echo ""
210 askconsent && return 0 || return 1
213 askconsent () {
214 sleep 1
215 echo -n "Do you want to proceed ? (YES/no) "
216 read PROCEED
217 echo "askconsent final = ${PROCEED}: for $FAIL" >> "$LOG"
218 case $PROCEED in
219 [Yy] | [Yy][Ee][Ss])
220 return 0; # successful:
223 return 1; # error
225 esac
228 abspath () {
229 case `uname -s` in
230 CYGWIN*)
231 echo $(cygpath -ua "$1")
233 Darwin)
234 #[[ $(echo $1 | awk '/^\//') == $1 ]] && echo "$1" || echo "$PWD/$1"
235 [[ ${1:0:1} == "/" ]] && echo "$1" || echo "$PWD/$1"
237 Linux)
238 echo $(readlink -f "$1")
241 if [[ ${1:0:1} == "/" ]]; then
242 echo "$1"
243 elif [[ ${1:0:2} == "./" ]]; then
244 echo "$PWD/${1:2}"
245 else
246 echo "$PWD/$1"
249 esac
252 extpath () {
253 case `uname -s` in
254 CYGWIN*)
255 echo $(cygpath -da "$1")
258 echo $(abspath "$1")
260 esac
263 #contains () { for e in "${@:2}"; do [[ "$e" = "$1" ]] && return 0; done; return 1; }
265 parse () { echo "${1%%_*}"; }
267 getbuildprop () {
268 if [ -f system/build.prop ]; then
269 result=(`grep $1 system/build.prop | tr -d '\r' | tr '=' ' '`)
270 echo "${result[1]}"
271 else
272 echo ""
277 aosp_mod_check() {
278 jar -xvf "$ROMX" system/framework/framework2.jar >> $LOG
279 if [ -s system/framework/framework2.jar ]; then
280 AOSP=aosp-mod
281 else
282 AOSP=aosp-jb
284 echo $AOSP
287 proberom () {
288 VID=$(getbuildprop ro.build.version.release)
290 if [ "${VID%.*}" == "2.3" ]; then
291 ANDR=gingerbread
292 RID=cm;
293 elif [ "${VID%.*}" == "4.0" ]; then
294 ANDR=ics
295 MID=$(getbuildprop ro.rommanager.developerid)
296 [[ "$MID" == "" ]] && MID=$(parse $(getbuildprop ro.modversion))
297 DID=$(getbuildprop ro.product.device)
298 [[ "$DID" == "" ]] && DID=$(getbuildprop ro.build.product)
300 case "$MID" in
301 pa|cm9|cm|cyanogenmod*)
302 RID=cm;
304 aokp|teamkang)
305 RID=aokp;
308 RID=aosp;
309 echo "!!! $MID is not supported yet, trying generic latest !!!"
311 esac
312 elif [ "${VID%.*}" == "4.1" ]; then
313 ANDR=jellybean
314 MID=$(getbuildprop ro.build.display.id)
315 [[ "$MID" == "" ]] && MID=$(parse $(getbuildprop ro.build.display.id))
316 DID=$(getbuildprop ro.product.device)
317 [[ "$DID" == "" ]] && DID=$(getbuildprop ro.build.product)
319 case "$MID" in
320 cm10|cm*|cyanogenmod*)
321 RID=cm;
323 aokp*|teamkang)
324 RID=aokp;
326 pa*)
327 RID=pa;
330 RID=`aosp_mod_check`;
332 esac
333 else
334 printerror "unknown ROM version $VID"
336 echo "... result of ROM check: version= $ANDR rom= $RID ..."
339 # NEW # Advanced build date query
340 # This should set the patch date automatically.
342 buildquery() {
343 UTC=$(getbuildprop ro.build.date.utc)
344 case `uname -s` in
345 [Dd]arwin)
346 BDID=`date -r $UTC +%Y%m%d`
347 echo "#### Darwin Build Query used ####" >> "$LOG"
350 BDID=`date -d "1970-01-01 UTC $UTC seconds" +"%Y%m%d"`
351 echo "#### Standard Build Query ####" >> "$LOG"
353 esac
355 if [ $# -gt 3 ]; then
356 BID=${4//,/ };
357 elif
358 [[ ${#PID[@]} -gt 1 ]]; then
359 BID=latest;
360 else
361 for P in ${PID[@]}; do
362 [ -d patches/$P/$ANDR ] && A=$ANDR || A=all
363 [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
364 [ -d patches/$P/$A/$R/$BID ] && B=$BID || B=latest
365 PAR=patches/$P/$A/$R
366 PARB=patches/$P/$A/$R/$B
367 DATES=`ls patches/$P/$A/$R/ | grep '201*'`
368 # attempt to set builds before extant patches to earliest
369 for i in $DATES; do
370 if [[ $i -lt $BID ]]; then
371 BID=$i
373 if [[ $(($BID-$BDID)) -lt 0 ]]; then
374 BID=$i
375 else
376 for g in $DATES; do
377 if [[ $(($BDID-$g)) -eq 0 ]]; then
378 BID=$g
379 break
380 elif [[ $(($BDID-$g)) -gt 0 ]] && [[ $g -gt $BID ]]; then
381 BID=$g
383 done
385 done
386 echo "... $PID patch date has been set to: $BID..."
387 B=$BID
388 done
392 proberil () {
393 RIL=$(getbuildprop ro.telephony.ril_class)
394 [[ -n $RIL ]] && printtask "... result of ROM check: RIL= $RIL"
397 garbage () {
398 unset GARBAGE
399 GARBAGE=($(find $@ -name "*.orig")) && rm -rf ${GARBAGE[@]} && echo "GARBAGE= ${GARBAGE[@]}" >> "$LOG"
400 GARBAGE=($(find $@ -name "*.rej")) && rm -rf ${GARBAGE[@]} && echo "GARBAGE= ${GARBAGE[@]}" >> "$LOG"
403 helpcheck () {
404 cd patches
405 echo ""
406 echo "AVAILABLE PATCHES"
407 echo ""
408 echo "Note: There are ONLY 4 ROMTYPES: [pa, cm, aokp, and aosp]."
409 echo "The Auto-Patcher will automatically slot if you need aosp-jb or aosp-mod"
410 echo ""
411 echo "*******************************************************************"
412 echo ""
413 echo "Please keep in mind that the Android flavor {ics, jellybean..etc}"
414 echo " is there only for your information and is not needed on the command line"
415 echo ""
416 find -H * -name 2012* | more -d -10
417 echo ""
418 echo "*****************************************************************"
419 echo ""
420 echo "All listed mods are currently supported. So if you only see older patches,"
421 echo "that simply means those early patches are still working for current roms."
422 echo ""
423 cleanup
424 printusage
427 rominfo () {
428 echo "# VER= $VERSION" >> "$LOG"
429 echo "# OS= $OSTYPE ${BIT}bit" >> "$LOG"
430 echo "# VID= $VID" >> "$LOG"
431 echo "# PID= ${PID[@]}" >> "$LOG"
432 echo "# MID= $MID" >> "$LOG"
433 echo "# RID= $RID" >> "$LOG"
434 echo "# ANDR= $ANDR" >> "$LOG"
435 echo "# BDID= $BDID" >> "$LOG"
436 echo "# DID= $DID" >> "$LOG"
437 echo "# BID= $BID" >> "$LOG"
441 [[ $# -lt 1 || $1 == "-h" || -z $1 ]] && printusage
443 ROOT=$(abspath `dirname "$0"`)
444 TOOL="$ROOT/patch_tools.tgz"
445 PTCH="$ROOT/patch_patches.tgz"
446 ROM=$(abspath "$1")
447 ROMX=$(extpath "$1")
449 if [[ $OSTYPE == "cygwin" ]]; then
450 EXE=".exe"
451 PGFORM="0x"
454 DATE=`date +%Y%m%d%H%M%S`
455 TEMP=$(abspath ./tmp$DATE)
456 LOG=$(abspath ./log$DATE.txt)
457 #echo $ROOT $TOOL $ROM $TEMP "$LOG"
458 echo "ROOT= $ROOT" >> "$LOG"
459 echo "TOOL= $TOOL" >> "$LOG"
460 echo "ROM= $ROM" >> "$LOG"
461 echo "ROMX= $ROMX" >> "$LOG"
463 \rm -f restore.zip update.zip
465 CHK=$(getconf LONG_BIT)
466 if [[ $CHK = "64" ]]; then
467 BIT=64
468 else [[ $BIT = "32" ]];
469 BIT=32
473 printtask "### verify tools ###"
474 which patch >> "$LOG" || misstools "Missing \"patch diff command\""
475 which tar >> "$LOG" || misstools "Missing \"tar archive command\""
476 which jar >> "$LOG" || misstools "Missing \"jar (JDK)\""
477 which java >> "$LOG" || misstools "Missing \"java (JRE)\""
478 which cpio >> "$LOG" || misstools "Missing \"cpio archive command\""
479 [ -f "$TOOL" ] || misspatch "patch tool is missing"
480 [ -f "$PTCH" ] || misspatch "patch patches are missing"
481 printtask "### verify ROM ###"
482 [ -f "$ROM" ] || printerror "Cannot find $ROM"
483 echo "### running $0 $@" >> "$LOG"
484 mkdir "$TEMP"
485 cd "$TEMP"
487 PATH="$TEMP:$PATH"
488 export PATH
489 echo "PATH= $PATH" >> "$LOG"
491 printtask "### unzip patch tools ###"
492 tar xzf "$TOOL" > /dev/null || printerror "failed extracting tools !!!"
494 # Get OS specific tools
495 case `uname -s` in
496 CYGWIN*)
497 \mv cygwin/aapt.exe .
498 \mv cygwin/*.exe boot/
499 \rm -rf cygwin # why? If you want to we can keep/add it to mac/linux, though.
501 [Dd]arwin)
502 \mv mac/aapt .
504 [Ll]inux)
505 \mv linux/aapt .
508 \mv linux/aapt .
510 esac
511 # Ensure aapt permissions - possibly to one day be expanded to all tools in a function.
512 chmod a+x aapt$EXE
514 printtask "### unzip patch patches ###"
515 tar xzf "$PTCH" > /dev/null || printerror "failed extracting patches !!!"
517 # we need build.prop for both RIL probing and ROM probing
518 jar -xvf "$ROMX" system/build.prop 2>> "$LOG" || printerror "failed extracting build.prop from $ROMX !!!"
520 # rom + ril type
521 proberil
522 printtask "### probe rom type ###"
523 proberom
525 # rom type
526 if [ $# -gt 1 ]; then
527 PIT=$2
528 if [[ $PIT = "-h" ]]; then
529 helpcheck
532 if [ $# -gt 2 ]; then
533 RID=$3
534 case "$RID" in
535 cm10|cm*|cyanogenmod*)
537 aokp*|teamkang)
538 RID=aokp;
540 pa*)
541 RID=pa;
543 aosp*)
544 RID=`aosp_mod_check`;
547 romerror "$RID is not a valid ROMTYPE!!"
549 esac
550 echo "... Using $RID patches ..."
551 else
552 echo "... Using results of ROM check ..."
555 # patch type
556 if [ $# -gt 1 ]; then
557 PIL=${2//,/ }
558 else
559 #PIL=`ls patches/`
560 PIL="pdroid v6supercharger"
561 echo "No mods selected. Defaulting to the v6 and Pdroid, just because we can."
563 # a hack to make sure pdroid is the first mod
564 [[ ${PIL[@]} =~ pdroid ]] && PID=(pdroid)
565 for P in ${PIL[@]}; do
566 [[ ${PID[@]} =~ $P ]] || PID=(${PID[@]} $P)
567 done
569 # Build date
570 if [ $# -gt 3 ]; then
571 BID=$4
572 # even here, we should try and create a function that matches as close to the above BID as possible
573 echo "... $PID patch date set manually to $BID ..."
574 else
575 buildquery
578 # echo ROM info
579 rominfo
581 if [[ ( "$PID" == "-h" || "$RID" == "-h" || "$BID" == "-h" ) ]]; then
582 helpcheck
583 else
584 for P in ${PID[@]}; do
585 [ -d patches/$P ] || printerror "$P is not available"
586 [ -d patches/$P/$ANDR ] && R=$ANDR || A=all
587 [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
588 #[ -d patches/$P/$A/$R/$BID ] && B=$BID || B=latest
590 unset FILES
591 [ -f patches/$P/files.txt ] && FILES=$(cat patches/$P/files.txt)
592 [ -f patches/$P/$A/files.txt ] && FILES=$(cat patches/$P/$A/files.txt)
593 [ -f patches/$P/$A/$R/files.txt ] && FILES=$(cat patches/$P/$A/$R/files.txt)
594 for FILE in ${FILES[@]}; do
595 case "$FILE" in
596 +system/*)
597 [[ ${CID[@]} =~ ${FILE:1} ]] && printerror "${FILE:1} is already being copied" || CID=(${CID[@]} ${FILE:1})
599 -system/*)
600 [[ ${EID[@]} =~ ${FILE:1} ]] || EID=(${EID[@]} ${FILE:1})
602 system/*.jar)
603 [[ ${JID[@]} =~ $FILE ]] || JID=(${JID[@]} $FILE)
605 system/*.apk)
606 [[ ${AID[@]} =~ $FILE ]] || AID=(${AID[@]} $FILE)
608 *.img)
609 [[ ${IID[@]} =~ $FILE ]] || IID=(${IID[@]} $FILE)
611 system/*)
612 [[ ${TID[@]} =~ $FILE ]] || TID=(${TID[@]} $FILE)
614 esac
615 done
616 done
617 echo "### DELETE FILES= ${EID[@]}" >> "$LOG"
618 echo "### COPY FILES= ${CID[@]}" >> "$LOG"
619 echo "### JAR FILES= ${JID[@]}" >> "$LOG"
620 echo "### APK FILES= ${AID[@]}" >> "$LOG"
621 echo "### IMG FILES= ${IID[@]}" >> "$LOG"
622 echo "### TXT FILES= ${TID[@]}" >> "$LOG"
624 printtask "### unzip rom ###"
625 # extract system files
626 jar -xvf "$ROMX" ${JID[@]} ${AID[@]} ${IID[@]} ${TID[@]} 2>> "$LOG" || printerror "failed extracting ${JID[@]} ${AID[@]} ${IID[@]} from $ROMX !!!"
627 #\cp -a system/ backup # only for debugging purpose
629 printtask "### create updater script ###"
630 jar -xvf "$ROMX" META-INF/com/google/android/updater-script META-INF/com/google/android/update-binary
631 jar -xvf "$ROMX" META-INF/com/google/android/update-binary-installer 1> /dev/null 2> /dev/null
632 if [ -f META-INF/com/google/android/update-binary-installer ]; then
633 printtask "... using Aroma installer ..."
634 \mv META-INF/com/google/android/update-binary-installer META-INF/com/google/android/update-binary
635 else
636 printtask "... using Clockworkmod installer ..."
638 if [ -f META-INF/com/google/android/update-binary ]; then
639 #echo "# original updater-script" >> "$LOG"
640 #cat META-INF/com/google/android/updater-script >> "$LOG"
642 cat META-INF/com/google/android/updater-script | grep ^mount | grep "/system" | head -n 1 > updater-script
643 [ -s updater-script ] || echo "run_program(\"/sbin/busybox\", \"mount\", \"/system\");" > updater-script
645 for FILE in ${EID[@]}; do
646 # restore the original
647 jar -xvf "$ROMX" $FILE 2> /dev/null || printtask "!!! warning $FILE cannot be restored !!!"
648 done
649 for FILE in ${CID[@]}; do
650 # replace with the original or delete it
651 jar -xvf "$ROMX" $FILE 2> /dev/null || echo "delete(\"/$FILE\");" >> updater-script
652 done
654 echo "package_extract_dir(\"system\", \"/system\");" >> updater-script
655 # add permission management
656 cat META-INF/com/google/android/updater-script | grep set_perm | grep "/system" >> updater-script
657 # TODO: remove if we don't need it any more
658 for P in ${PID[@]}; do
659 if [ -s patches/$P/$A/set_perm.txt ]; then
660 echo "...setting permissions..."
661 cat patches/$P/$A/set_perm.txt >> updater-script
663 done
665 cat META-INF/com/google/android/updater-script | grep ^unmount | grep "/system" | head -n 1 >> updater-script
667 [[ ${IID[@]} =~ boot.img ]] && cat META-INF/com/google/android/updater-script | grep "boot.img" >> updater-script
669 cat updater-script > META-INF/com/google/android/updater-script
671 echo "# generated updater-script" >> "$LOG"
672 cat updater-script >> "$LOG"
673 else
674 # this is not enough any more
675 jar -xvf update-template.zip META-INF/com/google/android/update-binary META-INF/com/google/android/updater-script
678 printtask "### create restore.zip ###"
679 jar -cvf tmp.zip ${JID[@]} ${AID[@]} ${IID[@]} META-INF > /dev/null 2>> "$LOG"
680 java -jar signapk.jar updatecert.pem updatekey.pk8 tmp.zip signed.zip 2>> "$LOG"
681 \mv signed.zip restore.zip
682 \rm -f tmp.zip
684 printtask "### decompile system files ###"
686 # decompile system files
687 for FILE in ${JID[@]}; do
688 JAR=${FILE##*/}
689 printtask "... decompile $JAR ..."
690 java -jar baksmali.jar -b -a 15 -o tmp/${FILE}.out/smali ${FILE} >> "$LOG" 2>> "$LOG"
691 if [ $FILE == "system/framework/framework.jar" ]; then
692 printtask "... extract preloaded-classes ..."
693 jar -xvf ${FILE} preloaded-classes >> "$LOG" 2>> "$LOG"
694 \mv preloaded-classes tmp/${FILE}.out/
696 done
697 for FILE in ${AID[@]}; do
698 APK=${FILE##*/}
699 printtask "... decompile $APK ..."
700 if [ $FILE == "system/framework/framework-res.apk" ]; then
701 printtask "... installing framework files ..."
702 java -jar apktool.jar if $FILE
704 java -jar apktool.jar d -f $FILE tmp/$FILE.out
706 done
707 for FILE in ${IID[@]}; do
708 printtask "### unpacking $FILE ###"
709 case "$FILE" in
710 boot.img)
711 if [ -f boot.img ]; then
712 cd boot
713 ./unpackbootimg$EXE -i ../boot.img >> "$LOG" 2>> "$LOG"
714 mkdir ramdisk
715 cd ramdisk
716 gzip -dc ../boot.img-ramdisk.gz | cpio -i >> "$LOG" 2>> "$LOG"
717 cd ../../
718 else
719 printerror "A mod needs to patch boot.img! This rom does not contains boot.img."
723 printerror "cannot process $FILE"
725 esac
726 done
729 # apply system modification
731 for P in ${PID[@]}; do
732 [ -d patches/$P/$ANDR ] && A=$ANDR || A=all
733 [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
734 [ -d patches/$P/$A/$R/$BID ] && B=$BID || B=latest
735 PAR=patches/$P/$A/$R
736 PARB=patches/$P/$A/$R/$B
738 unset FILES COPY DELS JARS APKS IMGS TXTS
739 [ -f patches/$P/files.txt ] && FILES=$(cat patches/$P/files.txt)
740 [ -f patches/$P/$A/files.txt ] && FILES=$(cat patches/$P/$A/files.txt)
741 [ -f patches/$P/$A/$R/files.txt ] && FILES=$(cat patches/$P/$A/$R/files.txt)
742 for FILE in ${FILES[@]}; do
743 case $FILE in
744 +system/*)
745 COPY=(${COPY[@]} ${FILE:1})
747 -system/*)
748 DELS=(${DELS[@]} ${FILE:1})
750 system/*.jar)
751 JARS=(${JARS[@]} $FILE)
753 system/*.apk)
754 APKS=(${APKS[@]} $FILE)
756 *.img)
757 IMGS=(${IMGS[@]} $FILE)
759 system/*)
760 TXTS=(${TXTS[@]} $FILE)
762 esac
763 done
765 [[ -n ${COPY[@]} ]] && printtask "### copy patch $P/$A/$R/$B ###"
766 for FILE in ${COPY[@]}; do
767 printtask "... copying $FILE ..."
768 DEST=${FILE%/*}
769 mkdir -p ./$DEST
770 if [ -e patches/$P/$A/$R/$B/$FILE ]; then
771 [ -e $FILE ] && printtask "... overwriting $FILE from $P ..."
772 \cp -av patches/$P/$A/$R/$B/$FILE ./$DEST >> "$LOG"
773 else
774 printerror "$FILE is missing in the patch"
776 done
778 # unconditional smali copying
779 [ -f patches/$P/smali.txt ] && SMALI=$(cat patches/$P/smali.txt)
780 [ -f patches/$P/$A/smali.txt ] && SMALI=$(cat patches/$P/$A/smali.txt)
781 [ -f patches/$P/$A/$R/smali.txt ] && SMALI=$(cat patches/$P/$A/$R/smali.txt)
782 [ -f patches/$P/$A/$R/$B/smali.txt ] && SMALI=$(cat patches/$P/$A/$R/$B/smali.txt)
783 MOVE=${SMALI##*/}
784 for SMALI in ${SMALI[@]}; do
785 printtask "... overwriting $MOVE ..."
786 \cp -av patches/$P/$ANDR/provisionals/$SMALI tmp/$SMALI >> "$LOG"
787 done
790 [[ -n ${DELS[@]} ]] && printtask "### delete patch $P/$A/$R/$B ###"
791 for FILE in ${DELS[@]}; do
792 printtask "... deleting $FILE ..."
793 \rm -rf ./$FILE
794 done
796 [[ -n ${JARS[@]} ]] && printtask "### jar patch $P/$A/$R/$B ###"
797 for FILE in ${JARS[@]}; do
798 JAR=${FILE##*/}
799 NAME=${JAR%.jar}
800 printtask "... apply $NAME.patch ..."
801 patch -Nls -p1 -d tmp/${FILE}.out < $PARB/${NAME}.patch 2>&1 > ${JAR}.log
802 cat ${JAR}.log | tee -a "$LOG"
803 case "$NAME" in
804 framework)
805 # patch preloaded-classes only if a patch exists
806 if [ -f $PARB/preloaded.patch ]; then
807 printtask "... apply preloaded.patch ..."
808 patch -Nls -p1 -d tmp/${FILE}.out < $PARB/preloaded.patch 2>&1 > preloaded.log
809 cat preloaded.log | tee -a "$LOG"
810 [[ `grep FAILED preloaded.log` != "" ]] && printerror "failed preloaded.patch"
814 echo -n "" # do nothing else
816 esac
817 # substitute failed smali with premade jar
818 unset FAILS
819 FAILS=(`grep FAILED ${JAR}.log | sed -e 's/.*smali\///' | sed -e 's/\.rej//'`)
820 if [[ -n ${FAILS[@]} ]]; then
821 printtask "!!! PATCHING FAILED FOR ${FAILS[@]} !!!"
822 echo "### replace ${FAILS[@]} ###" > ${JAR}.log
823 for FAIL in ${FAILS[@]}; do
824 echo "!!! patch error for $FAIL !!!" >> "$LOG"
825 #cat tmp/$FILE.out/smali/${FAIL}.rej >> "$LOG"
826 [[ ${SID[@]} =~ $FAIL ]] && printerror "$FAIL was already replaced before"
827 [ -f patches/$P/$ANDR/provisionals/${FILE}.out/smali/${FAIL} ] || printerror "failed $FAIL in $JAR cannot be remedied"
828 copyfailed || printerror "failed $NAME.patch"
829 printtask "... replace $FAIL ..."
830 \rm -rf tmp/${FILE}.out/smali/${FAIL} tmp/${FILE}.out/smali/${FAIL}.orig tmp/${FILE}.out/smali/${FAIL}.rej
831 \cp -av patches/$P/$ANDR/provisionals/${FILE}.out/smali/${FAIL} tmp/${FILE}.out/smali/${FAIL} >> "$LOG" || echo "FAILED to cp $FAIL" >> ${JAR}.log
832 SID=(${SID[@]} $FAIL)
833 done
835 [[ `grep FAILED ${JAR}.log` != "" ]] && printerror "failed $NAME.patch"
836 garbage tmp/${FILE}.out
837 if [ -f patches/$P/$A/$R/$B/{JAR}.sh ]; then
838 printtask "... apply $JAR.sh ..."
839 . patches/$P/$A/$R/$B/{JAR}.sh 2>&1 > ${JAR}.log
840 cat ${JAR}.log | tee -a "$LOG"
842 done
844 [[ -n ${APKS[@]} ]] && printtask "### apk patch $P/$R/$B ###"
845 for FILE in ${APKS[@]}; do
846 APK=${FILE##*/}
847 if [ -d "tmp/${FILE}.out" ] ; then
848 printtask "... apply $APK.patch ..."
849 patch -Nls -p2 -d tmp/${FILE}.out < $PARB/${APK}.patch 2>&1 >> ${APK}.log
850 cat ${APK}.log | tee -a "$LOG"
851 [[ `grep FAILED ${APK}.log` != "" ]] && printerror "failed $APK.patch"
852 garbage tmp/${FILE}.out
853 else
854 echo "No $APK in your ROM. Not able to patch this file."
855 echo "## No $APK in ROM ###" >> "$LOG"
857 done
858 [[ -n ${IMGS[@]} ]] && printtask "### img patch $P/$A/$R/$B ###"
859 for FILE in ${IMGS[@]}; do
860 IMG=${FILE##*/}
861 #NAME=${IMG%.img}
862 echo "... applying $IMG.patch ..."
863 patch -Nls -p1 -d boot/ramdisk < $PARB/${IMG}.patch >> "$LOG" || printerror "failed patching $FILE"
864 # Special Instructions- married to boot.img patching by necessity, not design. When
865 # added to it's own section, it only runs for one mod. WIP.
866 #if [ -f patches/$P/$R/special_instructions.sh ]; then
867 # . patches/$P/$R/special_instructions.sh
868 #elif [ -f patches/$P/special_instruction.sh ]; then
869 # . patches/$P/special_instructions.sh
871 done
873 [[ -n ${TXTS[@]} ]] && printtask "### text patch $P/$A/$R/$B ###"
874 for FILE in ${TXTS[@]}; do
875 TXT=${FILE##*/}
876 if [[ -f $PARB/${TXT}.patch ]]; then
877 printtask "... apply $TXT.patch ..."
878 patch -Nls -p2 -d system/ < $PARB/${TXT}.patch 2>&1 > ${TXT}.log
879 #cat ${TXT}.log | tee -a "$LOG"
880 [[ `grep FAILED ${TXT}.log` != "" ]] && printerror "failed $TXT.patch"
882 if [[ -f $PAR/${TXT}.awk ]]; then
883 printtask "... apply $TXT.awk ..."
884 awk -f $PAR/${TXT}.awk $FILE > text || printerror "failed $TXT.awk"
885 mv text $FILE
887 if [[ -f $PAR/${TXT}.sed ]]; then
888 printtask "... apply $TXT.sed ..."
889 sed -f $PAR/${TXT}.sed $FILE > text || printerror "failed $TXT.sed"
890 mv text $FILE
892 if [[ -f $PAR/${TXT}.sh ]]; then
893 printtask "... apply $TXT.sh ..."
894 . $PAR/${TXT}.sh 2>&1 >> ${TXT}.log || printerror "failed $TXT.sh"
896 garbage system/
897 done
898 done
900 # Rebuilding system files
902 [[ -n ${JID[@]} ]] && printtask "### rebuild jar ###"
903 for FILE in ${JID[@]}; do
904 JAR=${FILE##*/}
905 printtask "... rebuild $JAR ..."
906 java -Xmx512M -jar smali.jar -a 15 tmp/${FILE}.out/smali -o tmp/${FILE}.out/classes.dex >> "$LOG" 2>> "$LOG"
907 [ -f tmp/${FILE}.out/classes.dex ] || printerror "failed compiling $JAR !!!"
908 jar -ufv ${FILE} -C tmp/${FILE}.out/ classes.dex >> "$LOG" 2>> "$LOG"
909 if [ $FILE == "system/framework/framework.jar" ]; then
910 jar -ufv ${FILE} -C tmp/${FILE}.out/ preloaded-classes >> "$LOG" 2>> "$LOG"
912 done
914 [[ -n ${AID[@]} ]] && printtask "### rebuild apk ###"
915 for FILE in ${AID[@]}; do
916 APK=${FILE##*/}
917 printtask "... rebuild $APK ..."
918 java -jar apktool.jar b tmp/${FILE}.out >> "$LOG" 2>> "$LOG" || printerror "failed compiling $FILE !!!"
919 jar -uvf $FILE -C tmp/${FILE}.out/build/apk/ . > /dev/null 2>> "$LOG"
920 #\rm -r tmp/${FILE}.out
921 done
923 [[ -n ${IID[@]} ]] && printtask "### rebuild img ###"
924 for FILE in ${IID[@]}; do
925 printtask "### rebuild $FILE ###"
926 case $FILE in
927 boot.img)
928 # Rebuilding boot.img
929 cd boot
930 printtask "... rebuilding ramdisk ..."
931 ./mkbootfs$EXE ./ramdisk | gzip > ramdisk-new.gz
933 printtask "... composing boot.img ..."
934 BASE=$(cat boot.img-base)
935 # convert hex to decimal in cygwin by prefixing "0x" and C-style arithmetic
936 PGSIZE=$((${PGFORM}$(cat boot.img-pagesize)))
937 CMDLINE=$(cat boot.img-cmdline)
938 echo "cmdline = no_console_suspend=1 console=null $CMDLINE" >> "$LOG"
939 echo "pagesize = $PGSIZE" >> "$LOG"
940 echo "base = $BASE" >> "$LOG"
942 printtask "... rebuilding boot.img ..."
943 ./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"
944 cd ..
947 printerror "cannot process $FILE"
949 esac
950 done
952 printtask "### create update.zip ###"
953 jar -cfv tmp.zip ${IID[@]} ${JID[@]} ${AID[@]} ${CID[@]} ${TID[@]} META-INF >> "$LOG" 2>> "$LOG"
954 java -jar signapk.jar updatecert.pem updatekey.pk8 tmp.zip update.zip 2>> "$LOG"
955 #\rm -f tmp.zip
957 #echo "# miscellaneous log" >> "$LOG"
959 \mv restore.zip update.zip ..
960 echo "### finished ###"
961 echo "flash update.zip to install the modified"
962 echo "flash restore.zip to restore the original"
964 # add announcement
965 for P in ${PID[@]}; do
966 [ -d patches/$P/$ANDR ] && A=$ANDR || A=all
967 [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
968 if [ -f patches/$P/$A/$R/README.txt ]; then
969 cat patches/$P/$A/$R/README.txt
970 elif [ -f patches/$P/$A/README.txt ]; then
971 cat patches/$P/$A/README.txt
973 done
975 cd ..
976 \rm -rf "$TEMP"