updater.bat 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. @ECHO OFF & SETLOCAL EnableDelayedExpansion
  2. TITLE ghacks user.js updater
  3. REM ## ghacks-user.js updater for Windows
  4. REM ## author: @claustromaniac
  5. REM ## version: 4.1
  6. REM ## instructions: https://github.com/ghacksuserjs/ghacks-user.js/wiki/3.3-Updater-Scripts
  7. SET _myname=%~n0
  8. SET _myparams=%*
  9. :parse
  10. IF "%~1"=="" (GOTO endparse)
  11. IF /I "%~1"=="-unattended" (SET _ua=1)
  12. IF /I "%~1"=="-log" (SET _log=1)
  13. IF /I "%~1"=="-logp" (SET _log=1 & SET _logp=1)
  14. IF /I "%~1"=="-multioverrides" (SET _multi=1)
  15. IF /I "%~1"=="-merge" (SET _merge=1)
  16. IF /I "%~1"=="-updatebatch" (SET _updateb=1)
  17. SHIFT
  18. GOTO parse
  19. :endparse
  20. IF DEFINED _updateb (
  21. REM The normal flow here goes from phase 1 to phase 2 and then phase 3.
  22. IF NOT "!_myname:~0,9!"=="[updated]" (
  23. IF EXIST "[updated]!_myname!.bat" (
  24. REM ## Phase 3 ##: The new script, with the original name, will:
  25. REM * Delete the [updated]*.bat script
  26. REM * Begin the normal routine
  27. REN "[updated]!_myname!.bat" "[updated]!_myname!.bat.old"
  28. DEL /F "[updated]!_myname!.bat.old"
  29. CALL :message "Script updated^!"
  30. TIMEOUT 3 >nul
  31. CLS
  32. GOTO begin
  33. )
  34. REM ## Phase 1 ##
  35. REM * Download new batch and name it [updated]*.bat
  36. REM * Start that script in a new CMD window
  37. REM * Exit
  38. CALL :message "Updating script..."
  39. REM Uncomment the next line and comment the powershell call for testing.
  40. REM COPY /B /V /Y "!_myname!.bat" "[updated]!_myname!.bat"
  41. (
  42. powershell -Command "(New-Object Net.WebClient).DownloadFile('https://github.com/ghacksuserjs/ghacks-user.js/raw/master/updater.bat', '[updated]!_myname!.bat')"
  43. ) >nul 2>&1
  44. IF EXIST "[updated]!_myname!.bat" (
  45. START /min CMD /C "[updated]!_myname!.bat" !_myparams!
  46. EXIT /B
  47. ) ELSE (
  48. CALL :message "Failed. Make sure PowerShell is allowed internet access."
  49. TIMEOUT 120 >nul
  50. EXIT /B
  51. )
  52. ) ELSE (
  53. IF "!_myname!"=="[updated]" (
  54. CALL :message "The [updated] label is reserved. Rename this script and try again."
  55. TIMEOUT 300 >nul
  56. ) ELSE (
  57. REM ## Phase 2 ##: The [updated]*.bat script will:
  58. REM * Copy itself overwriting the original batch
  59. REM * Start that script in a new CMD instance
  60. REM * Exit
  61. IF EXIST "!_myname:~9!.bat" (
  62. REN "!_myname:~9!.bat" "!_myname:~9!.bat.old"
  63. DEL /F "!_myname:~9!.bat.old"
  64. )
  65. COPY /B /V /Y "!_myname!.bat" "!_myname:~9!.bat"
  66. START CMD /C "!_myname:~9!.bat" !_myparams!
  67. )
  68. EXIT /B
  69. )
  70. )
  71. :begin
  72. ECHO:
  73. ECHO:
  74. ECHO: ########################################
  75. ECHO: #### user.js Updater for Windows ####
  76. ECHO: #### by claustromaniac ####
  77. ECHO: #### v4.1 ####
  78. ECHO: ########################################
  79. ECHO:
  80. SET /A "_line=0"
  81. IF NOT EXIST user.js (
  82. CALL :message "user.js not detected in the current directory."
  83. ) ELSE (
  84. FOR /F "skip=1 tokens=1,2 delims=:" %%G IN (user.js) DO (
  85. SET /A "_line+=1"
  86. IF !_line! GEQ 4 (GOTO exitloop)
  87. IF !_line! EQU 1 (SET _name=%%H)
  88. IF !_line! EQU 2 (SET _date=%%H)
  89. IF !_line! EQU 3 (SET _version=%%G)
  90. )
  91. :exitloop
  92. IF !_line! GEQ 4 (
  93. IF /I NOT "!_name!"=="!_name:ghacks=!" (
  94. CALL :message "ghacks user.js !_version:~2!,!_date!"
  95. ) ELSE (
  96. CALL :message "Current user.js version not recognised."
  97. )
  98. ) ELSE (
  99. CALL :message "Current user.js version not recognised."
  100. )
  101. )
  102. ECHO:
  103. IF NOT DEFINED _ua (
  104. CALL :message "This batch should be run from your Firefox profile directory."
  105. ECHO: It will download the latest version of ghacks user.js from github and then
  106. CALL :message "append any of your own changes from user-overrides.js to it."
  107. CALL :message "Visit the wiki for more detailed information."
  108. ECHO:
  109. TIMEOUT 1 /nobreak >nul
  110. CHOICE /C SHE /N /M "Start [S] Help [H] Exit [E]"
  111. CLS
  112. IF ERRORLEVEL 3 (EXIT /B)
  113. IF ERRORLEVEL 2 (GOTO :showhelp)
  114. )
  115. IF DEFINED _log (
  116. CALL :log >>user.js-update-log.txt 2>&1
  117. IF DEFINED _logp (START user.js-update-log.txt)
  118. EXIT /B
  119. :log
  120. ECHO:##################################################################
  121. CALL :message "%date%, %time%"
  122. )
  123. IF EXIST user.js.old.bak (DEL /F user.js.old.bak)
  124. IF EXIST user.js (
  125. IF EXIST user.js.bak (REN user.js.bak user.js.old.bak)
  126. REN user.js user.js.bak
  127. CALL :message "Current user.js file backed up."
  128. )
  129. CALL :message "Retrieving latest user.js file from github repository..."
  130. (
  131. powershell -Command "(New-Object Net.WebClient).DownloadFile('https://github.com/ghacksuserjs/ghacks-user.js/raw/master/user.js', 'user.js')"
  132. ) >nul 2>&1
  133. IF EXIST user.js (
  134. IF DEFINED _multi (
  135. FORFILES /P user.js-overrides /M *.js >nul 2>&1
  136. IF NOT ERRORLEVEL 1 (
  137. IF DEFINED _merge (
  138. CALL :message "Merging..."
  139. COPY /B /V /Y user.js-overrides\*.js user-overrides-merged.js
  140. CALL :merge user-overrides-merged.js
  141. COPY /B /V /Y user.js+user-overrides-merged.js user.js
  142. CALL :merge user.js
  143. ) ELSE (
  144. CALL :message "Appending..."
  145. COPY /B /V /Y user.js+"user.js-overrides\*.js" user.js
  146. )
  147. (
  148. ECHO:
  149. ECHO:user_pref("_user-overrides.js.parrot", "Parrot successfully overridden");
  150. )>>user.js
  151. ) ELSE (CALL :message "No override files found.")
  152. ECHO:
  153. ) ELSE (
  154. IF EXIST "user-overrides.js" (
  155. COPY /B /V /Y user.js+"user-overrides.js" "user.js"
  156. IF DEFINED _merge (
  157. CALL :message "Merging user-overrides.js..."
  158. CALL :merge user.js
  159. ) ELSE (
  160. CALL :message "user-overrides.js appended."
  161. )
  162. (
  163. ECHO:
  164. ECHO:user_pref("_user-overrides.js.parrot", "Parrot successfully overridden");
  165. )>>user.js
  166. ) ELSE (CALL :message "user-overrides.js not found.")
  167. ECHO:
  168. )
  169. CALL :message "Handling backups..."
  170. SET "changed="
  171. IF EXIST user.js.bak (
  172. FC user.js.bak user.js >nul && SET "changed=false" || SET "changed=true"
  173. )
  174. IF "!changed!"=="true" (
  175. IF EXIST user.js.old.bak DEL /F user.js.old.bak
  176. CALL :message "Update complete."
  177. ) ELSE (
  178. IF "!changed!"=="false" (
  179. DEL /F user.js.bak
  180. IF EXIST user.js.old.bak REN user.js.old.bak user.js.bak
  181. CALL :message "Update completed without changes."
  182. ) ELSE (CALL :message "Update complete.")
  183. )
  184. ECHO:
  185. ) ELSE (
  186. IF EXIST user.js.bak (REN user.js.bak user.js)
  187. IF EXIST user.js.old.bak (REN user.js.old.bak user.js.bak)
  188. CALL :message "Update failed. Make sure PowerShell is allowed internet access."
  189. ECHO: No changes were made.
  190. )
  191. IF NOT DEFINED _log (
  192. IF NOT DEFINED _ua (PAUSE)
  193. )
  194. EXIT /B
  195. REM ########### Message Function ###########
  196. :message
  197. SETLOCAL DisableDelayedExpansion
  198. IF NOT DEFINED _log (ECHO:)
  199. ECHO: %~1
  200. IF NOT DEFINED _log (ECHO:)
  201. ENDLOCAL
  202. GOTO :EOF
  203. REM ############ Merge function ############
  204. :merge
  205. SETLOCAL DisableDelayedExpansion
  206. (
  207. FOR /F "tokens=1,* delims=," %%G IN ('FINDSTR /B /I /C:"user_pref" "%~1"') DO (SET "%%G=%%H")
  208. FOR /F "tokens=1,* delims=:" %%I IN ('FINDSTR /N "^" "%~1"') DO (
  209. IF ""=="%%J" (
  210. ECHO:
  211. ) ELSE (
  212. FOR /F "delims=," %%K IN ("%%J") DO (
  213. IF NOT [user_pref("_user.js.parrot"]==[%%K] (
  214. IF DEFINED %%K (
  215. SETLOCAL EnableDelayedExpansion
  216. FOR /F "delims=" %%L IN ("!%%K!") DO (
  217. ENDLOCAL
  218. IF NOT "%%L"=="ALREADY MERGED" (
  219. ECHO:%%K,%%L
  220. SET "%%K=ALREADY MERGED"
  221. )
  222. )
  223. ) ELSE (
  224. ECHO:%%J
  225. )
  226. ) ELSE (
  227. ECHO:%%J
  228. )
  229. )
  230. )
  231. )
  232. )>updatertempfile
  233. MOVE /Y updatertempfile "%~1" >nul
  234. ENDLOCAL
  235. GOTO :EOF
  236. REM ############### Help ##################
  237. :showhelp
  238. MODE 80,43
  239. CLS
  240. CALL :message "Available arguments (case-insensitive):"
  241. CALL :message " -log"
  242. ECHO: Write the console output to a logfile (user.js-update-log.txt)
  243. CALL :message " -logP"
  244. ECHO: Like -log, but also open the logfile after updating.
  245. CALL :message " -merge"
  246. ECHO: Merge overrides instead of appending them. One-line comments and
  247. ECHO: _user.js.parrot lines are appended normally. Overrides for inactive
  248. ECHO: user.js prefs will be appended. When -Merge and -MultiOverrides are used
  249. ECHO: together, a user-overrides-merged.js file is also generated in the root
  250. ECHO: directory for quick reference. It contains only the merged data from
  251. ECHO: override files and can be safely discarded after updating, or used as the
  252. ECHO: new user-overrides.js. When there are conflicting records for the same
  253. ECHO: pref, the value of the last one declared will be used. Visit the wiki
  254. ECHO: for usage examples and more detailed information.
  255. CALL :message " -multiOverrides"
  256. ECHO: Use any and all .js files in a user.js-overrides sub-folder as overrides
  257. ECHO: instead of the default user-overrides.js file. Files are appended in
  258. ECHO: alphabetical order.
  259. CALL :message " -unattended"
  260. ECHO: Run without user input.
  261. CALL :message " -updatebatch"
  262. ECHO: Update the script itself on execution, before the normal routine.
  263. CALL :message ""
  264. PAUSE
  265. CLS
  266. MODE 80,25
  267. GOTO :begin
  268. REM #####################################