updater.bat 8.3 KB

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