updater.bat 8.8 KB

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