updater.bat 9.1 KB

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