user.js

mirror of https://github.com/arkenfox/user.js
Log | Files | Refs | README

updater.bat (10919B)


      1 @ECHO OFF & SETLOCAL EnableDelayedExpansion
      2 TITLE arkenfox user.js updater
      3 
      4 REM ## arkenfox user.js updater for Windows
      5 REM ## author: @claustromaniac
      6 REM ## version: 4.19
      7 REM ## instructions: https://github.com/arkenfox/user.js/wiki/5.1-Updater-[Options]#-windows
      8 
      9 SET v=4.19
     10 
     11 VERIFY ON
     12 CD /D "%~dp0"
     13 SET _myname=%~n0
     14 SET _myparams=%*
     15 
     16 :parse
     17 IF "%~1"=="" (GOTO endparse)
     18 IF /I "%~1"=="-unattended" (SET _ua=1)
     19 IF /I "%~1"=="-log" (SET _log=1)
     20 IF /I "%~1"=="-logp" (SET _log=1 & SET _logp=1)
     21 IF /I "%~1"=="-multioverrides" (SET _multi=1)
     22 IF /I "%~1"=="-merge" (SET _merge=1)
     23 IF /I "%~1"=="-updatebatch" (SET _updateb=1)
     24 IF /I "%~1"=="-singlebackup" (SET _singlebackup=1)
     25 IF /I "%~1"=="-esr" (SET _esr=1)
     26 SHIFT
     27 GOTO parse
     28 :endparse
     29 
     30 FOR /F %%i IN ('PowerShell -Command "[Enum]::GetNames([Net.SecurityProtocolType]) -contains 'Tls12'"') DO (
     31 	IF "%%i" == "False" (
     32 		CALL :message "Your PowerShell version doesn't support TLS1.2 ^!"
     33 		ECHO:  Instructions to update PowerShell are on the arkenfox wiki
     34 		PAUSE
     35 		EXIT
     36 	)
     37 )
     38 
     39 IF DEFINED _updateb (
     40 	REM The normal flow here goes from phase 1 to phase 2 and then phase 3.
     41 	IF NOT "!_myname:~0,9!"=="[updated]" (
     42 		IF EXIST "[updated]!_myname!.bat" (
     43 			REM ## Phase 3 ##: The new script, with the original name, will:
     44 			REM 	* Delete the [updated]*.bat and *.bat.old scripts
     45 			REM 	* Begin the normal routine
     46 			FC "[updated]!_myname!.bat" "!_myname!.bat.old" >nul
     47 			IF NOT "!errorlevel!"=="0" (
     48 				CALL :message "Script updated to version !v!"
     49 				TIMEOUT 3 >nul
     50 			)
     51 			REN "[updated]!_myname!.bat" "[updated]!_myname!.bat.old"
     52 			DEL /F "!_myname!.bat.old" "[updated]!_myname!.bat.old"
     53 			GOTO begin
     54 		)
     55 		REM ## Phase 1 ##
     56 		REM 	* Download new batch and name it [updated]*.bat
     57 		REM 	* Start that script in a new CMD window
     58 		REM 	* Exit
     59 		CALL :message "Updating script..."
     60 		REM Uncomment the next line and comment out the PowerShell call for testing.
     61 		REM COPY /B /Y "!_myname!.bat" "[updated]!_myname!.bat" >nul
     62 		CALL :psdownload https://raw.githubusercontent.com/arkenfox/user.js/master/updater.bat "[updated]!_myname!.bat"
     63 		IF EXIST "[updated]!_myname!.bat" (
     64 			START /min CMD /C "[updated]!_myname!.bat" !_myparams!
     65 		) ELSE (
     66 			CALL :message "Failed. Make sure PowerShell is allowed internet access."
     67 			TIMEOUT 120 >nul
     68 		)
     69 	) ELSE (
     70 		IF "!_myname!"=="[updated]" (
     71 			CALL :message "The [updated] label is reserved. Rename this script and try again."
     72 			TIMEOUT 300 >nul
     73 		) ELSE (
     74 			REM ## Phase 2 ##: The [updated]*.bat script will:
     75 			REM 	* Rename the old script and make a copy of itself with the original name.
     76 			REM 	* Run that copy in a new CMD instance
     77 			REM 	* Exit
     78 			IF EXIST "!_myname:~9!.bat" ( REN "!_myname:~9!.bat" "!_myname:~9!.bat.old" )
     79 			COPY /B /Y "!_myname!.bat" "!_myname:~9!.bat"
     80 			START CMD /C "!_myname:~9!.bat" !_myparams!
     81 		)
     82 	)
     83 	EXIT /B
     84 )
     85 
     86 :begin
     87 CLS
     88 ECHO:
     89 ECHO:
     90 ECHO:                ########################################
     91 ECHO:                ####  user.js Updater for Windows   ####
     92 ECHO:                ####       by claustromaniac        ####
     93 ECHO:                ####             v!v!              ####
     94 ECHO:                ########################################
     95 ECHO:
     96 SET /A "_line=0"
     97 IF NOT EXIST user.js (
     98 	CALL :message "user.js not detected in the current directory."
     99 ) ELSE (
    100 	FOR /F "skip=1 tokens=1,* delims=:" %%G IN (user.js) DO (
    101 		SET /A "_line+=1"
    102 		IF !_line! GEQ 4 (GOTO exitloop)
    103 		IF !_line! EQU 1 (SET _name=%%H)
    104 		IF !_line! EQU 2 (SET _date=%%H)
    105 		IF !_line! EQU 3 (SET _version=%%G)
    106 	)
    107 	:exitloop
    108 	IF NOT "!_name!"=="" (
    109 		SET "_tempvar="
    110 		IF /I NOT "!_name!"=="!_name:ghacks=!" SET _tempvar=1
    111 		IF /I NOT "!_name!"=="!_name:arkenfox=!" SET _tempvar=1
    112 		IF !_tempvar! EQU 1 (
    113 			CALL :message "!_name! !_version:~2!,!_date!"
    114 		) ELSE (CALL :message "Current user.js version not recognised.")
    115 	) ELSE (CALL :message "Current user.js version not recognised.")
    116 )
    117 ECHO:
    118 IF NOT DEFINED _ua (
    119 	CALL :message "This batch should be run from your Firefox profile directory."
    120 	ECHO:  It will download the latest version of arkenfox user.js from github and then
    121 	CALL :message "append any of your own changes from user-overrides.js to it."
    122 	CALL :message "Visit the wiki for more detailed information."
    123 	ECHO:
    124 	TIMEOUT 1 /nobreak >nul
    125 	CHOICE /C SHE /N /M "Start [S] Help [H] Exit [E]"
    126 	CLS
    127 	IF ERRORLEVEL 3 (EXIT /B)
    128 	IF ERRORLEVEL 2 (GOTO :showhelp)
    129 )
    130 IF DEFINED _log (
    131 	CALL :log >>user.js-update-log.txt 2>&1
    132 	IF DEFINED _logp (START user.js-update-log.txt)
    133 	EXIT /B
    134 	:log
    135 	SET _log=2
    136 	ECHO:##################################################################
    137 	ECHO:  %date%, %time%
    138 )
    139 IF EXIST user.js.new (DEL /F "user.js.new")
    140 CALL :message "Retrieving latest user.js file from github repository..."
    141 CALL :psdownload https://raw.githubusercontent.com/arkenfox/user.js/master/user.js "user.js.new"
    142 IF EXIST user.js.new (
    143 	IF DEFINED _esr (
    144 		CALL :message "Activating ESR section..."
    145 		CALL :activate user.js.new ".x still uses all the following prefs"
    146 	)
    147 	IF DEFINED _multi (
    148 		FORFILES /P user.js-overrides /M *.js >nul 2>&1
    149 		IF NOT ERRORLEVEL 1 (
    150 			IF DEFINED _merge (
    151 				CALL :message "Merging..."
    152 				COPY /B /Y user.js-overrides\*.js user-overrides-merged.js
    153 				CALL :merge user-overrides-merged.js
    154 				COPY /B /Y user.js.new+user-overrides-merged.js user.js.new
    155 				CALL :merge user.js.new
    156 			) ELSE (
    157 				CALL :message "Appending..."
    158 				COPY /B /Y user.js.new+"user.js-overrides\*.js" user.js.new
    159 			)
    160 		) ELSE (CALL :message "No override files found.")
    161 	) ELSE (
    162 		IF EXIST "user-overrides.js" (
    163 			COPY /B /Y user.js.new+"user-overrides.js" "user.js.new"
    164 			IF DEFINED _merge (
    165 				CALL :message "Merging user-overrides.js..."
    166 				CALL :merge user.js.new
    167 			) ELSE (
    168 				CALL :message "user-overrides.js appended."
    169 			)
    170 		) ELSE (CALL :message "user-overrides.js not found.")
    171 	)
    172 	IF EXIST user.js (
    173 		FC user.js.new user.js >nul && SET "_changed=false" || SET "_changed=true"
    174 	)
    175 	IF "!_changed!"=="true" (
    176 		CALL :message "Backing up..."
    177 		IF DEFINED _singlebackup (
    178 			MOVE /Y user.js user.js.bak >nul
    179 		) ELSE (
    180 			FOR /F "delims=" %%# IN ('powershell get-date -format "{yyyyMMdd_HHmmss}"') DO @SET ldt=%%#
    181 			MOVE /Y user.js "user-backup-!ldt!.js" >nul
    182 		)
    183 		REN user.js.new user.js
    184 		CALL :message "Update complete."
    185 	) ELSE (
    186 		IF "!_changed!"=="false" (
    187 			DEL /F user.js.new >nul
    188 			CALL :message "Update completed without changes."
    189 		) ELSE (
    190 			REN user.js.new user.js
    191 			CALL :message "Update complete."
    192 			SET "_changed=true"
    193 		)
    194 	)
    195 ) ELSE (
    196 	CALL :message "Update failed. Make sure PowerShell is allowed internet access."
    197 	ECHO:  No changes were made.
    198 )
    199 IF NOT DEFINED _log (
    200 	IF NOT DEFINED _ua (
    201 		IF EXIST prefsCleaner.bat (
    202 			IF "!_changed!"=="true" (
    203 				CALL :message "Would you like to run the prefsCleaner now?"
    204 				CHOICE /C YN /N /M "(Y/N) "
    205 				IF "1"=="!errorlevel!" ( START "" cmd.exe /C "prefsCleaner.bat" )
    206 			) ELSE (PAUSE)
    207 		) ELSE (PAUSE)
    208 	)
    209 )
    210 EXIT /B
    211 
    212 ::::::::::::::: Message :::::::::::::::
    213 :message
    214 SETLOCAL DisableDelayedExpansion
    215 IF NOT "2"=="%_log%" (ECHO:)
    216 ECHO:  %~1
    217 IF NOT "2"=="%_log%" (ECHO:)
    218 ENDLOCAL
    219 GOTO :EOF
    220 
    221 ::::::::::::::: Download :::::::::::::::
    222 :psdownload
    223 (
    224 	PowerShell -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (New-Object Net.WebClient).DownloadFile('%~1', '%~2')"
    225 ) >nul 2>&1
    226 GOTO :EOF
    227 
    228 ::::::::::::::: Activate Section :::::::::::::::
    229 :activate
    230 :: arg1 = file
    231 :: arg2 = line substring
    232 SETLOCAL DisableDelayedExpansion
    233 (
    234 	FOR /F "tokens=1,* delims=:" %%G IN ('FINDSTR /N "^" "%~1"') DO (
    235 		SET "_temp=%%H"
    236 		SETLOCAL EnableDelayedExpansion
    237 		IF "!_temp:%~2=!"=="!_temp!" (
    238 			ENDLOCAL & ECHO:%%H
    239 		) ELSE (
    240 			ECHO://!_temp:~2!
    241 			ENDLOCAL
    242 		)
    243 	)
    244 )>updatertempfile
    245 MOVE /Y updatertempfile "%~1" >nul
    246 ENDLOCAL
    247 GOTO :EOF
    248 
    249 ::::::::::::::: Merge :::::::::::::::
    250 :merge
    251 SETLOCAL DisableDelayedExpansion
    252 FOR /F tokens^=2^,^*^ delims^=^'^" %%G IN ('FINDSTR /R /C:"^user_pref[ 	]*\([ 	]*[\"'].*[\"'][ 	]*,.*\)[ 	]*;" "%~1"') DO (SET "[%%G]=%%H")
    253 FOR /F tokens^=2^,^*^ delims^=^' %%G IN ('FINDSTR /R /C:"^//// --- comment-out --- '[^'][^']*'.*" "%~1"') DO (SET "__unset__%%G=1")
    254 (
    255 	FOR /F "tokens=1,* delims=:" %%I IN ('FINDSTR /N "^" "%~1"') DO (
    256 		SET "_temp=%%J"
    257 		SETLOCAL EnableDelayedExpansion
    258 		IF NOT "!_temp:~0,9!"=="user_pref" (
    259 			ENDLOCAL & ECHO:%%J
    260 		) ELSE (
    261 			IF "!_temp:;=!"=="!_temp!" (
    262 				ENDLOCAL & ECHO:%%J
    263 			) ELSE (
    264 				ENDLOCAL
    265 				FOR /F tokens^=2^ delims^=^'^" %%K IN ("%%J") DO (
    266 					IF NOT "_user.js.parrot"=="%%K" (
    267 						IF DEFINED __unset__%%K (
    268 							ECHO://%%J
    269 						) ELSE (
    270 							IF DEFINED [%%K] (
    271 								SETLOCAL EnableDelayedExpansion
    272 								FOR /F "delims=" %%L IN ("![%%K]!") DO (
    273 									ENDLOCAL & ECHO:user_pref("%%K"%%L
    274 									SET "[%%K]="
    275 								)
    276 							)
    277 						)
    278 					) ELSE (ECHO:%%J)
    279 				)
    280 			)
    281 		)
    282 	)
    283 )>updatertempfile
    284 MOVE /Y updatertempfile "%~1" >nul
    285 ENDLOCAL
    286 GOTO :EOF
    287 
    288 ::::::::::::::: Help :::::::::::::::
    289 :showhelp
    290 MODE 80,54
    291 CLS
    292 CALL :message "Available arguments (case-insensitive):"
    293 CALL :message "  -esr"
    294 ECHO:     Activate ESR related preferences
    295 CALL :message "  -log"
    296 ECHO:     Write the console output to a logfile (user.js-update-log.txt)
    297 CALL :message "  -logP"
    298 ECHO:     Like -log, but also open the logfile after updating.
    299 CALL :message "  -merge"
    300 ECHO:     Merge overrides instead of appending them. Single-line comments and
    301 ECHO:     _user.js.parrot lines are appended normally. Overrides for inactive
    302 ECHO:     user.js prefs will be appended. When -Merge and -MultiOverrides are used
    303 ECHO:     together, a user-overrides-merged.js file is also generated in the root
    304 ECHO:     directory for quick reference. It contains only the merged data from
    305 ECHO:     override files and can be safely discarded after updating, or used as the
    306 ECHO:     new user-overrides.js. When there are conflicting records for the same
    307 ECHO:     pref, the value of the last one declared will be used. Visit the wiki
    308 ECHO:     for usage examples and more detailed information.
    309 CALL :message "  -multiOverrides"
    310 ECHO:     Use any and all .js files in a user.js-overrides sub-folder as overrides
    311 ECHO:     instead of the default user-overrides.js file. Files are appended in
    312 ECHO:     alphabetical order.
    313 CALL :message "  -unattended"
    314 ECHO:     Run without user input.
    315 CALL :message "  -singleBackup"
    316 ECHO:     Use a single backup file and overwrite it on new updates, instead of
    317 ECHO:     cumulative backups. This was the default behaviour before v4.3.
    318 CALL :message "  -updateBatch"
    319 ECHO:     Update the script itself on execution, before the normal routine.
    320 CALL :message ""
    321 PAUSE
    322 MODE 80,25
    323 GOTO :begin