2017-11-17 15:45:16 +01:00
@ ECHO OFF
TITLE ghacks user.js updater
REM ### ghacks-user.js updater for Windows
REM ## author: @claustromaniac
2017-12-03 14:35:41 +01:00
REM ## version: 3.1
2017-11-17 15:45:16 +01:00
2017-11-26 15:57:32 +01:00
SET _myname = %~n0
SET _myparams = %*
2017-11-17 15:45:16 +01:00
SETLOCAL EnableDelayedExpansion
2017-11-19 08:58:47 +01:00
: parse
2017-11-26 15:57:32 +01:00
IF " %~1 " == " " (
GOTO endparse
)
IF /I " %~1 " == " -unattended " (
SET _ua = 1
)
IF /I " %~1 " == " -log " (
SET _log = 1
)
2017-11-26 21:47:30 +01:00
IF /I " %~1 " == " -logp " (
SET _log = 1
SET _logp = 1
)
2017-11-26 15:57:32 +01:00
IF /I " %~1 " == " -multioverrides " (
SET _multi = 1
)
2017-11-26 21:47:30 +01:00
IF /I " %~1 " == " -merge " (
SET _merge = 1
)
2017-11-28 22:53:20 +01:00
IF /I " %~1 " == " -updatebatch " (
SET _updateb = 1
)
2017-11-19 08:58:47 +01:00
SHIFT
GOTO parse
: endparse
ECHO .
2017-11-28 22:53:20 +01:00
IF DEFINED _updateb (
2017-12-03 14:35:41 +01:00
REM THe normal flow here goes from phase 1 to phase 2 and then phase 3.
2017-11-28 22:53:20 +01:00
IF NOT " !_myname:~0,9! " == " [updated] " (
2017-12-03 14:35:41 +01:00
REM Phase 3
REM The new script, with the original name, should:
REM Delete the [updated]*.bat script
REM Begin the normal script routine.
2017-11-30 01:10:01 +01:00
IF EXIST " [updated]!_myname!.bat " (
2017-12-03 14:35:41 +01:00
REN [updated]!_myname!.bat [updated]!_myname!.bat.old
DEL /F " [updated]!_myname!.bat.old "
2017-11-30 23:17:58 +01:00
ECHO Script updated^^!
ECHO .
TIMEOUT 3 > nul
CLS
ECHO .
2017-11-30 01:10:01 +01:00
GOTO begin
)
2017-12-03 14:35:41 +01:00
REM Phase 1
REM -updatebatch will:
REM Download new batch and name it [updated]*.bat
REM Open that script in a new CMD window.
REM Exit
2017-11-30 23:17:58 +01:00
ECHO Updating script...
2017-11-28 22:53:20 +01:00
ECHO .
REM Uncomment the next line and comment the powershell call for testing.
REM COPY /B /V /Y "!_myname!.bat" "[updated]!_myname!.bat"
2017-11-29 05:56:33 +01:00
(
powershell -Command " (New-Object Net.WebClient).DownloadFile('https://github.com/ghacksuserjs/ghacks-user.js/raw/master/updater.bat', '[updated]!_myname!.bat') "
) >nul 2>&1
2017-11-28 22:53:20 +01:00
IF EXIST " [updated]!_myname!.bat " (
2017-11-30 01:10:01 +01:00
START /min CMD /C " [updated]!_myname!.bat " !_myparams!
2017-11-28 22:53:20 +01:00
EXIT /B
) ELSE (
ECHO Failed. Make sure PowerShell is allowed internet access.
ECHO .
2017-11-30 23:17:58 +01:00
TIMEOUT 120 > nul
2017-11-28 22:53:20 +01:00
EXIT /B
)
) ELSE (
2017-12-03 14:35:41 +01:00
REM Phase 2
REM The [updated]*.bat script will:
REM Copy itself overwriting the original batch.
REM Start that script in a new CMD instance.
REM Exit.
2017-11-30 01:10:01 +01:00
IF " !_myname! " == " [updated] " (
ECHO The [updated] label is reserved. Rename this script and try again.
2017-11-29 05:33:02 +01:00
ECHO .
2017-11-30 23:17:58 +01:00
TIMEOUT 300 > nul
2017-11-30 01:10:01 +01:00
) ELSE (
2017-12-03 14:35:41 +01:00
IF EXIST !_myname:~9! .bat (
REN !_myname:~9! .bat !_myname:~9! .bat.old
DEL /F !_myname:~9! .bat.old
)
2017-11-30 01:10:01 +01:00
COPY /B /V /Y " !_myname! .bat " " !_myname:~9! .bat "
START CMD /C " !_myname:~9! .bat " !_myparams!
2017-11-28 22:53:20 +01:00
)
2017-11-30 01:10:01 +01:00
EXIT /B
2017-11-28 22:53:20 +01:00
)
)
: begin
2017-11-26 15:57:32 +01:00
SET /A " _line=0 "
IF NOT EXIST user.js (
ECHO user.js not detected in the current directory.
) ELSE (
FOR /F " skip=1 tokens=1,2 delims=: " %% G IN ( user.js) DO (
2017-11-17 15:45:16 +01:00
SET /A " _line+=1 "
2017-11-26 15:57:32 +01:00
IF !_line! GEQ 4 (
GOTO exitloop
)
IF !_line! EQU 1 (
SET _name = %% H
)
IF !_line! EQU 2 (
SET _date = %% H
)
IF !_line! EQU 3 (
SET _version = %% G
)
2017-11-17 15:45:16 +01:00
)
2017-11-26 15:57:32 +01:00
: exitloop
2017-11-17 15:45:16 +01:00
IF !_line! GEQ 4 (
2017-11-26 15:57:32 +01:00
IF /I NOT " !_name! " == " !_name:ghacks=X! " (
ECHO ghacks user.js !_version:~2! ,!_date!
) ELSE (
ECHO Current user.js version not recognised.
)
) ELSE (
ECHO Current user.js version not recognised.
)
)
2017-11-17 15:45:16 +01:00
ECHO .
2017-11-26 15:57:32 +01:00
IF NOT DEFINED _ua (
ECHO .
2017-11-19 08:58:47 +01:00
ECHO This batch should be run from your Firefox profile directory. It will download the latest version of ghacks user.js from github and then append any of your own changes from user-overrides.js to it.
ECHO .
2017-12-01 05:05:24 +01:00
ECHO Visit the wiki for more detailed information.
ECHO .
2017-11-19 08:58:47 +01:00
CHOICE /M " Continue "
2017-12-01 05:05:24 +01:00
IF ERRORLEVEL 2 ( EXIT /B )
2017-11-19 08:58:47 +01:00
)
2017-11-17 15:45:16 +01:00
CLS
ECHO .
2017-11-26 15:57:32 +01:00
IF DEFINED _log (
CALL : log >> user.js-update-log.txt 2 >& 1
2017-11-27 14:02:12 +01:00
IF DEFINED _logp (
START user.js-update-log.txt
)
2017-11-19 08:58:47 +01:00
EXIT /B
2017-11-19 18:41:19 +01:00
: log
2017-11-19 08:58:47 +01:00
ECHO ##################################################################
ECHO .
ECHO %date% , %time%
ECHO .
)
2017-12-03 16:04:41 +01:00
IF EXIST user.js.old.bak ( DEL /F user.js.old.bak )
2017-11-17 15:45:16 +01:00
IF EXIST user.js (
2017-12-03 16:04:41 +01:00
IF EXIST user.js.bak ( REN user.js.bak user.js.old.bak )
2017-11-17 15:45:16 +01:00
REN user.js user.js.bak
ECHO Current user.js file backed up.
ECHO .
)
2017-11-26 15:57:32 +01:00
ECHO Retrieving latest user.js file from github repository...
2017-11-29 05:56:33 +01:00
(
powershell -Command " (New-Object Net.WebClient).DownloadFile('https://github.com/ghacksuserjs/ghacks-user.js/raw/master/user.js', 'user.js') "
) >nul 2>&1
2017-11-17 15:45:16 +01:00
ECHO .
IF EXIST user.js (
2017-11-26 15:57:32 +01:00
IF DEFINED _multi (
ECHO Multiple overrides enabled. List of files found:
FORFILES /P user.js-overrides /M *.js
IF %ERRORLEVEL% EQU 0 (
IF DEFINED _merge (
ECHO .
2017-11-26 21:47:30 +01:00
ECHO Merging...
2017-11-26 15:57:32 +01:00
ECHO .
2017-11-26 21:47:30 +01:00
COPY /B /V /Y user.js-overrides\*.js user-overrides
2017-11-27 14:02:12 +01:00
CALL : merge user-overrides user-overrides-merged.js
2017-11-28 22:53:20 +01:00
COPY /B /V /Y user.js+user-overrides-merged.js updatertempfile
CALL : merge updatertempfile user.js
2017-11-26 15:57:32 +01:00
) ELSE (
ECHO .
ECHO Appending...
ECHO .
COPY /B /V /Y user.js+" user.js-overrides\*.js " user.js
)
)
2017-11-19 08:58:47 +01:00
ECHO .
2017-11-26 15:57:32 +01:00
) ELSE (
IF EXIST " user-overrides.js " (
IF DEFINED _merge (
2017-11-26 21:47:30 +01:00
ECHO Merging user-overrides.js...
2017-11-28 22:53:20 +01:00
COPY /B /V /Y user.js+user-overrides.js updatertempfile
CALL : merge updatertempfile user.js
2017-11-26 15:57:32 +01:00
) ELSE (
ECHO Appending user-overrides.js...
ECHO .
COPY /B /V /Y user.js+" user-overrides.js " " user.js "
)
) ELSE (
ECHO user-overrides.js not found.
)
2017-11-19 08:58:47 +01:00
ECHO .
2017-11-17 15:45:16 +01:00
)
2017-11-19 08:58:47 +01:00
ECHO Handling backups...
SET " changed= "
2017-11-26 15:57:32 +01:00
IF EXIST user.js.bak (
FC user.js.bak user.js > nul && SET " changed=false " || SET " changed=true "
)
2017-11-17 15:45:16 +01:00
ECHO .
ECHO .
2017-11-19 08:58:47 +01:00
IF " !changed! " == " true " (
2017-11-27 14:02:12 +01:00
IF EXIST user.js.old.bak DEL /F user.js.old.bak
2017-11-19 08:58:47 +01:00
ECHO Update complete.
) ELSE (
IF " !changed! " == " false " (
DEL /F user.js.bak
IF EXIST user.js.old.bak REN user.js.old.bak user.js.bak
ECHO Update completed without changes.
2017-11-26 15:57:32 +01:00
) ELSE (
ECHO Update complete.
)
2017-11-19 08:58:47 +01:00
)
ECHO .
2017-11-17 15:45:16 +01:00
) ELSE (
2017-12-01 05:05:24 +01:00
IF EXIST user.js.bak ( REN user.js.bak user.js )
IF EXIST user.js.old.bak ( REN user.js.old.bak user.js.bak )
2017-11-17 15:45:16 +01:00
ECHO .
ECHO Update failed. Make sure PowerShell is allowed internet access.
ECHO .
2017-11-19 08:58:47 +01:00
ECHO No changes were made.
ECHO .
)
2017-11-26 15:57:32 +01:00
IF NOT DEFINED _log (
IF NOT DEFINED _ua PAUSE
2017-11-17 15:45:16 +01:00
)
2017-11-26 21:47:30 +01:00
EXIT /B
2017-11-27 14:02:12 +01:00
REM ###### Merge function ######
: merge
2017-12-01 05:05:24 +01:00
IF EXIST updatertempfile1 ( DEL /F updatertempfile1 )
2017-11-27 14:02:12 +01:00
SETLOCAL disabledelayedexpansion
2017-11-29 17:15:20 +01:00
(
2017-11-30 23:17:58 +01:00
FOR /F " tokens=1,* delims=] " %% G IN ( 'FIND /n /v "" ^< " %~1 "' ) DO (
2017-11-29 17:15:20 +01:00
SET " _pref= %% H "
SETLOCAL enabledelayedexpansion
SET " _temp=!_pref: =! "
IF /I " user_pref " == " !_temp:~0,9! " (
IF /I NOT " user.js.parrot " == " !_temp:~12,14! " (
FOR /F " delims=, " %% S IN ( " !_pref! " ) DO (
SET " _pref= %% S "
)
SET _pref = !_pref:"=""!
FIND /I " !_pref! " updatertempfile1 > nul 2 >& 1
IF ERRORLEVEL 1 (
FOR /F " tokens=* delims= " %% X IN ( 'FIND /I "!_pref!" %~1 ' ) DO (
SET " _temp= %% X "
SET " _temp=!_temp: =! "
IF /I " user_pref " == " !_temp:~0,9! " (
SET " _pref= %% X "
)
2017-11-27 14:02:12 +01:00
)
2017-11-29 17:15:20 +01:00
ECHO (!_pref!
ECHO (!_pref!>> updatertempfile1
2017-11-26 21:47:30 +01:00
)
2017-11-29 17:15:20 +01:00
) ELSE (
ECHO (!_pref!
2017-11-26 21:47:30 +01:00
)
2017-11-27 14:02:12 +01:00
) ELSE (
2017-11-29 17:15:20 +01:00
ECHO (!_pref!
2017-11-26 21:47:30 +01:00
)
2017-11-29 17:15:20 +01:00
ENDLOCAL
2017-11-26 21:47:30 +01:00
)
2017-11-29 17:15:20 +01:00
)>%~2
2017-11-27 14:02:12 +01:00
ENDLOCAL
2017-11-29 17:15:20 +01:00
DEL /F %1 updatertempfile1 > nul
2017-11-28 17:32:32 +01:00
GOTO : EOF
2017-11-27 14:02:12 +01:00
REM ############################