mateorod

auto_patcher-OSX-ping-test

Jul 28th, 2013
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 72.43 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Look after changelog for script/patch version info.
  4.  
  5. # PDroid created by svyat. Official PDroid thread is
  6. # http://forum.xda-developers.com/showthread.php?t=1357056
  7.  
  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
  11.  
  12. # ChangeLog
  13. # 2012.05.00. (v0.1)
  14. # 2012.05.12. (v0.2)   allows user to specify a different version of patches
  15. # 2012.05.13. (v0.3.0) force "patch -N", remove temp upon failure, generate log output
  16. # 2012.05.13. (v0.3.1) revised the generation of updater-script (SGS2)
  17. # 2012.05.15.          added AOSP patches
  18. # 2012.05.15.          added crespo-aosp patches; device-specific patching if patch exists
  19. # 2012.05.15.          remove restore.zip upon failure
  20. # 2012.05.15. (v0.4.1) added CM7 (GB) patches
  21. # 2012.05.18. (v0.4.2) added checking for tool commands
  22. # 2012.05.21. (v0.4.3) apply device-specific patches only when it is specified
  23. # 2012.05.26.            added task title to log
  24. # 2012.05.31. (v0.4.4) CM9 patches updated, crespo-aosp added
  25. # 2012.06.04. (v0.4.5) added checking tools and rom, added aroma installer support
  26. #                        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. (v1.9.6) Conditional support for Official cm10 and aokp-jb builds
  57. # 2012.08.23. (v1.9.7) Final/confirmed pdroid support for Official CM10 and AOKP-JB
  58. # 2012.09.04  (v1.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. (v2.0.0) Added .apk patching
  62. #                       Added 3G Dongle support for Nexus 7 (trevd)
  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
  67. # 2012.09.11. (v2.0.1) Cygwin support fixes
  68. # 2012.09.11. (v2.0.2) Bugfix for 3gdongle (all)
  69. # 2012.09.16. (v2.1.0) Significant restructuring of core operations; added edit text file
  70. #                       fix for "method-cap" errors in pdroid.
  71. # 2012.09.20. (v2.1.1) Fix for Phone FC's; OSX support finalized; ContentResolver resolved
  72. # 2012.09.28. (v2.1.2) Hotfix for failed pdroid patching. Logging improvements.
  73. # 2012.10.07. (v2.1.5) Final pdroid support for CM7.2 Official Releases
  74. #                       Added aosp-mod patches for pdroid patching of AOSP kangs
  75. #                       Switched to Chainfire's (f)aapt for speed/space reasons
  76. # 2012.10.13. (v2.1.6) New PDroid patches for aokp-jb and cm10 4.1.2
  77. #                       Revert (f)aapt- temporary due to reported issues
  78. # 2012.10.15. (v2.17) New (again) CM10 pdroid patches.
  79. #                     Move files to be copied into $p/$R/$B
  80. #                      -This required a bunch of file moving-report broken symlinks.
  81. #                     Some error check on the old RIL delete functions (thanks bundrik)
  82. # 2012.10.16. (v2.1.7.5) Fix FC on phone with CM10 pdroid patches
  83. # 2012.10.19. (v2.1.9) Testing CM10 support for PDroid Extension by CollegeDev.
  84. # 2012.10.22. (v2.2.0) aosp/aokp and cm10 support for pdroid extension by CollegeDev
  85. #                       MAJOR reworking of patches and improved query. only 4 mod types:
  86. #                       cm, aokp, aosp, and pa. See our OP for details.
  87. # 2012.10.26. (v2.3.0) Final (LTS) version. Stable for now until upstream breaks any patches
  88. #                       Thanks to KicknGuitar for the OSX testing!
  89. # 2012.11.01. (v2.3.1) Fix for pdroid RIL deletion for older roms
  90. #                       Better Build query logic and help messages
  91. # 2012.11.02. (v2.3.5) Add Evervolv support for all mods- Thanks to new maintainer poncik!
  92. #                       Fix method cap for pdroid/pd2.0 for AOKP and kangs
  93. # 2012.11.06. (v2.4.0) Update pd2.0 patches to latest pd2.0 release- all except Evervolv
  94. #                       add GsmService to smali.txt; improve rom probe
  95. # 2012.11.08. (v2.4.2) Add Evervolv support for pd2.0; Device-specific scripts
  96. #                       Will display error if using a mod on an inapplicable rom
  97. # 2012.11.09. (v2.4.5) Error catch for random method issues, fix pd2.0 Evervolv and aosp-jb
  98. # 2012.11.17. (v2.4.6) Fix pdroid for CM7. FINALLY!.
  99. # 2012.11.17. (v2.4.7) Internal debugging improvements
  100. # 2013.01.13. (v2.5.0) Added 'openpdroid' mod --Open-Source Pdroid framework
  101. #                       BIG Improvements to help and error-catch. Try ./auto_patcher -h
  102. # 2013.01.15. (v2.5.1) OpenPDroid support for Evervolv and ParanoidAndroid, hotfix for Mms
  103. # 2013.01.15. (v2.5.2) Fix for cygwin temp files (kobik), provisional telephony files added
  104. # 2013.01.16. (v2.5.3) SlimRom support. Rearrange structure for ApG.
  105. # 2013.01.23. (v2.5.4) Fix small bugs related to rearrange
  106. # 2013.01.25. (v2.5.7) 4.2 TabletUI support [cm,aokp,aosp,Evervolv]
  107. # 2013.01.28. (v2.5.8) Fix for ICS PDroid; small fixes/expansions
  108. # 2013.02.01. (v2.6.0) OpenPdroid 1.0.1 patches (cm, aokp, aosp, evervolv and slimrom. no pa.
  109. #                       Fixed TabletUI for Cm10.1- aosp to come. Removed most old patches.
  110. # 2013.02.01. (v2.6.25) PA OpenPDroid 1.0.1 patches; AOSP TabletUI. removed opd all 1.0.0
  111. # 2013.02.09. (v2.7.0) Add new AOKP, CM9, CM10,CM10.1 and PA (3.0) patches for OPD
  112. # 2013.02.09. (v2.7.5) Add new AOKP, AOSP, CM10.1 and PA (3.0) patches for OPD (Andr. 4.2.2)
  113. # 2013.02.17. (v2.7.9) 4.2.2 opd 4 evervolv; v6supercharger,3gdongle and all but tabletUI.
  114. # 2013.02.20. (v2.8.0) New CM10.1 opd patches. Each BID can have own provisionals now.
  115. # 2013.02.23. (v2.8.2) SlimRom 4.2.2 support (from karamelos), APK recompile safety net.
  116. # 2013.03.01. (v2.8.3) PAC (.aosp-pac) 4.2.2 support (from AussieLambChops), apktool 1.5.2
  117. # 2013.03.03. (v2.8.4) Remove settings.apk from tabUI, replace Mms.apk. Refactors.
  118. # 2013.03.06. (v2.8.5) Bugfixes, mainly.
  119. # 2013.03.17. (v2.8.6) Added 4.2.2 tabletUI test run for CM from Caldair.
  120. # 2013.03.18. (v2.8.6.5) Hotfix from caldair for tabletUI, new SlimRom patches
  121. # 2013.03.18. (v2.8.7) TabletUI support for 10 inch screens (caldair)
  122. # 2013.03.25. (v2.8.8) Fix for Cygwin paths with spaces (from kobik77)
  123. # 2013.04.06. (v2.9.0) Added new tabletUI patches for CM and 4.2 conflicts.
  124. # 2013.04.10. (v2.9.1) Updated aokp openpdroid patches (0405) please report.
  125. # 2013.04.18. (V2.9.2) Added AOSP and SlimROm TabletUI patches (credit: Caldair)
  126. # 2013.04.18. (V2.9.3) Added missing framework2.jar opd aosp 4.1
  127. # 2013.05.08. (v2.9.4) Updates to TabletUI (from Caldair)
  128. # 2013.05.18. (v2.9.5) New AOKP openpdroid patches (0514)
  129. # 2013.05.20. (v2.9.6) New CM and PAC-man (aosp) openpdroid patches.
  130. # 2013.05.27. (v2.9.7) $INCREMENTAL patches for bugs/updates and GPS fix for opd (credits to phillipberndt)
  131. # 2013.05.30. (v2.9.8) Improve apk handling: test fix disappearing apks. New opd: evervolv,slim
  132. # 2013.06.18. (v2.9.83) Introduce auto-update for OSX and Linux...turn off by setting it false in .config
  133. #                        Update PACman patches for OPD and update telephony for PA to silence errors
  134. # 2013.06.18. (v2.9.84) Some environment trickery to speed up search/sort. Add WEBSITE for mods.
  135. #                        Check updating. New AOKP OPD patches
  136. # 2013.06.19. (v2.9.85) Fixes to auto-update. Added changelog.txt
  137. # 2013.06.20. (v2.9.865) Various bugfixes, add color, obfuscate $PROVISIONALS.
  138. # 2013.06.21. (v2.9.87) Added ALTERNATE patches feature- hopefully fixes PA telephony issues.
  139. # 2013.06.22. (2.9.875) New tabletUI patches, revert flawed CONFLICTS feature
  140. # 2013.06.22. (2.9.88) New CM opd patches
  141. # 2013.06.28. (2.9.89) revert baksmali changes for now (still needs work), add new PACman OPD 6/26
  142. # 2013.07.02. (2.9.91) Switch to SlimRom team's aapt and apktool for OSX+Windows (Need OSX builder!)
  143. # 2013.07.07. (2.9.92) New opd patches for Slim/PA; some fixes to tabletUi by Caldair
  144. # 2013.07.17. (2.9.927) Fix missing Mms (finally), support for CM Final, many bugfixes (conflicts, mod ordering, etc.)
  145. # 2013.07.20. (2.9.93) New OPD CM patches, many bugfixes
  146. # 2013.07.20. (2.9.935) New PA, AOKP patches. gitattribute. FIx PING for cygwin. (h/t: gcydtmkq)
  147.  
  148.  
  149. # SHELL PROGRAMMING NOTES:
  150. # [ -s FILE ]
  151. # [[ -n STRING ]] or [[ -z STRING ]]
  152.  
  153. VERSION="v2.9.9356"
  154. # PATCHES_VERSION needs to be incremented every time ANY changes occur to patches.
  155. PATCHES_VERSION="20130726"
  156. CURRENT_ANDROID="4.2.2"
  157.  
  158. # Available ROMTYPES.
  159. # ROMTYPES that are hidden can be forced on the command line. But they are best left
  160. # blank to be  automatically parsed by our romprobe or by just entering the parent...usually AOSP.
  161.  
  162. #Non-obvious ROMTYPE legend
  163. # @.aosp-slim: SlimRom             @.aosp-pac: PAC-man
  164. # @.aosp-ever: Evervolv            @pa: ParanoidAndroid (official jellybean branch only for now)
  165. declare -a AVAILABLE_ROMTYPES=('aokp' 'aosp' 'cm' 'pa' '.aosp-slim' '.aosp-pac' '.aosp-ever')
  166.  
  167. # pass variables to children
  168. set -a
  169.  
  170. # Set any environmental variables
  171. LANG=C
  172.  
  173. # Config file: For now hosting AUTO_UPDATE and PRODUCTION(currently unused). Not under version control.
  174. if [ ! -f .config ]; then
  175.      cp -a ap_scripts/config_template .config
  176. fi
  177. source .config
  178.  
  179. # Colorize and add text parameters
  180. red=$((tput setaf 1) 2>/dev/null)              # red
  181. grn=$((tput setaf 2) 2>/dev/null)              # green
  182. cyan=$((tput setaf 6) 2>/dev/null)             # cyan
  183. txtBold=$((tput bold) 2>/dev/null)             # Bold
  184. txtReset=$((tput sgr0) 2>/dev/null)            # Reset
  185.  
  186. colorize () {
  187.      # pass color and then reset
  188.      echo -e $@${txtReset}
  189. }
  190.  
  191. colorize_bold () {
  192.      echo -e ${txtBold}$@${txtReset}
  193. }
  194.  
  195. printusage_help () {
  196.      PID="openpdroid,3gdongle" # default example
  197.      brief_help
  198. }
  199.  
  200. brief_help () {
  201.      echo "    usage:     ./auto_patcher <ROM> <MODS> <ROMTYPE>"
  202.      echo "    example:   ./auto_patcher CM10.zip $PID cm"
  203.      echo ""
  204.      echo "Use ./auto_patcher -h for the help menu"
  205.      echo ""
  206.      cleanup
  207.      exit
  208. }
  209.  
  210. usage_help () {
  211.      PID=mods
  212.      mods_display
  213.      echo "Multiple MODS can be combined by separating them with a comma and NO SPACES!!!"
  214.      echo ""
  215.      echo "    usage:     ./auto_patcher <ROM> <MODS> <ROMTYPE>"
  216.      echo ""
  217.      echo "    example:   ./auto_patcher CM10.zip openpdroid,3gdongle cm"
  218.      echo ""
  219.      usage_more_help
  220. }
  221.  
  222. usage_more_help () {
  223.      echo "* press 'q' available patches or CTRL-c to quit the help menu *"
  224. }
  225.  
  226. display_help () {
  227.      cd patches
  228.      echo ""
  229.      echo "AVAILABLE PATCHES"
  230.      echo ""
  231.      echo "*******************************************************************"
  232.      echo "Note: There are ONLY 4 ROMTYPES: [pa, cm, aokp, and aosp]."
  233.      echo ""
  234.      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
  235.      echo ""
  236.      echo "!!! Do not put Android Version number on command line !!!!"
  237.      echo "*****************************************************************"
  238.      echo "Specific auto_patcher script options"
  239.      echo "    ./auto_patcher -h mods      Lists mods"
  240.      echo "                   -h <MODNAME> Specific <MOD> info"
  241.      echo "                   -h advanced  Advanced usage+lists all available patch dates"
  242.      echo ""
  243.      cleanup
  244.      exit
  245. }
  246.  
  247. romtype_help () {
  248.      # From rom_error (bad romtype) and date_error (patch date w/o specifying romtype)
  249.      echo ""
  250.      echo "!!!There are only four supported ROMTYPES: 1) cm  2) aokp  3) aosp  4) pa"
  251.      echo ""
  252.      echo "Find out which of the above ROMTYPES your rom is based on and try again!"
  253.      echo "**********************************************"
  254.      echo ""
  255.      cleanup
  256.      brief_help
  257. }
  258.  
  259. advanced_help () {
  260.      echo ""
  261.      echo "The Auto-Patcher Advanced Usage"
  262.      echo "*********************************"
  263.      echo ""
  264.      echo "For those of you determined to try everything..."
  265.      echo ""
  266.      echo "You can also try adding a patch date as a final argument."
  267.      echo ""
  268.      echo "      ./auto_patcher <ROM> <MOD> <ROMTYPE> <DATE>"
  269.      echo ""
  270.      echo "Please note: There are only four ROMTYPES: {pa, aokp, aosp and cm}"
  271.      echo "      -The various appellations after that are for internal use only!"
  272.      echo ""
  273.      echo "The Auto-Patcher has a pretty great rom probe, specifying the date is"
  274.      echo " deprecated...you don't need to, we promise."
  275.      echo ""
  276. }
  277.  
  278. cleanup () {
  279.      [[ -n $ROOT ]] && \rm -rf "$ROOT"/tmp*
  280. }
  281.  
  282. garbage () {
  283.     unset GARBAGE
  284.     GARBAGE=($(find $@ -name "*.orig")) && rm -rf ${GARBAGE[@]} && echo "GARBAGE= ${GARBAGE[@]}" >> "$LOG"
  285.     GARBAGE=($(find $@ -name "*.rej"))  && rm -rf ${GARBAGE[@]} && echo "GARBAGE= ${GARBAGE[@]}" >> "$LOG"
  286. }
  287.  
  288.  
  289. print_error () {
  290.      # General purpose error. Used for errors in main, as opposed to probe or set-up.
  291.      echo ""
  292.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  293.      printtask "!!! error: $@"
  294.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  295.      echo ""
  296.      brief_help
  297. }
  298.  
  299. smali_error () {
  300.      # Smali binary error. Problem often goes away on second run.
  301.      echo ""
  302.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  303.      printtask "!!! error: $@"
  304.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  305.      echo ""
  306.      if [[ $(grep 'method index is too large' "$LOG") != "" ]]; then
  307.           printtask "!!! Problem with method cap !!!"
  308.           printtask "... attempting fix ..."
  309.           . patches/.common/framework.jar.sh 2>&1 > ${JAR}.log
  310.           printtask "... rebuild framework.jar ..."
  311.           java -Xmx512M -jar smali.jar -a $API tmp/${FILE}.out/smali -o tmp/${FILE}.out/classes.dex >> "$LOG" 2>> "$LOG"
  312.           [ -f tmp/${FILE}.out/classes.dex ] || print_error "Could not resolve method cap issue- please submit log"
  313.      else
  314.           printtask "!!! This error is generally due to an unknown bug in the smali binary!"
  315.           echo "In our experience, it goes away if you run the patcher a second time."
  316.           echo " Please run the Auto-Patcher with the same command once again."
  317.           echo ""
  318.           echo "If after the third attempt the same thing happens,"
  319.           echo " please upload your logfile to our support thread!"
  320.           echo ""
  321.           cleanup
  322.           exit
  323.      fi
  324. }
  325.  
  326. unknown_mod_error () {
  327.      echo ""
  328.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  329.      printtask "!!! error: $@"
  330.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  331.      echo ""
  332.      cleanup
  333.      printusage_help
  334. }
  335.  
  336. mismatch_error () {
  337.      # Function that exposes attempts to apply a mod to an upsupported Android version
  338.      echo ""
  339.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  340.      printtask "!!! error: $@"
  341.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  342.      echo ""
  343.      cleanup
  344.      brief_help
  345. }
  346.  
  347. date_error () {
  348.      # Remind users to include a romtype if they are going to specify a patch date
  349.      echo ""
  350.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  351.      printtask "!!! error: $@"
  352.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  353.      echo ""
  354.      echo "Please put a ROMTYPE before the patch date and run again!!"
  355.      romtype_help
  356. }
  357.  
  358. rom_error () {
  359.      # For when an unsupported romtype is manually entered on the command-line
  360.      echo ""
  361.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  362.      printtask "!!! error: $@"
  363.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  364.      romtype_help
  365. }
  366.  
  367. conflict_error () {
  368.      # For conflicting packages
  369.      echo ""
  370.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  371.      printtask "!!! error: $@"
  372.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  373.      cleanup
  374.      PID=mods
  375.      mods_info
  376. }
  377.  
  378. misspatch_error () {
  379.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  380.      printtask "!! error: $@ "
  381.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  382.      echo ""
  383.      printtask "!! The Auto-Patcher could not find its patches and/or tools!"
  384.      echo ""
  385.      echo "* Did you run ./batch.sh first?"
  386.      echo ""
  387.      echo "The patch_patches.tgz and patch_tools.tgz need to be in this directory,"
  388.      echo "  one way or another..."
  389.      echo ""
  390.      cleanup
  391.      brief_help
  392. }
  393.  
  394. misstools_error () {
  395.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  396.      printtask "!! error: $@ "
  397.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  398.      printtask "Your set-up is missing a dependency."
  399.      echo "We require JDK, tar, patch and cpio."
  400.      echo ""
  401.      echo ""
  402.      echo "Each distro is different"
  403.      echo "but ensure you have them all installed and try again."
  404.      echo ""
  405.      cleanup
  406.      brief_help
  407. }
  408.  
  409. miss_git_error () {
  410.      printtask "Updater: disabled"
  411.      echo ""
  412.      printtask "You will not be able to get automatic updates because we could not find 'git'."
  413.      printtask "Find out how to install git for your system and that feature should work automatically"
  414.      echo ""
  415. }
  416.  
  417. printtask () {
  418.     echo "" >> "$LOG"
  419.     echo -e $@ | tee -a "$LOG"
  420. }
  421.  
  422. print_to_log () {
  423.      echo "$@" >> "$LOG"
  424. }
  425.  
  426. copy_prepatched_provisional () {
  427.      print_to_log "File for which patching failed:"
  428.      print_to_log "---------------------------------------------"
  429.      print_to_log ""
  430.      print_to_log "${FAIL##*/}"
  431.      print_to_log ""
  432.      print_to_log "---------------------------------------------"
  433.      print_to_log ""
  434.      print_to_log "We are adding a pre-patched copy of those files."
  435.      print_to_log ""
  436.      print_to_log "Please only file a bug report if running the program gives you problems!!!"
  437.      print_to_log "... replace $FAIL ..."
  438.      \rm -rf tmp/${FILE}.out/smali/${FAIL} tmp/${FILE}.out/smali/${FAIL}.orig tmp/${FILE}.out/smali/${FAIL}.rej
  439.      \cp -av  "$PROVISIONAL" tmp/${FILE}.out/smali/${FAIL} >> "$LOG" 2>&1
  440.      SID=(${SID[@]} $FAIL)
  441.      print_to_log "... failed patch sucessfully resolved ..."
  442. }
  443.  
  444. prep_alternate_patch () {
  445.      # Use alt.patch, if exists. Increment in case there are multiple alternates.
  446.      mv -v $ALTERNATIVE_PATCH $PARB/${NAME}.patch >> "$LOG"
  447.      ALT_PATCH_NUMBER=$((PATCH_NUMBER + 1))
  448.  
  449.      # Get fresh files/variables + decompile again (use rm b/c privacy files wouldn't get overwritten)
  450.      rm -rf tmp/${FILE}.out/smali
  451.      java -jar baksmali.jar -b -a $API -o tmp/${FILE}.out/smali ${FILE} >> "$LOG" 2>> "$LOG"
  452.      unset FAILS && unset SID
  453.      current_jar=$(($current_jar - 1))
  454.      break
  455. }
  456.  
  457. no_network_access () {
  458.      PING_CHECK "www.github.com" >> "$LOG" 2>&1 && NETWORK=true # second try and check github
  459.      PING_CHECK "www.google.com" >> "$LOG" 2>&1 # test for internet in general
  460.      if [[ "$NETWORK" == true ]]; then
  461.           return
  462.      else
  463.           printtask "Check for update failed, probably because of no internet access."
  464.           echo "The autopatcher will continue with current local version: $VERSION"
  465.           NETWORK=false
  466.           echo ""
  467.           sleep 2
  468.      fi
  469. }
  470.  
  471. auto_update_error () {
  472.      echo ""
  473.      printtask "### Update failed ###"
  474.      printtask ""
  475.      printtask "It appears you may have made some changes to the script that prevent automatic updating."
  476.      printtask "You could run 'git reset --hard' to return the autopatcher to its original form"
  477.      printtask " or set AUTO_UPDATE to false in the .config file to stop getting this message."
  478.      echo ""
  479.      printusage_help
  480. }
  481.  
  482. backup_script () {
  483.      printtask "... backing up auto_patcher to auto_patcher.bak ..."
  484.      cp -av auto_patcher auto_patcher.bak >> "$LOG" #backup for user in case of changes
  485.      echo ""
  486. }
  487.  
  488. warn_of_overwrite () {
  489.      # Only seen once, by users without ".git" directories
  490.      # Only users who got program @ download page see this, all others (ApG included) use git
  491.      echo "This is a one time set-up process that will overwrite all local changes!"
  492.      echo "If this is not ok with you press ctrl-C and set AUTO_UPDATE to false in .config"
  493.      echo ""
  494. }
  495.  
  496. establish_repo() {
  497.           printtask "### establishing the repo ###"
  498.           echo "... please be patient, this should only be needed once ..."
  499.           echo ""
  500.           git clone https://github.com/mateor/auto-patcher >> "$LOG" || no_network_access
  501.           if ($NETWORK); then
  502.                mv -v auto-patcher/".git" . >> "$LOG"
  503.                rm -rf auto-patcher
  504.                git checkout -q master || auto_update_error
  505.                git reset --hard 2>&1 >> "$LOG"
  506.                echo ""
  507.           fi
  508. }
  509.  
  510. export_log_and_variables () {
  511.      cp -a "$LOG" .updated.log
  512.      rm -f "$LOG"
  513.      AUTO_PATCHER_OLD_VERSION=$VERSION
  514.      AUTO_PATCHER_OLD_PATCHES_VERSION=$PATCHES_VERSION
  515.      AUTO_PATCHER_WAS_UPDATED=true
  516. }
  517.  
  518. print_changelog () {
  519.      CHANGELOG="changelog.txt"
  520.      echo "" > "$CHANGELOG"
  521.      CURRENT_HEAD=$(git rev-parse HEAD)
  522.      echo "Changelog from Autopatcher$AUTO_PATCHER_OLD_VERSION to Autopatcher$VERSION" >> "$CHANGELOG"
  523.      echo "---------------------------------------------------------------------------" >> "$CHANGELOG"
  524.      echo "" >> "$CHANGELOG"
  525.      echo "    Get more info about any individual commit by entering 'git show \$NUMBER'" >> "$CHANGELOG"
  526.      echo "" >> "$CHANGELOG"
  527.      git log --pretty=oneline $ORIGINAL_HEAD...$CURRENT_HEAD >> changelog.txt
  528.      printtask "... "$CHANGELOG" has been updated ..."
  529. }
  530.  
  531. replace_updater () {
  532.  
  533. # Function for devices that use SDcard installs. Also to be tested for CM7
  534. #  since CM7 seems to use an updater-script that wipes EMMC. Testing
  535.  
  536.      \rm $UPDATER_LOC/$UPDS 2>&1
  537.      \cp patches/.common/$UPDATER_LOC/$UPDS $UPDATER_LOC >> "$LOG" 2>&1
  538.      echo "run_program(\"/sbin/busybox\", \"umount\", \"/system\");" >> UPDATE.txt
  539.      cat UPDATE.txt >> $UPDATER_LOC/$UPDS
  540.      echo ""
  541.      echo "New updater-script being used:" >> "$LOG"
  542.      echo "-----------------------------" >> "$LOG"
  543.      cat $UPDATER_LOC/$UPDS >> "$LOG"
  544. }
  545.  
  546. abspath () {
  547.      case $(uname -s) in
  548.      CYGWIN*)
  549.           echo $(cygpath -ua "$1") | sed 's:/$::g'
  550.           ;;
  551.      Darwin)
  552.           #[[ $(echo $1 | awk '/^\//') == $1 ]] && echo "$1" || echo "$PWD/$1"
  553.           [[ ${1:0:1} == "/" ]] && echo "$1" || echo "$PWD/$1"
  554.           ;;
  555.      Linux)
  556.           echo $(readlink -f "$1")
  557.           ;;
  558.      *)
  559.           if [[ ${1:0:1} == "/" ]]; then
  560.                echo "$1"
  561.           elif [[ ${1:0:2} == "./" ]]; then
  562.                echo "$PWD/${1:2}"
  563.           else
  564.                echo "$PWD/$1"
  565.           fi
  566.           ;;
  567.      esac
  568. }
  569.  
  570. mods_display () {
  571.      echo ""
  572.      echo "The Auto-Patcher $VERSION"
  573.      echo "*******************************"
  574.      echo ""
  575.      echo "The available mods are"
  576.      echo "****************************************************"
  577.      echo "*  pdroid   openpdroid  voice   v6supercharger     *"
  578.      echo "*  voice    insecure    secure  external_internal  *"
  579.      echo "*  tabletUI 3gdongle    pd2.0                      *"
  580.      echo "****************************************************"
  581.      echo ""
  582.      echo "To find out more about any mod, simply enter:"
  583.      echo "     ./auto_patcher -h <MODNAME>"
  584.      echo ""
  585.      cleanup
  586. }
  587.  
  588. mods_info () {
  589.      # Defaults
  590.      DEPENDENCY="None"
  591.      DEV="Unknown"
  592.      CEILING=$CURRENT_ANDROID
  593.      ROM_CONFLICTS=(None)
  594.      UNKNOWN_MOD=false
  595.      case $PID in
  596.      mods)
  597.           mods_display
  598.           exit
  599.           ;;
  600.      auto*)
  601.           echo ""
  602.           echo "**************************************"
  603.           echo "The Auto-Patcher ate a kitten once..."
  604.           echo "**************************************"
  605.           echo ""
  606.           DEV="mateor and Caldair"
  607.           WEBSITE="https://github.com/mateor/auto-patcher"
  608.           printusage_help
  609.           ;;
  610.      advanced)
  611.           cd patches/
  612.           (advanced_help && find -H * -name 201*) | more -d
  613.           echo ""
  614.           brief_help
  615.           ;;
  616.      voice)
  617.           FLOOR=4.0.0
  618.           DESC="Use on wifi-only tablets to have the software register as 'voice-enabled'. Allows VOIP like Google Voice to function."
  619.           DESC="bongostl's setup method can be seen at the link."
  620.           WEBSITE="http://forum.xda-developers.com/showthread.php?t=1823701"
  621.           DEV="bongostl"
  622.           ;;
  623.      v6 | v6supercharger)
  624.           FLOOR=4.0.0
  625.           CEILING=4.2.2
  626.           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."
  627.           DEV="Zeppelinrox"
  628.           DEPENDENCY="The V6 Supercharger Script. Get it at the link"
  629.           WEBSITE="http://forum.xda-developers.com/showthread.php?t=991276"
  630.  
  631.           PID="v6supercharger" # allows shorthand 'v6' to find patches.
  632.           ;;
  633.      pdroid)
  634.           FLOOR=2.3.7
  635.           CEILING=4.1.2
  636.           DESC="Security Software that manages access to personal data w/o causing FC's. No longer under development."
  637.           DESC1="Try the open-source expansion of this program, OpenPdroid."
  638.           DESC2="(It's the 'openpdroid' mod within the Auto-Patcher)."
  639.           DEV="Svyat"
  640.           DEPENDENCY="The Pdroid app. You can get it at the link"
  641.           WEBSITE="http://forum.xda-developers.com/showthread.php?t=1923576"
  642.  
  643.           ;;
  644.      pd2.0)
  645.           FLOOR=4.1.2
  646.           CEILING=4.1.2
  647.           DESC="An expansion of the PDroid application/framework."
  648.           DEV="CollegeDev"
  649.           DEPENDENCY="The free PDroid2.0 app available at the link"
  650.           WEBSITE="http://forum.xda-developers.com/showthread.php?t=1923576"
  651.           ;;
  652.      openpdroid)
  653.           FLOOR=4.1.2
  654.           CEILING=4.2.2
  655.           DESC="A fork of the open-source PDroid and Pdroid2.0 framework."
  656.           DEV="CollegeDev/FFU5y/wbedard/mateorod/Community"
  657.           DEPENDENCY="PDroidManager(opensource) or PDroid2.0(CollegeDev)"
  658.           WEBSITE="http://forum.xda-developers.com/showthread.php?t=1994860"
  659.           ;;
  660.      opendroid)
  661.           echo ""
  662.           echo "No mod named 'opendroid'. You probably mean 'openpdroid'. Try again."
  663.           PID="mods"
  664.           mods_info
  665.           ;;
  666.      external_internal)
  667.           FLOOR=2.3.3
  668.           CEILING=4.1.2
  669.           DESC="Swap internal and external storage."
  670.           ;;
  671.      insecure)
  672.           FLOOR=2.3.3
  673.           DESC="Make any boot.img insecure and allow remount as well as adb push/pull of system files. True root."
  674.           ;;
  675.      secure)
  676.           FLOOR=2.3.3
  677.           DESC="Make any boot.img secure."
  678.           ;;
  679.      3gdongle)
  680.           FLOOR=4.1.0
  681.           CEILING=4.2.2
  682.           DESC="Allows 3gdongles to work on wifi tablets w/o 3rd party applications."
  683.           DEV="trevd"
  684.           DEPENDENCY="None, but read the troubleshooting guide at the link"
  685.           WEBSITE="http://forum.xda-developers.com/showthread.php?t=1798631"
  686.           ;;
  687.      tabletUI)
  688.           FLOOR=4.1.0
  689.           CEILING=4.2.2
  690.           if [[ $ANDR == "4.2" ]]; then
  691.                ROM_CONFLICTS=(.aosp-ever aokp pa)
  692.           else
  693.                ROM_CONFLICTS=(pa .aosp-slim)
  694.           fi
  695.           DESC="Changes the user interface of Phones and Phablets to the popular Tablet UI."
  696.           DEV="barmullio/Caldair"
  697.           WEBSITE="http://forum.xda-developers.com/showthread.php?t=2154075"
  698.           ;;
  699.      *)
  700.           UNKNOWN_MOD=true
  701.           FLOOR=?????
  702.           CEILING=?????
  703.           DESC="??? Unknown modtype ??? Check your spelling or the mod list!"
  704.           DESC1="    Try: \"./auto_patcher -h mods\""
  705.      esac
  706.  
  707.      # strip decimals from android version (i.e. 2.3.7 becomes 237) so we can do math
  708.      NVID=${VID//[.]/}
  709.      COMPATF=${FLOOR//[.]/}
  710.      COMPATC=${CEILING//[.]/}
  711. }
  712.  
  713. apk_copy () {
  714.      if [ -f patches/$P/$A/$R/$B/$FILE ]; then
  715.           mkdir apk_staging
  716.           cd apk_staging
  717.           # Move the certs and manifests from the ROM's apk into the prepatched copy
  718.           jar -xvf ../"$FILE" META-INF AndroidManifest.xml >> "$LOG"
  719.           jar -ufv ../patches/$P/$A/$R/$B/$FILE META-INF/CERT* AndroidManifest.xml >> "$LOG" 2>> "$LOG"
  720.           cd ..
  721.           \cp -av patches/$P/$A/$R/$B/$FILE $FILE >> "$LOG" 2>&1
  722.           echo "... Patching $APK failed, but we have successfuly copied over a prepatched file ..."
  723.           apk_was_copied=true
  724.      else
  725.           print_error "failed patching $APK!!!"
  726.      fi
  727. }
  728.  
  729. no_mod_selected_error () {
  730.      echo ""
  731.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  732.      printtask "!!! error: $@"
  733.      printtask "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  734.      echo ""
  735.      printtask "You need to enter a MOD before you try and specify the ROMTYPE."
  736.      printtask "Check the help menu for the list of available MODS"
  737.      echo ""
  738.      printusage_help
  739. }
  740.  
  741. check_against_rom_types () {
  742.      # this checks to see if the user has entered a ROMTYPE in the MOD spot and outputs advice if so.
  743.      NUMBER_OF_AVAILABLE_ROMTYPES=${#AVAILABLE_ROMTYPES[@]}
  744.      for ((i=0; i < $NUMBER_OF_AVAILABLE_ROMTYPES; i++)); do
  745.           if [[ ${AVAILABLE_ROMTYPES[i]} == "$P" ]]; then
  746.                no_mod_selected_error "\"$P\" is a ROMTYPE not a MOD"
  747.           fi
  748.      done
  749. }
  750.  
  751. compat_check () {
  752.      for G in ${PID[@]}; do
  753.           mods_info
  754.           if ($UNKNOWN_MOD); then
  755.                check_against_rom_types
  756.                unknown_mod_error "\"$G\" is not a valid mod. Check your spelling or the mod list!!!"
  757.           elif [[ "$NVID" -lt "$COMPATF" ]]; then
  758.                mismatch_error "Your Android $VID rom is too old! The minimum for $G is \n !!! Android $FLOOR!"
  759.           elif [[ "$NVID" -gt "$COMPATC" ]]; then
  760.                mismatch_error "Sorry, $G has not been made available for Android $VID yet!"
  761.           fi
  762.      done
  763. }
  764.  
  765. rom_compat_check () {
  766.      N=${#ROM_CONFLICTS[@]}
  767.           for ((i=0; i < $N; i++)); do
  768.                if [[ $RID == ${ROM_CONFLICTS[$i]} ]]; then
  769.                     rom_compat_error
  770.                fi
  771.           done
  772. }
  773.  
  774. rom_compat_error () {
  775.      print_error "$RID is incompatible with $PID!!!"
  776. }
  777.  
  778. extpath () {
  779.      case $(uname -s) in
  780.      CYGWIN*)
  781.           echo $(cygpath -da "$1")
  782.           ;;
  783.      *)
  784.           echo $(abspath "$1")
  785.           ;;
  786.      esac
  787. }
  788.  
  789. #contains () { for e in "${@:2}"; do [[ "$e" = "$1" ]] && return 0; done; return 1; }
  790.  
  791. parse () { echo "${1%%_*}"; }
  792.  
  793. getbuildprop () {
  794.      if [ -f system/build.prop ]; then
  795.           result=($(grep $1 system/build.prop | tr -d '\r' | tr '=' ' '))
  796.           echo "${result[1]}"
  797.      else
  798.           echo ""
  799.      fi
  800. }
  801.  
  802.  
  803. aosp_mod_check() {
  804.      jar -xvf "$ROMX" system/framework/framework2.jar >> "$LOG"
  805.      if [ -s system/framework/framework2.jar ]; then
  806.           ever_check
  807.      else
  808.           AOSP=aosp
  809.      fi
  810.      echo $AOSP
  811. }
  812.  
  813. ever_check() {
  814.      MID=$(getbuildprop ro.build.romversion)
  815.      if [[ $MID == "Evervolv"* ]]; then
  816.           AOSP=".aosp-ever"
  817.      else
  818.           MID=$(getbuildprop ro.build.display.id)
  819.           if [[ "$MID" == "ev"* ]]; then
  820.                AOSP=".aosp-ever"
  821.           else
  822.                pac_check
  823.           fi
  824.      fi
  825.      echo $AOSP
  826. }
  827.  
  828. pac_check() {
  829.      MID=$(getbuildprop ro.build.romversion)
  830.      if [[ "$MID" == "pac"* ]] || [[ "$MID" == "PAC"* ]]; then
  831.           AOSP=".aosp-pac"
  832.      else
  833.           MID=$(getbuildprop ro.build.display.id)
  834.      if [[ "$MID" == "pac"* ]] || [[ "$MID" == "PAC"* ]]; then
  835.          AOSP=".aosp-pac"
  836.           else
  837.                slim_check
  838.           fi
  839.      fi
  840. }
  841.  
  842. slim_check() {
  843.      SLMID=$(getbuildprop ro.modversion)
  844.      SLMMID=$(getbuildprop updateme.name)
  845.      if [[ "$SLMMID" == "Slim"* ]] || [[ $SLMMID == "slim"* ]]; then
  846.           AOSP=".aosp-slim"
  847.      elif [[ "$SLMID" == "Slim"* ]] || [[ $SLMID == "slim"* ]]; then
  848.           AOSP=".aosp-slim"
  849.      else
  850.           if [[ "$ANDR" == "4.1" ]]; then
  851.                AOSP=.aosp-mod
  852.           else
  853.                AOSP="aosp"
  854.           fi
  855.      fi
  856. }
  857.  
  858. pa_check() {
  859.      CMID=$(getbuildprop ro.modversion)
  860.      PAID=$(getbuildprop ro.pa.version)
  861.      MID=$(getbuildprop ro.build.display.id)
  862.      [[ "$MID" == "" ]] && MID=$(parse $(getbuildprop ro.build.display.id))
  863.      DID=$(getbuildprop ro.product.device)
  864.      [[ "$DID" == "" ]] && DID=$(getbuildprop ro.build.product)
  865.           if [[ -n $PAID ]]; then
  866.                RID=pa
  867.           else
  868.                case $CMID in
  869.                [Pp][Aa]*)
  870.                     RID=pa;
  871.                     ;;
  872.                *)
  873.                     RID=cm;
  874.                     ;;
  875.                esac
  876.           fi
  877. }
  878.  
  879. romtype_set () {
  880.      case $RID in
  881.      cm)
  882.           ROMTYPE="cm"
  883.           ;;
  884.      aokp*)
  885.           ROMTYPE="aokp"
  886.           ;;
  887.      pa)
  888.           ROMTYPE="pa"
  889.           ;;
  890.      *)
  891.           ROMTYPE="aosp"
  892.           ;;
  893.      esac
  894. }
  895.  
  896. proberom () {
  897.      VID=$(getbuildprop ro.build.version.release)
  898.      if [ ${#VID} -lt 4 ]; then # in case of non-standard versioning in build.prop (defy!)
  899.           ANDR=$VID
  900.      else
  901.           ANDR="${VID%.*}" # Cut to 1 significant digit to cover specific range or release.
  902.      fi
  903.      if [ "$ANDR" == "2.3" ]; then
  904.           FLAVR=gingerbread
  905.           API=10
  906.           RID=cm;
  907.      elif [ "$ANDR" == "4.0" ]; then
  908.           FLAVR=ics
  909.           API=15
  910.           MID=$(getbuildprop ro.rommanager.developerid)
  911.           [[ "$MID" == "" ]] && MID=$(parse $(getbuildprop ro.modversion))
  912.           DID=$(getbuildprop ro.product.device)
  913.           [[ "$DID" == "" ]] && DID=$(getbuildprop ro.build.product)
  914.  
  915.           case "$MID" in
  916.           pa|cm9|cm|cyanogenmod*)
  917.                RID=cm;
  918.                ;;
  919.           aokp|teamkang)
  920.                RID=aokp;
  921.                ;;
  922.           aosp*)
  923.                RID=aosp;
  924.                ;;
  925.           *)
  926.                echo "!!! $MID is not supported yet, trying aosp !!!"
  927.                RID=aosp
  928.                ;;
  929.           esac
  930.      elif [ "$ANDR" == "4.1" ]; then
  931.           FLAVR=jellybean
  932.           API=16
  933.           CMID=$(getbuildprop ro.cm.version)
  934.                if [[ -n $CMID ]]; then
  935.                     pa_check
  936.                else
  937.                     MID=$(getbuildprop ro.build.display.id)
  938.                     [[ "$MID" == "" ]] && MID=$(parse $(getbuildprop ro.build.display.id))
  939.                     DID=$(getbuildprop ro.product.device)
  940.                     [[ "$DID" == "" ]] && DID=$(getbuildprop ro.build.product)
  941.                     case "$MID" in
  942.                     cm10|cm*|cyanogenmod*)
  943.                          RID=cm;
  944.                          ;;
  945.                     aokp*|teamkang)
  946.                          RID=aokp;
  947.                          ;;
  948.                     pa*)
  949.                          RID=pa;
  950.                          ;;
  951.                     ev*)
  952.                          RID=".aosp-ever";
  953.                          ;;
  954.                     *)
  955.                          RID=$(aosp_mod_check);
  956.                          ;;
  957.                     esac
  958.                fi
  959.      elif [ "$ANDR" == "4.2" ]; then
  960.           FLAVR=jellybean
  961.           API=17
  962.           CMID=$(getbuildprop ro.cm.version)
  963.           MID=$(getbuildprop ro.build.display.id)
  964.           [[ "$MID" == "" ]] && MID=$(parse $(getbuildprop ro.build.display.id))
  965.           DID=$(getbuildprop ro.product.device)
  966.           [[ "$DID" == "" ]] && DID=$(getbuildprop ro.build.product)
  967.  
  968.                case "$MID" in
  969.                cm*|cyanogenmod*)
  970.                     RID=cm;
  971.                     # hack to account for Final builds with old code and current build dates. Huge hack, but if there
  972.                     # is one thing I don't believe in, it's "educating the user-base". Not happening. So hack it is.
  973.                     if [[ "$CMID" == "10.1.2"* ]] || [[ "$CMID" == "10.1.3"* ]]; then
  974.                          echo " ### CMID: $CMID: Selecting CM Final Build patches" >> "$LOG"
  975.                          BID_OVERRIDE=20130516
  976.                     fi
  977.                     ;;
  978.                aokp*|teamkang)
  979.                     RID=aokp;
  980.                     ;;
  981.                [Pp][Aa]*)
  982.                     if [[ "$MID" == "pac"* ]] || [[ "$MID" == "PAC"* ]]; then
  983.                          RID=".aosp-pac"
  984.                     else
  985.                          RID=pa;
  986.                     fi
  987.                     ;;
  988.                ev*|Ever*)
  989.                     RID=".aosp-ever";
  990.                     ;;
  991.                *)
  992.                     RID="$(ever_check)";
  993.                     ;;
  994.                esac
  995.      else
  996.           print_error "unknown ROM version $VID: generally means cannot find/read build.prop"
  997.      fi
  998.      # romtype_set allows hiding ".aosp-*" types from users while leaving them as options on CLI
  999.      romtype_set
  1000.      echo "... result of ROM check: version= $VID rom= $ROMTYPE ..."
  1001. }
  1002.  
  1003.  
  1004. # Advanced build date query
  1005. # This should set the patch date automatically.
  1006.  
  1007. build_query() {
  1008. UTC=$(getbuildprop ro.build.date.utc)
  1009.  
  1010. # Difference in how OS X and Linux/Cygwin handles 'date' utility
  1011. case $(uname -s) in
  1012. [Dd]arwin)
  1013.      BDID=$(date -r $UTC +%Y%m%d)
  1014.      echo "#### Darwin Build Query used ####" >> "$LOG"
  1015.      ;;
  1016. *)
  1017.      BDID=$(date -d "1970-01-01 UTC $UTC seconds" +"%Y%m%d")
  1018.      echo "#### Standard Build Query ####" >> "$LOG"
  1019.      ;;
  1020. esac
  1021.  
  1022. # If patch date is specified on CLI, use that patch date
  1023. if [ $# -gt 3 ]; then
  1024.      BID=${4//,/ };
  1025.      BDID=$BID
  1026. else
  1027.      case $ANDR in
  1028.      2.3)
  1029.           # CM7 roms do not have UTC time stamp- so no build query.
  1030.           BID=latest;
  1031.           echo "*** CM7 roms default to latest patches. If using an older rom ***"
  1032.           echo "*** specify patch date on command line. Use -h to see available patches ***"
  1033.           ;;
  1034.      *)
  1035.           for P in ${PID[@]}; do
  1036.                [ -d patches/$P/$ANDR ] && A=$ANDR || A=all
  1037.                [ -d patches/$P/$A/"$RID" ] && R=$RID || R=generic
  1038.                [ -d patches/$P/$A/$R/$BID ] && B=$BID || B=latest
  1039.                PAR=patches/$P/$A/$R
  1040.                PARB=patches/$P/$A/$R/$B
  1041.                DATES=$(ls patches/$P/$A/"$R" | grep '201*')
  1042.                # Intialize variable - an upperbound to determine lowest in first loop.
  1043.                BID=20201231
  1044.                     # find earliest available patch date
  1045.                     for i in $DATES; do
  1046.                          if [[ $i -lt $BID ]]; then
  1047.                               BID=$i
  1048.                          fi
  1049.                     done
  1050.                # Check if earliest patches are from before build date. Else use earliest
  1051.  
  1052.                     # If rom is newer than earliest patch set, find best match
  1053.                     if [[ $(($BID-$BDID)) -lt 0 ]]; then
  1054.                          for g in $DATES; do
  1055.                               # use any patch that matches $UTC
  1056.                               if [[ $(($BDID-$g)) -eq 0 ]]; then
  1057.                                    BID=$g
  1058.                                    break
  1059.                               # Set highest $BID w/o going over $UTC
  1060.                               elif [[ $(($BDID-$g)) -gt 0 ]] && [[ $g -gt $BID ]]; then
  1061.                                    BID=$g
  1062.                               fi
  1063.                          done
  1064.                     fi
  1065.                     # Error catch if query fails.
  1066.                     [[ $BID == "20201231" ]] && BID=latest && echo "Build Query Failed!! Default to latest!" >> "$LOG"
  1067.                     if [[ -n "$BID_OVERRIDE" ]]; then
  1068.                          BID="$BID_OVERRIDE"
  1069.                     fi
  1070.                     echo "... $PID patch date has been set to: $BID ..."
  1071.                     B=$BID
  1072.                     break
  1073.           done
  1074.           ;;
  1075.      esac
  1076. fi
  1077. }
  1078.  
  1079. proberil () {
  1080.      RIL=$(getbuildprop ro.telephony.ril_class)
  1081.      [[ -n $RIL ]] && printtask "... result of ROM check: RIL= $RIL"
  1082. }
  1083.  
  1084. rominfo () {
  1085. echo "# VER= $VERSION" >> "$LOG"
  1086. echo "# OS=  $OSTYPE ${BIT}bit" >> "$LOG"
  1087. echo "# VID= $VID" >> "$LOG"
  1088. echo "# PID= ${PID[@]}" >> "$LOG"
  1089. echo "# MID= $MID" >> "$LOG"
  1090. echo "# RID= $RID" >> "$LOG"
  1091. echo "# ANDR= $ANDR" >> "$LOG"
  1092. echo "# BDID= $BDID" >> "$LOG"
  1093. echo "# DID= $DID" >> "$LOG"
  1094. echo "# BID= $BID" >> "$LOG"
  1095. echo "Patches are $PID/$ANDR/$RID/$BID" >> "$LOG"
  1096.  
  1097. if [ -d ../".git" ]; then
  1098.      echo "" >> "$LOG"
  1099.      echo -n "Current HEAD: " >> "$LOG"
  1100.      git log --pretty=oneline HEAD^..HEAD >> "$LOG"
  1101.      echo "" >> "$LOG"
  1102. fi
  1103.  
  1104. }
  1105.  
  1106. if [[ $1 == -h ]]; then
  1107.      if [ $# -gt 1 ]; then
  1108.           PID=$2
  1109.           mods_info
  1110.           echo ""
  1111.           echo "The Auto-Patcher $VERSION by Mateorod and Pastime1971"
  1112.           echo "*************************************************"
  1113.           echo ""
  1114.           echo "Mod Name:    $PID"
  1115.           echo ""
  1116.           echo "    $DESC"
  1117.           if [[ $DESC1 ]]; then # test to see is there is a second DESC line
  1118.                echo "    $DESC1"
  1119.           fi
  1120.           if [[ $DESC2 ]]; then
  1121.                echo -n "$DESC2"
  1122.           fi
  1123.           echo ""
  1124.           echo "Developer: $DEV"
  1125.           echo "Dependancies: $DEPENDENCY"
  1126.           if [[ $WEBSITE ]]; then
  1127.                echo "Website: $WEBSITE"
  1128.           fi
  1129.           echo ""
  1130.           echo "Works on ROMs from Android $FLOOR -> $CEILING"
  1131.           echo
  1132.           brief_help
  1133.           exit
  1134.      fi
  1135. fi
  1136.  
  1137. [[ $# -lt 1 || -z $1 ]] && PID="mods" && mods_info
  1138. [[ $1 == "-h" ]] && usage_help | less && display_help
  1139.  
  1140. echo ""
  1141. echo "The Auto-Patcher $VERSION"
  1142. # by pastime1971 and mateorod
  1143. if [[ -n "$AUTO_PATCHER_WAS_UPDATED" ]]; then
  1144.      echo "     (auto-updated from $AUTO_PATCHER_OLD_VERSION)"
  1145.      print_changelog
  1146. fi
  1147. echo ""
  1148.  
  1149. ROOT="$(abspath "$(dirname "$(abspath "$(type -p "$0")")")")"
  1150. TOOL="$ROOT/patch_tools.tgz"
  1151. PTCH="$ROOT/patch_patches.tgz"
  1152. cleanup
  1153. # set temporary directory
  1154. DATE=$(date +%Y%m%d%H%M%S)
  1155. TEMP="$ROOT/tmp$DATE"
  1156. TMP="$ROOT/tmp$DATE"
  1157. TMPDIR="$ROOT/tmp$DATE"
  1158. mkdir "$TEMP"
  1159. ROM=$(abspath "$1")
  1160. ROMX=$(extpath "$1")
  1161. PATCH_BINARY=patch
  1162. PINGCOUNT="-c1"
  1163.  
  1164. if [[ $OSTYPE == "cygwin" ]]; then
  1165.     EXE=".exe"
  1166.     PGFORM="0x"
  1167.     PINGCOUNT="-n 1"
  1168.     PATCH_BINARY="./patch$EXE "
  1169.     AUTO_UPDATE=false # CYGWIN AUTO_UPDATE TESTERS COMMENT THIS LINE HERE!
  1170. fi
  1171.  
  1172. PING_CHECK() {
  1173.     case $(uname -s) in
  1174.     [Dd]arwin*)
  1175.          (ping "$@" "$PINGCOUNT")
  1176.          ;;
  1177.     *)
  1178.          (ping "$PINGCOUNT" "$@")
  1179.          ;;
  1180.     esac
  1181. }
  1182.  
  1183.  
  1184. LOG=$(abspath ./log$DATE.txt)
  1185. #echo $ROOT $TOOL $ROM $TEMP "$LOG"
  1186. echo "ROOT= $ROOT" >> "$LOG"
  1187. echo "TOOL= $TOOL" >> "$LOG"
  1188. echo "ROM=  $ROM" >> "$LOG"
  1189. echo "ROMX= $ROMX" >> "$LOG"
  1190.  
  1191.  
  1192. # Auto-Update                           # excluding ApG/cygwin for now until I talk with kobik.
  1193. # To turn off auto updating, set AUTO_UPDATE in .config to false
  1194.  
  1195.  
  1196. # Check for git
  1197. if [ $(which git) ]; then
  1198.     GIT=true
  1199. else
  1200.     miss_git_error "Missing \"git\" utility"
  1201.     GIT=false
  1202. fi
  1203.  
  1204. # If program was updated, merge the logs
  1205. if [[ -n "$AUTO_PATCHER_WAS_UPDATED" ]]; then
  1206.     cat .updated.log >> "$LOG"
  1207.     rm -f .updated.log
  1208. fi
  1209.  
  1210. if [[ "$AUTO_UPDATE" == true ]] && [[ "$GIT" == true ]]; then
  1211.     PING_CHECK "www.github.com" > /dev/null 2>&1 && NETWORK=true || no_network_access
  1212.     if [[ "$NETWORK" == true ]]; then
  1213.          printtask "### checking for updates ###"
  1214.  
  1215.           # establish the repo if not already present- one time operation.
  1216.           if [ ! -d ".git" ]; then
  1217.                printtask "... initial setup to allow automatic updating ..."
  1218.                backup_script && warn_of_overwrite
  1219.                establish_repo && export_log_and_variables
  1220.                printtask "### rerunning script with latest version ###"
  1221.                $0 "$@"
  1222.                exit 0
  1223.           fi
  1224.  
  1225.           # Here is the regular update process
  1226.           git fetch origin 2>&1 >> "$LOG" || no_network_access
  1227.           if !("$NETWORK"); then
  1228.                printtask "Updater: not able to run auto_update."
  1229.           else
  1230.                COMMITS_BEHIND=$(git rev-list HEAD..origin/master --count)
  1231.                case "$COMMITS_BEHIND" in
  1232.                0)
  1233.                     printtask "Updater: Auto-Patcher is already up to date"
  1234.                     ;;
  1235.                *)
  1236.                     ORIGINAL_HEAD=$(git rev-parse HEAD)
  1237.                     printtask "... Updater: update found ..."
  1238.                     backup_script
  1239.                     printtask "... Updating Auto-Patcher ..."
  1240.                     git merge --no-edit origin/master 2>&1 >> "$LOG" || auto_update_error
  1241.                     export_log_and_variables
  1242.                     printtask "### updated, now rerunning script with latest version ###"
  1243.                     $0 "$@"
  1244.                     exit 0
  1245.                     ;;
  1246.                esac
  1247.                echo ""
  1248.           fi
  1249.      fi
  1250. fi
  1251.  
  1252. if [[ -n "$AUTO_PATCHER_WAS_UPDATED" ]]; then
  1253.      if [[ $PATCHES_VERSION -gt $AUTO_PATCHER_OLD_PATCHES_VERSION ]]; then
  1254.           echo "... integrating new patches ..."
  1255.           sh batch.sh >> /dev/null 2>> "$LOG"
  1256.      fi
  1257. fi
  1258.  
  1259. #remove old zips, if built under identical parameters.
  1260. \rm -f $RESTORE.zip $UPDATE.zip
  1261.  
  1262. CHK=$(getconf LONG_BIT)
  1263. if [[ $CHK = "64" ]]; then
  1264.      BIT=64
  1265. else [[ $BIT = "32" ]];
  1266.      BIT=32
  1267. fi
  1268.  
  1269. printtask "### verify tools ###"
  1270. which patch >> "$LOG" || misstools_error "Missing \"patch\" utility."
  1271. which tar >> "$LOG" || misstools_error "Missing \"tar\" utility"
  1272. which jar >> "$LOG" || misstools_error "Missing \"Java Development Kit\" (JDK)"
  1273. which java >> "$LOG" || misstools_error "Missing \"Java Development Kit\" (JDK)"
  1274. which cpio >> "$LOG" || misstools_error "Missing \"cpio\" utility"
  1275. [ -f "$TOOL" ] || misspatch_error "patch_tool.tgz is missing"
  1276. [ -f "$PTCH" ] || misspatch_error "patch_patches.tgz is missing"
  1277. printtask "### verify ROM ###"
  1278. [ -f "$ROM" ] || print_error "Cannot find $1!! Make sure you have the location and name right"
  1279. echo "### running $0 $@" >> "$LOG"
  1280. cd "$TEMP"
  1281.  
  1282. PATH="$TEMP:$PATH"
  1283. export PATH
  1284. echo "PATH= $PATH" >> "$LOG"
  1285.  
  1286. printtask "### unzip patch tools ###"
  1287. tar xzf "$TOOL" > /dev/null || print_error "failed extracting tools !!!"
  1288.  
  1289. # Get OS specific tools
  1290.      case $(uname -s) in
  1291.      CYGWIN*)
  1292.           \mv cygwin/aapt.exe .
  1293.           \mv cygwin/patch.exe .
  1294.           \mv cygwin/*.exe boot/
  1295.           \rm -rf cygwin # why? If you want to we can keep/add it to mac/linux, though.
  1296.           ;;
  1297.      [Dd]arwin)
  1298.           \mv mac/aapt .
  1299.           ;;
  1300.      [Ll]inux)
  1301.           \mv linux/aapt .
  1302.           ;;
  1303.      *)
  1304.           \mv linux/aapt .
  1305.           ;;
  1306.      esac
  1307. # Ensure aapt permissions - possibly to one day be expanded to all tools in a function.
  1308. chmod a+x aapt$EXE
  1309.  
  1310. printtask "### unzip patch patches ###"
  1311. tar xzf "$PTCH" > /dev/null || print_error "failed extracting patches !!!"
  1312.  
  1313. # we need build.prop for both RIL probing and ROM probing
  1314. jar -xvf "$ROMX" system/build.prop 2>> "$LOG" || print_error "failed extracting build.prop from $ROMX !!!"
  1315.  
  1316. # rom + ril type
  1317. proberil
  1318. printtask "### probe rom type ###"
  1319. proberom
  1320.  
  1321. # rom type
  1322. if [ $# -gt 1 ]; then
  1323.      PIT=$2
  1324.      if [[ "$PIT" = "-h" ]]; then
  1325.           display_help
  1326.      fi
  1327. fi
  1328.  
  1329. if [ $# -gt 2 ]; then
  1330.      RID=$3        
  1331.      case "$RID" in
  1332.      cm*|cyanogenmod*)
  1333.           RID=cm;
  1334.           ;;
  1335.      aokp*|teamkang)
  1336.           RID=aokp;
  1337.           ;;
  1338.      pa*)
  1339.           RID=pa;
  1340.           ;;
  1341.     .aosp-ever)
  1342.           RID=".aosp-ever";
  1343.           ;;
  1344.     .aosp-slim)
  1345.           RID=".aosp-slim"
  1346.           ;;
  1347.     .aosp-pac)
  1348.           RID=".aosp-pac"
  1349.           ;;
  1350.      aosp*)
  1351.           case "ANDR" in
  1352.           4.1)
  1353.                RID=$(aosp_mod_check);
  1354.                ;;
  1355.           *)
  1356.                RID=$(ever_check)
  1357.                ;;
  1358.           esac
  1359.           ;;
  1360.      201*)
  1361.           date_error "You must specify ROMTYPE if designating a patch date!!"
  1362.           ;;
  1363.      *)
  1364.           rom_error "$RID is not a valid ROMTYPE!!"
  1365.           ;;
  1366.      esac
  1367.      romtype_set
  1368.      echo "... Using $ROMTYPE patches ..."
  1369. else
  1370.      echo "... Using results of ROM check ..."
  1371. fi
  1372.  
  1373. # patch type
  1374. if [ $# -gt 1 ]; then
  1375.      PIL=${2//,/ } # If more than one mod, make string separated by spaces
  1376. else
  1377.      PIL="openpdroid"
  1378.      echo "No mods selected. Defaulting to openpdroid, since that's what you probably want..."
  1379. fi
  1380.  
  1381. # Ensure only 1 Pdroid mod at a time; PDroid first, so provisionals do not overwrite other mods...
  1382.  
  1383. PID=(${PIL// / })                      # turn string of requested mods into array
  1384. COPYPID=("${PID[@]}")                  # working copy to iterate over
  1385. MODS_NUMBER=${#COPYPID[@]}             # number of elements (mods)
  1386.  
  1387. if [[ $MODS_NUMBER > 1 ]]; then        # because if only one mod, not needed
  1388. INDEX=0
  1389.      for p in ${COPYPID[@]}; do
  1390.           # this first loop detects presence of any pdroid mod and sets it to be first applied (SPID[0])
  1391.           if [ "$p" = "pdroid" ] || [ "$p" = "pd2.0" ] ||[ "$p" = "openpdroid" ]; then
  1392.                SPID[0]=$p              #set pdroid first
  1393.                unset COPYPID["$INDEX"]
  1394.                break
  1395.           else
  1396.                INDEX=$((INDEX + 1))    # if (!pdroid), set as 1, 2...
  1397.           fi
  1398.      done
  1399.      for p in ${COPYPID[@]}; do
  1400.           # ensures they aren't applyingy more than one pdroid mod and slots in any remaining mods.
  1401.           if [ "$p" = "pdroid" ] || [ $p = "pd2.0" ] ||[ $p = "openpdroid" ]; then
  1402.                conflict_error "Those mods conflict! Only one pdroid mod at a time!"
  1403.           else
  1404.                i=${#SPID[@]}
  1405.                SPID[$i]=$p
  1406.                i+=1
  1407.           fi
  1408.      done
  1409. else
  1410.      SPID=("${COPYPID[@]}")
  1411. fi
  1412. # reset the mod list, with the pdroid-type mod applied first (for provisional use's sake)
  1413. PID=("${SPID[@]}")
  1414.  
  1415. # Check if selected mods are available for rom's Android version.
  1416. compat_check
  1417.  
  1418. # Make sure mod and rom don't conflict (e.g. pa and tabletUI)
  1419. for T in ${PID[@]}; do
  1420.      PID=$T                  #Set PID so mods_info function returns different values for each mod
  1421.      mods_info
  1422.      if [[ ${ROM_CONFLICTS[@]} != "None" ]]; then
  1423.           rom_compat_check
  1424.      fi
  1425. done
  1426. # reset to original ordered list
  1427. PID=("${SPID[@]}")
  1428.  
  1429. # Build date
  1430. if [ $# -gt 3 ]; then # if a date is specified on command line...
  1431.      # Set date to match argument and ensure that patches with that date exist
  1432.      BID=$4
  1433.      [ -d patches/$PID/$ANDR/$RID/$BID ] || print_error  "No $PID patches for $RID on $BID!! Try the help menu!!"
  1434.      # Run build_query- this overrides the above but gets useful info
  1435.      build_query
  1436.      # Reset patch date to argument (basically undo the effects of the build_query call)
  1437.      BID=$4
  1438.      echo "... $PID patch date set manually to: $BID ..."
  1439. else
  1440.      build_query # if no date specified, use one from build_query
  1441. fi
  1442.  
  1443. # echo ROM info
  1444. rominfo
  1445.  
  1446. if [[ ( "$PID" == "-h" || "$RID" == "-h" || "$BID" == "-h" ) ]]; then
  1447.      #Although this placement may be perfect for romfilter. although it throws the scheme...
  1448.      display_help
  1449. fi
  1450. #Begin cycle
  1451.  
  1452. for P in ${PID[@]}; do
  1453.      [ -d patches/$P ] || print_error "$P is not available"
  1454.      [ -d patches/$P/$ANDR ] && A=$ANDR || A=all
  1455.      [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
  1456.      [ -d patches/$P/$A/$R/$BID ] && B=$BID || B=latest
  1457.  
  1458.      unset FILES
  1459.      [ -f patches/$P/files.txt ] && FILES=$(cat patches/$P/files.txt)
  1460.      [ -f patches/$P/$A/files.txt ] && FILES=$(cat patches/$P/$A/files.txt)
  1461.      [ -f patches/$P/$A/$R/files.txt ] && FILES=$(cat patches/$P/$A/$R/files.txt)
  1462.      [ -f patches/$P/$A/$R/$B/files.txt ] && FILES=$(cat patches/$P/$A/$R/$B/files.txt)
  1463.      for FILE in ${FILES[@]}; do
  1464.           case "$FILE" in
  1465.           +system/*)
  1466.                [[ ${CID[@]} =~ ${FILE:1} ]] && print_error "${FILE:1} is already being copied" || CID=(${CID[@]} ${FILE:1})
  1467.                ;;
  1468.           -system/*)
  1469.                [[ ${EID[@]} =~ ${FILE:1} ]] || EID=(${EID[@]} ${FILE:1})
  1470.                ;;
  1471.           system/*.jar)
  1472.                [[ ${JID[@]} =~ $FILE ]] || JID=(${JID[@]} $FILE)
  1473.                echo "set_perm(0, 0, 0644, \"/$FILE\");" >> UPDATE.txt
  1474.                ;;
  1475.           # added for the use-case of apktool failure
  1476.           @system/*apk) # apks to decompile with smali binaries instead of apktool
  1477.                [[ ${JID[@]} =~ ${FILE:1} ]] || JID=(${JID[@]} ${FILE:1})
  1478.                FORMATTED_FILE=${FILE:1}
  1479.                echo "set_perm(0, 0, 0644, \"/$FORMATTED_FILE\");" >> UPDATE.txt
  1480.                ;;
  1481.           system/*.apk)
  1482.                [[ ${AID[@]} =~ $FILE ]] || AID=(${AID[@]} $FILE)
  1483.                echo "set_perm(0, 0, 0644, \"/$FILE\");" >> UPDATE.txt
  1484.                # $APKID will have elements removed sometimes--$AID is the master list for the update.zip
  1485.                declare -a APKID=( "${AID[@]}" )
  1486.                ;;
  1487.           *.img)
  1488.                [[ ${IID[@]} =~ $FILE ]] || IID=(${IID[@]} $FILE)
  1489.                ;;
  1490.           system/*)
  1491.                [[ ${TID[@]} =~ $FILE ]] || TID=(${TID[@]} $FILE)
  1492.                ;;
  1493.           esac
  1494.      done
  1495. done
  1496. echo "### DELETE FILES= ${EID[@]}" >> "$LOG"
  1497. echo "### COPY FILES= ${CID[@]}" >> "$LOG"
  1498. echo "### JAR FILES= ${JID[@]}" >> "$LOG"
  1499. echo "### APK FILES= ${AID[@]}" >> "$LOG"
  1500. echo "### IMG FILES= ${IID[@]}" >> "$LOG"
  1501. echo "### TXT FILES= ${TID[@]}" >> "$LOG"
  1502.  
  1503. # extract system files
  1504. printtask "### unzip rom ###"
  1505. jar -xvf "$ROMX" ${JID[@]} ${AID[@]} ${IID[@]} ${TID[@]} 2>> "$LOG" || print_error "failed extracting ${JID[@]} ${AID[@]} ${IID[@]} from $ROMX !!!"
  1506.  
  1507.  
  1508. # Update/Restore.zip scripts and prep
  1509. printtask "### create updater script ###"
  1510. UPDATER_LOC=META-INF/com/google/android
  1511. UPDS=updater-script
  1512. UPDATE="update-$ROMTYPE-$DID-$BDID-${PIL// /-}"
  1513. RESTORE="restore-$ROMTYPE-$DID-$BDID"
  1514.  
  1515. jar -xvf "$ROMX" $UPDATER_LOC/$UPDS $UPDATER_LOC/update-binary
  1516. jar -xvf "$ROMX" $UPDATER_LOC/update-binary-installer 1> /dev/null 2> /dev/null
  1517.  
  1518.      # Determine Installer- Aroma or ClockworkMod
  1519. if [ -f $UPDATER_LOC/update-binary-installer ]; then
  1520.      printtask "... using Aroma installer ..."
  1521.      \mv $UPDATER_LOC/update-binary-installer $UPDATER_LOC/update-binary
  1522. else
  1523.      printtask "... using Clockworkmod installer ..."
  1524. fi
  1525.  
  1526.      # Create updater scripts
  1527. cat patches/.common/updater-message.txt >> $UPDS
  1528. cat $UPDATER_LOC/$UPDS | grep ^mount | grep "/system" | head -n 1 >> $UPDS
  1529. [ -s $UPDS ] || echo "run_program(\"/sbin/busybox\", \"mount\", \"/system\");" >> $UPDS
  1530.      # restore the original
  1531. for FILE in ${EID[@]}; do
  1532.      jar -xvf "$ROMX" $FILE 2> /dev/null || printtask "!!! warning $FILE cannot be restored !!!"
  1533. done
  1534.      # replace with the original or delete it
  1535. for FILE in ${CID[@]}; do
  1536.      jar -xvf "$ROMX" $FILE 2> /dev/null || echo "delete(\"/$FILE\");" >> $UPDS
  1537. done
  1538. echo "package_extract_dir(\"system\", \"/system\");" >> $UPDS
  1539.      # permission management for files that need specific perms
  1540. cat $UPDATER_LOC/$UPDS | grep set_perm | grep "/system" >> $UPDS
  1541. for P in ${PID[@]}; do
  1542.      if [ -s patches/$P/$A/set_perm.txt ]; then
  1543.           echo "...setting permissions..."
  1544.           cat patches/$P/$A/set_perm.txt >> $UPDS
  1545.      fi
  1546. done
  1547. cat $UPDATER_LOC/$UPDS | grep ^unmount | grep "/system" | head -n 1 >> $UPDS
  1548. [[ ${IID[@]} =~ boot.img ]] && cat $UPDATER_LOC/$UPDS | grep "boot.img" >> $UPDS
  1549. echo "ui_print(\"                                        .\");" >> $UPDS
  1550. echo "ui_print(\"                                            .\");" >> $UPDS
  1551. echo "ui_print(\"                                                done!\");" >> $UPDS
  1552. cat $UPDS > $UPDATER_LOC/$UPDS
  1553. echo "# generated $UPDS" >> "$LOG"
  1554. cat $UPDS >> "$LOG"
  1555.  
  1556. printtask "### create restore.zip ###"
  1557. jar -cvf tmp.zip ${JID[@]} ${AID[@]} ${IID[@]} META-INF > /dev/null 2>> "$LOG"
  1558. java -jar signapk.jar updatecert.pem updatekey.pk8 tmp.zip signed.zip 2>> "$LOG"
  1559. \mv signed.zip $RESTORE.zip
  1560. \rm -f tmp.zip
  1561.  
  1562.  
  1563. # Begin patching process
  1564.  
  1565.      # decompile system files
  1566. printtask "### decompile system files ###"
  1567. for FILE in ${JID[@]}; do
  1568.      JAR=${FILE##*/}
  1569.      printtask "... decompile $JAR ..."
  1570.      java -jar baksmali.jar -b -a $API -o tmp/${FILE}.out/smali ${FILE} >> "$LOG" 2>> "$LOG"
  1571.      if [ $FILE == "system/framework/framework.jar" ]; then
  1572.           printtask "... extract preloaded-classes ..."
  1573.           jar -xvf ${FILE} preloaded-classes >> "$LOG" 2>> "$LOG"
  1574.           \mv preloaded-classes tmp/${FILE}.out/
  1575.      fi
  1576. done
  1577.  
  1578.      # install framework-files for APK...
  1579. if [ ${#AID[@]} -gt 0 ]; then
  1580.      jar -xvf "$ROMX" system/framework/framework-res.apk >> "$LOG"
  1581.      printtask "... installing framework files ..."
  1582.      java -jar apktool.jar if system/framework/framework-res.apk
  1583.    # Decompile APKs
  1584.      for FILE in ${AID[@]}; do
  1585.           APK=${FILE##*/}
  1586.           if [ -f $FILE ]; then
  1587.                printtask "... decompile $APK ..."
  1588.                java -jar apktool.jar d -f $FILE tmp/$FILE.out 2>> "$LOG"
  1589.           fi
  1590.      done
  1591. fi
  1592.  
  1593.      # Unpack image files {boot.img only so far...}
  1594. for FILE in ${IID[@]}; do
  1595.      printtask "### unpacking $FILE ###"
  1596.      case "$FILE" in
  1597.      boot.img)
  1598.           if [ -f boot.img ]; then
  1599.                cd boot
  1600.                ./unpackbootimg$EXE -i ../boot.img >> "$LOG" 2>> "$LOG"
  1601.                mkdir ramdisk
  1602.                cd ramdisk
  1603.                gzip -dc ../boot.img-ramdisk.gz | cpio -i >> "$LOG" 2>> "$LOG"
  1604.                cd ../../
  1605.           else
  1606.                print_error "A mod needs to patch boot.img! This rom does not contains boot.img."
  1607.           fi
  1608.           ;;
  1609.      *)
  1610.           print_error "cannot process $FILE"
  1611.           ;;
  1612.      esac
  1613. done
  1614.  
  1615. # apply system modification
  1616.  
  1617. for P in ${PID[@]}; do
  1618.      [ -d patches/$P/$ANDR ] && A=$ANDR || A=all
  1619.      [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
  1620.      [ -d patches/$P/$A/$R/$BID ] && B=$BID || B=latest
  1621.      PAR=patches/$P/$A/$R
  1622.      PARB=patches/$P/$A/$R/$B
  1623.  
  1624.      unset FILES COPY DELS JARS APKS IMGS TXTS
  1625.      [ -f patches/$P/files.txt ] && FILES=$(cat patches/$P/files.txt)
  1626.      [ -f patches/$P/$A/files.txt ] && FILES=$(cat patches/$P/$A/files.txt)
  1627.      [ -f patches/$P/$A/$R/files.txt ] && FILES=$(cat patches/$P/$A/$R/files.txt)
  1628.      [ -f patches/$P/$A/$R/$B/files.txt ] && FILES=$(cat patches/$P/$A/$R/$B/files.txt)
  1629.      for FILE in ${FILES[@]}; do
  1630.           case $FILE in
  1631.           +system/*)
  1632.                COPY=(${COPY[@]} ${FILE:1})
  1633.                ;;
  1634.           -system/*)
  1635.                DELS=(${DELS[@]} ${FILE:1})
  1636.                ;;
  1637.           system/*.jar)
  1638.                JARS=(${JARS[@]} $FILE)
  1639.                ;;
  1640.           @system/*)
  1641.                JARS=(${JARS[@]} ${FILE:1})
  1642.                ;;
  1643.           system/*.apk)
  1644.                APKS=(${APKS[@]} $FILE)
  1645.                ;;
  1646.           *.img)
  1647.                IMGS=(${IMGS[@]} $FILE)
  1648.                ;;
  1649.           system/*)
  1650.                TXTS=(${TXTS[@]} $FILE)
  1651.                ;;
  1652.           esac
  1653.      done
  1654.  
  1655.      [[ -n ${COPY[@]} ]] && echo "### copy patch $P/$A/$ROMTYPE/$B ###"
  1656.      echo "### copy patch $P/$A/$R/$B ###" >> "$LOG"
  1657.      for FILE in ${COPY[@]}; do
  1658.           printtask "... copying $FILE ..."
  1659.           DEST=${FILE%/*}
  1660.           mkdir -p ./$DEST
  1661.           if [ -e patches/$P/$A/$R/$B/$FILE ]; then
  1662.                printtask "... overwriting $FILE from $P ..."
  1663.                \cp -av patches/$P/$A/$R/$B/$FILE ./$DEST >> "$LOG" 2>&1
  1664.           elif [ -e patches/$P/common/$FILE ]; then
  1665.                printtask "... overwriting $FILE from $P ..."
  1666.                echo " ### SOMETHING WENT WRONG!! Could not find file-- using common. ###" >> "$LOG"
  1667.                \cp -av patches/$P/common/$FILE ./$DEST >> "$LOG" 2>&1
  1668.           else
  1669.                print_error "$FILE is missing in the patch. Please upload your logfile to the support thread!"
  1670.           fi
  1671.      done
  1672.  
  1673.      # unconditional smali copying- For particularly troublesome files
  1674.      [ -f patches/$P/smali.txt ] && SMALI=$(cat patches/$P/smali.txt)
  1675.      [ -f patches/$P/$A/smali.txt ] && SMALI=$(cat patches/$P/$A/smali.txt)
  1676.      [ -f patches/$P/$A/$R/smali.txt ] && SMALI=$(cat patches/$P/$A/$R/smali.txt)
  1677.      [ -f patches/$P/$A/$R/$B/smali.txt ] && SMALI=$(cat patches/$P/$A/$R/$B/smali.txt)
  1678.      MOVE=${SMALI##*/}
  1679.      for SMALI in ${SMALI[@]}; do
  1680.            if [ -f patches/$P/$A/$R/$B/$SMALI ]; then
  1681.                 printtask "... overwriting $MOVE ..."
  1682.                 \cp -a patches/$P/$ANDR/$R/$B/$SMALI tmp/$SMALI >> "$LOG" 2>&1
  1683. # This lower area should be deprecated now...wait and see. Each BID has own link to provisionals now.
  1684.            elif [ -f patches/$P/$A/.provisionals/$SMALI ]; then
  1685.                 printtask "... overwriting $MOVE ..."
  1686.                 \cp -a patches/$P/$ANDR/.provisionals/$SMALI tmp/$SMALI >> "$LOG"
  1687.            fi
  1688.      done
  1689.  
  1690.      [[ -n ${DELS[@]} ]] && echo "### delete patch $P/$A/$ROMTYPE/$B ###"
  1691.      echo "### delete patch $P/$A/$R/$B ###" >> "$LOG"
  1692.      for FILE in ${DELS[@]}; do
  1693.           printtask "... deleting $FILE ..."
  1694.           \rm -rf ./$FILE
  1695.      done
  1696.      [[ -n ${JARS[@]} ]] && echo "### jar patch $P/$A/$ROMTYPE/$B ###"
  1697.      echo "### jar patch $P/$A/$R/$B ###" >> "$LOG"
  1698.  
  1699.      # Iterate through jars and patch. If fails, try prepatched provisionals or look for alternative patch.
  1700.      for (( current_jar=0; current_jar < ${#JARS[@]}; current_jar++)); do
  1701.           FILE=${JARS[$current_jar]}
  1702.           JAR=${FILE##*/}
  1703.           if [[ $FILE == *".jar" ]]; then # allows for *.apks to be decompiled by baksmali
  1704.                NAME=${JAR%.jar}
  1705.           else
  1706.                # check for abscense of any apks decompiled by baksmali (~* in files.txt). Missing jars will still error
  1707.                if [ ! -d "tmp/${FILE}.out" ] ; then
  1708.                     echo "No $JAR in your ROM. Not able to patch this file."
  1709.                     echo "## No $JAR in ROM ###" >> "$LOG"
  1710.                     declare -a JID=( ${JID[@]/$FILE/} )
  1711.                     break;
  1712.                fi
  1713.                NAME=$JAR # TODO: Maybe rename this, since it now encompases apks at times
  1714.           fi
  1715.           printtask "... apply $NAME.patch ..."
  1716.           $PATCH_BINARY -Nls -p1 -d tmp/${FILE}.out < $PARB/${NAME}.patch 2>&1 > ${JAR}.log
  1717.           for INCREMENTAL in $(ls $PARB/${NAME}-*.patch 2>/dev/null); do
  1718.                printtask "... apply $(basename $INCREMENTAL) ..."
  1719.                $PATCH_BINARY -Nls -p1 -d tmp/${FILE}.out < $INCREMENTAL 2>&1 >> ${JAR}.log
  1720.           done
  1721.           cat ${JAR}.log >> "$LOG"
  1722.           case "$NAME" in
  1723.           framework)
  1724.                # patch preloaded-classes only if a patch exists
  1725.                if [ -f $PARB/preloaded.patch ]; then
  1726.                     printtask "... apply preloaded.patch ..."
  1727.                     $PATCH_BINARY -Nls -p1 -d tmp/${FILE}.out < $PARB/preloaded.patch 2>&1 > preloaded.log
  1728.                     cat preloaded.log >> "$LOG"
  1729.                     [[ $(grep FAILED preloaded.log) != "" ]] && print_error "failed preloaded.patch"
  1730.                fi
  1731.                ;;
  1732.           *)
  1733.                echo -n ""     # do nothing else
  1734.                ;;
  1735.           esac
  1736.           # substitute failed smali with provisional (a prepatched smali file)
  1737.           unset FAILS
  1738.           FAILS=($(grep FAILED ${JAR}.log | sed -e 's/.*smali\///' | sed -e 's/\.rej//'))
  1739.           ALT_PATCH_NUMBER=2
  1740.           if [[ -n ${FAILS[@]} ]]; then
  1741.                print_to_log "!!! PATCHING FAILED FOR ${FAILS[@]} !!!"
  1742.                echo "### replace ${FAILS[@]} ###" > ${JAR}.log
  1743.                for FAIL in ${FAILS[@]}; do
  1744.                     PROVISIONAL="patches/$P/$ANDR/.provisionals/${FILE}.out/smali/${FAIL}"
  1745.                     ALTERNATIVE_PATCH=$(ls $PARB/${NAME}$ALT_PATCH_NUMBER*.patch 2>/dev/null)
  1746.                     if [ ! -f "$PROVISIONAL" ]; then
  1747.                          if [ -f "$ALTERNATIVE_PATCH" ]; then
  1748.                               printtask "## trying alternative ${NAME} patch ###"
  1749.                               prep_alternate_patch
  1750.                          else
  1751.                               print_error "failed $FAIL in $JAR cannot be remedied"
  1752.                          fi
  1753.                     else
  1754.                          echo "!!! patch error for $FAIL !!!" >> "$LOG"
  1755.                          [[ ${SID[@]} =~ $FAIL ]] && print_error "$FAIL was already replaced before"
  1756.                          copy_prepatched_provisional
  1757.                     fi
  1758.                done
  1759.           fi
  1760.           [[ $(grep FAILED ${JAR}.log) != "" ]] &&  print_error "failed $NAME.patch"
  1761.           garbage tmp/${FILE}.out
  1762.  
  1763.      # run special $JAR.sh scripts if any JAR needs special attention
  1764.           if [ -f patches/$P/$A/$R/$B/${JAR}.sh ]; then
  1765.                printtask "... apply $JAR.sh ..."
  1766.                . patches/$P/$A/$R/$B/${JAR}.sh 2>&1 > ${JAR}.log
  1767.                cat ${JAR}.log | tee -a "$LOG"
  1768.           fi
  1769.      done
  1770.  
  1771.      [[ -n ${APKS[@]} ]] && echo "### apk patch $P/$A/$ROMTYPE/$B ###"
  1772.      echo "### apk patch $P/$A/$R/$B ###" >> "$LOG"
  1773.      for FILE in ${APKS[@]}; do
  1774.           APK=${FILE##*/}
  1775.           if [ -d "tmp/${FILE}.out" ] ; then
  1776.                printtask "... apply $APK.patch ..."
  1777.                $PATCH_BINARY -Nls -p2 -d tmp/${FILE}.out < $PARB/${APK}.patch 2>&1 > ${APK}.log
  1778.                cat ${APK}.log >> "$LOG"
  1779.  
  1780.                if [[ $(grep FAILED ${APK}.log) != "" ]]; then
  1781.                     apk_copy
  1782.                     declare -a APKID=( ${APKID[@]/$FILE/} )
  1783.                fi
  1784.                garbage tmp/${FILE}.out
  1785.           else
  1786.                echo "No $APK in your ROM. Not able to patch this file."
  1787.                echo "## No $APK in ROM ###" >> "$LOG"
  1788.           fi
  1789.      done
  1790.      [[ -n ${IMGS[@]} ]] && echo "### img patch $P/$A/$ROMTYPE/$B ###"
  1791.      echo "### img patch $P/$A/$R/$B ###" >> "$LOG"
  1792.      for FILE in ${IMGS[@]}; do
  1793.           IMG=${FILE##*/}
  1794.           printtask "... applying $IMG.patch ..."
  1795.           $PATCH_BINARY -Nls -p1 -d boot/ramdisk < $PARB/${IMG}.patch >> "$LOG" || print_error "failed patching $FILE"
  1796.      done
  1797.  
  1798. # Text Editing Functions --this can really do just about anything. Needs a working restore!
  1799.      [[ -n ${TXTS[@]} ]] && echo "### text patch $P/$A/$ROMTYPE/$B ###"
  1800.      echo "### text patch $P/$A/$R/$B ###" >> "$LOG"
  1801.      for FILE in ${TXTS[@]}; do
  1802.           TXT=${FILE##*/}
  1803.           if [[ -f $PARB/${TXT}.patch ]]; then
  1804.                printtask "... apply $TXT.patch ..."
  1805.                $PATCH_BINARY -Nls -p2 -d system/ < $PARB/${TXT}.patch 2>&1 > ${TXT}.log
  1806.                #cat ${TXT}.log | tee -a "$LOG"
  1807.                [[ $(grep FAILED ${TXT}.log) != "" ]] && print_error "failed $TXT.patch"
  1808.           fi
  1809.           if [[ -f $PAR/${TXT}.awk ]]; then
  1810.                printtask "... apply $TXT.awk ..."
  1811.                awk -f $PAR/${TXT}.awk $FILE > text || print_error "failed $TXT.awk"
  1812.                mv text $FILE
  1813.           fi
  1814.           if [[ -f $PAR/${TXT}.sed ]]; then
  1815.                printtask "... apply $TXT.sed ..."
  1816.                sed -f $PAR/${TXT}.sed $FILE > text || print_error "failed $TXT.sed"
  1817.                mv text $FILE
  1818.           fi
  1819.           if [[ -f $PAR/${TXT}.sh ]]; then
  1820.                printtask "... apply $TXT.sh ..."
  1821.                . $PAR/${TXT}.sh 2>&1 >> ${TXT}.log || print_error "failed $TXT.sh"
  1822.           fi
  1823.           garbage system/
  1824.      done
  1825. done
  1826.  
  1827.      # Device specific scripts- Placement is up-for-review
  1828. if [ -f patches/.devices/$DID/$DID.sh ]; then
  1829.      . patches/.devices/$DID/$DID.sh
  1830. fi
  1831.  
  1832.      # Replace updater-script for CM7- it formats system -Testing
  1833. if [ "$ANDR" == "gingerbread" ]; then
  1834.      replace_updater
  1835. fi
  1836.  
  1837. # Rebuilding system files
  1838.  
  1839. [[ -n ${JID[@]} ]] && printtask "### rebuild jar ###"
  1840. for FILE in ${JID[@]}; do
  1841.      JAR=${FILE##*/}
  1842.      printtask "... rebuild $JAR ..."
  1843.      java -Xmx512M -jar smali.jar -a $API tmp/${FILE}.out/smali -o tmp/${FILE}.out/classes.dex >> "$LOG" 2>> "$LOG"
  1844.      [ -f tmp/${FILE}.out/classes.dex ] || smali_error "failed compiling $JAR !!!"
  1845.      jar -ufv ${FILE} -C tmp/${FILE}.out/ classes.dex >> "$LOG" 2>> "$LOG"
  1846.      if [ $FILE == "system/framework/framework.jar" ]; then
  1847.           jar -ufv ${FILE} -C tmp/${FILE}.out/ preloaded-classes >> "$LOG" 2>> "$LOG"
  1848.      fi
  1849. done
  1850.  
  1851. [[ -n ${APKID[@]} ]] && printtask "### rebuild apk ###"
  1852. for FILE in ${APKID[@]}; do
  1853.      APK=${FILE##*/}
  1854.      if [ -d tmp/${FILE}.out ]; then
  1855.           apk_was_copied=false
  1856.           printtask "... rebuild $APK ..."
  1857.           java -jar apktool.jar b tmp/${FILE}.out >> "$LOG" 2>> "$LOG" || apk_copy
  1858.           if !("$apk_was_copied"); then
  1859.                jar -uvf $FILE -C tmp/${FILE}.out/build/apk/ . > /dev/null 2>> "$LOG"
  1860.           fi
  1861.      fi
  1862. done
  1863.  
  1864. [[ -n ${IID[@]} ]] && printtask "### rebuild img ###"
  1865. for FILE in ${IID[@]}; do
  1866.      printtask "### rebuild $FILE ###"
  1867.      case $FILE in
  1868.      boot.img)
  1869.           # Rebuilding boot.img
  1870.           cd boot
  1871.           printtask "... rebuilding ramdisk ..."
  1872.           ./mkbootfs$EXE ./ramdisk | gzip > ramdisk-new.gz
  1873.           printtask "... composing boot.img ..."
  1874.           BASE=$(cat boot.img-base)
  1875.           # hex to decimal in cygwin prefixing "0x" w/ C arithmetic
  1876.           PGSIZE=$((${PGFORM}$(cat boot.img-pagesize)))
  1877.           CMDLINE=$(cat boot.img-cmdline)
  1878.           echo "cmdline = no_console_suspend=1 console=null $CMDLINE" >> "$LOG"
  1879.           echo "pagesize = $PGSIZE" >> "$LOG"
  1880.           echo "base = $BASE" >> "$LOG"
  1881.           printtask "... rebuilding boot.img ..."
  1882.           ./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"
  1883.           cd ..
  1884.           ;;
  1885.      *)
  1886.           print_error "cannot process $FILE"
  1887.           ;;
  1888.      esac
  1889. done
  1890.  
  1891. printtask "### create $UPDATE.zip ###"
  1892. jar -cfv tmp.zip ${IID[@]} ${JID[@]} ${AID[@]} ${CID[@]} ${TID[@]} META-INF >> "$LOG" 2>> "$LOG"
  1893. java -jar signapk.jar updatecert.pem updatekey.pk8 tmp.zip $UPDATE.zip 2>> "$LOG"
  1894.  
  1895. \mv $RESTORE.zip $UPDATE.zip ..
  1896. printtask "### finished ###"
  1897. printtask ""
  1898. printtask "$(colorize_bold ${red}Congratulations), the patching was $(colorize_bold ${red}successful)."
  1899. printtask "Any errors you may have seen were succesfully handled!"
  1900. printtask ""
  1901. printtask "!!! Only file a bug report if the software is $(colorize_bold ${grn}not) behaving as expected !!!"
  1902. printtask ""
  1903. echo "to install patched files, flash:"
  1904. printtask "---$(colorize_bold ${grn}$UPDATE.zip)"
  1905. echo ""
  1906. printtask "to restore original system, flash:"
  1907. printtask "---$(colorize_bold ${grn}$RESTORE.zip)"
  1908. echo ""
  1909.  
  1910.      # add announcement- optional.
  1911. for P in ${PID[@]}; do
  1912.      [ -d patches/$P/$ANDR ] && A=$ANDR || A=all
  1913.      [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
  1914.      [ -d patches/$P/$A/$R/$BID ] && B=$BID || B=latest
  1915.      if [ -f patches/$P/$A/$R/$B/README.txt ]; then
  1916.           cat patches/$P/$A/$R/$B/README.txt
  1917.      elif [ -f patches/$P/$A/$R/README.txt ]; then
  1918.           cat patches/$P/$A/$R/README.txt
  1919.      elif [ -f patches/$P/$A/README.txt ]; then
  1920.           cat patches/$P/$A/README.txt
  1921.      fi
  1922. done
  1923.  
  1924. cd ..
  1925. \rm -rf "$TEMP"
Add Comment
Please, Sign In to add comment