Paste Shaver

Text Content

@echo off
setlocal enabledelayedexpansion

rem ============================================================
rem  CONFIGURATION – EDIT THIS SECTION
rem ============================================================

rem Choose which edition to use (1=Stable, 2=Developer, 3=ESR, 4=Nightly, 5=ESR115)
set "EDITION=1"

rem Path to your silent installer for each edition (fixed filename)
rem Update these paths to match your network share or local folder
set "INSTALLER_1=\\server\share\Firefox-Stable-Setup.exe"
set "INSTALLER_2=\\server\share\Firefox-Developer-Setup.exe"
set "INSTALLER_3=\\server\share\Firefox-ESR-Setup.exe"
set "INSTALLER_4=\\server\share\Firefox-Nightly-Setup.exe"
set "INSTALLER_5=\\server\share\Firefox-ESR115-Setup.exe"

rem Installation folder (use "registry" for Standard Firefox, or full path for others)
rem Registry detection only works for Standard Firefox installed via the official installer
set "FOLDER_1=registry"
set "FOLDER_2=C:\Program Files\Firefox Developer Edition"
set "FOLDER_3=registry"
set "FOLDER_4=C:\Program Files\Firefox Nightly"
set "FOLDER_5=registry"

rem ============================================================
rem  PARSE COMMAND-LINE ARGUMENTS
rem ============================================================

rem Initialize flags
set "VERBOSE=0"          rem Set to 1 if -v switch is used
set "KILL_PROCESS=0"     rem Set to 1 if -k switch is used
set "EDITION_OVERRIDE="  rem Stores edition number if passed as argument

:ParseArgs
if "%~1"=="" goto :ArgsDone

rem Check for verbose switches
if /I "%~1"=="-v" set "VERBOSE=1"
if /I "%~1"=="/v" set "VERBOSE=1"
if /I "%~1"=="-verbose" set "VERBOSE=1"
if /I "%~1"=="/verbose" set "VERBOSE=1"

rem Check for kill switches
if /I "%~1"=="-k" set "KILL_PROCESS=1"
if /I "%~1"=="/k" set "KILL_PROCESS=1"
if /I "%~1"=="-kill" set "KILL_PROCESS=1"
if /I "%~1"=="/kill" set "KILL_PROCESS=1"
if /I "%~1"=="--kill" set "KILL_PROCESS=1"

rem Check if argument is a number 1-5 (edition override)
echo %~1 | findstr /r "^[1-5]$" >nul
if not errorlevel 1 set "EDITION_OVERRIDE=%~1"

shift
goto :ParseArgs
:ArgsDone

rem Apply edition override if provided on command line
if defined EDITION_OVERRIDE set "EDITION=%EDITION_OVERRIDE%"

rem ============================================================
rem  VERBOSE MODE – DISPLAY FULL JSON FROM MOZILLA API
rem ============================================================

if %VERBOSE%==1 (
    echo ===== Fetching full JSON from Mozilla API =====
    echo.
    powershell -command "$r = Invoke-RestMethod -Uri 'https://product-details.mozilla.org/1.0/firefox_versions.json'; $r | ConvertTo-Json"
    echo.
    echo ===============================================
    echo.
)

rem ============================================================
rem  DO NOT EDIT BELOW THIS LINE
rem ============================================================

rem Map edition number to Mozilla API key
rem These keys correspond to the fields in the JSON response
if "%EDITION%"=="1" set "API_KEY=LATEST_FIREFOX_VERSION"
if "%EDITION%"=="2" set "API_KEY=LATEST_FIREFOX_DEVEL_VERSION"
if "%EDITION%"=="3" set "API_KEY=FIREFOX_ESR"
if "%EDITION%"=="4" set "API_KEY=FIREFOX_NIGHTLY"
if "%EDITION%"=="5" set "API_KEY=FIREFOX_ESR115"

rem Validate the edition number
if "%API_KEY%"=="" (
    echo ERROR: Invalid edition %EDITION%. Use 1,2,3,4,5.
    echo   1 = Stable
    echo   2 = Developer Edition
    echo   3 = ESR
    echo   4 = Nightly
    echo   5 = ESR115
    exit /b 1
)

rem Get the installer path and installation folder for the selected edition
call :GetVar INSTALLER_%EDITION% INSTALLER_PATH
call :GetVar FOLDER_%EDITION% FIREFOX_FOLDER

rem Display current settings
echo.
echo ==========================================
echo Firefox Update Script
echo ==========================================
echo Edition: %EDITION% (key: %API_KEY%)
echo Installer: %INSTALLER_PATH%
if %KILL_PROCESS%==1 echo Kill switch: ENABLED (Firefox will be terminated)
if %VERBOSE%==1 echo Verbose mode: ENABLED
echo ==========================================
echo.

rem ============================================================
rem  DETERMINE INSTALLATION FOLDER
rem ============================================================

rem If the folder is set to "registry", query the Windows registry
rem This only works for standard Firefox installed via the official installer
if /I "%FIREFOX_FOLDER%"=="registry" (
    echo Detecting Firefox installation folder from registry...
    for /F "skip=2 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe query "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe" /v Path 2^>nul') do (
        if /I "%%I"=="Path" set "FIREFOX_FOLDER=%%K"
    )
    echo Registry detection complete.
    echo.
)

rem ============================================================
rem  CHECK IF FIREFOX IS INSTALLED
rem ============================================================

:CheckFirefox
if not defined FIREFOX_FOLDER (
    echo Firefox installation folder not found.
    echo Proceeding with fresh installation...
    echo.
    goto Install
)

if not exist "%FIREFOX_FOLDER%\firefox.exe" (
    echo Firefox executable not found at %FIREFOX_FOLDER%
    echo Proceeding with fresh installation...
    echo.
    goto Install
)

echo Firefox found at: %FIREFOX_FOLDER%

rem ============================================================
rem  FETCH TARGET VERSION FROM MOZILLA API
rem ============================================================

echo Fetching latest %API_KEY% from Mozilla...
for /f "delims=" %%V in ('powershell -command "(Invoke-RestMethod -Uri 'https://product-details.mozilla.org/1.0/firefox_versions.json').%API_KEY%"') do set "TARGET_VERSION=%%V"

rem Validate that we got a version number
if not defined TARGET_VERSION (
    echo.
    echo ERROR: Could not fetch target version. Please check:
    echo   - Internet connection
    echo   - Firewall/proxy settings
    echo   - Mozilla API availability
    echo.
    exit /b 1
)
echo Target version available: %TARGET_VERSION%

rem ============================================================
rem  GET INSTALLED VERSION
rem ============================================================

echo Getting currently installed version...
for /f "tokens=2" %%V in ('"%FIREFOX_FOLDER%\firefox.exe" -v 2^>nul ^| findstr /r "[0-9]\.[0-9]"') do set "INSTALLED_VERSION=%%V"

if defined INSTALLED_VERSION (
    echo Currently installed version: %INSTALLED_VERSION%
) else (
    echo Could not determine installed version.
    echo Will proceed with update anyway.
    echo.
)

rem ============================================================
rem  COMPARE VERSIONS AND DECIDE ACTION
rem ============================================================

if defined INSTALLED_VERSION if "%INSTALLED_VERSION%"=="%TARGET_VERSION%" (
    echo.
    echo ==========================================
    echo Firefox is already up to date!
    echo Version %INSTALLED_VERSION% is the latest.
    echo ==========================================
    echo.
    goto :EOF
)

rem ============================================================
rem  INSTALL OR UPDATE FIREFOX
rem ============================================================

:Install
if defined INSTALLED_VERSION (
    echo.
    echo ==========================================
    echo Updating Firefox from %INSTALLED_VERSION%
    echo to version %TARGET_VERSION%
    echo ==========================================
    echo.
) else (
    echo.
    echo ==========================================
    echo Installing Firefox version %TARGET_VERSION%
    echo ==========================================
    echo.
)

rem ============================================================
rem  KILL FIREFOX PROCESSES (if enabled)
rem ============================================================

if %KILL_PROCESS%==1 (
    echo Terminating all running Firefox processes...
    echo This may take a moment...
    echo.
    
    rem Forcefully terminate all firefox.exe processes
    taskkill /F /IM firefox.exe 2>nul
    
    rem Wait 2 seconds to ensure processes are fully closed
    timeout /t 2 /nobreak >nul
    
    rem Check if any Firefox processes remain
    tasklist /FI "IMAGENAME eq firefox.exe" 2>nul | find /I "firefox.exe" >nul
    if not errorlevel 1 (
        echo.
        echo WARNING: Some Firefox processes could not be terminated.
        echo The update may fail if files are in use.
        echo You may need to run this script as Administrator.
        echo.
    ) else (
        echo All Firefox processes have been terminated.
        echo.
    )
)

rem ============================================================
rem  RUN THE INSTALLER
rem ============================================================

rem Verify the installer file exists before running it
if not exist "%INSTALLER_PATH%" (
    echo.
    echo ERROR: Installer not found at:
    echo %INSTALLER_PATH%
    echo.
    echo Please verify the path in the script configuration.
    echo.
    exit /b 1
)

echo Running silent installer...
echo This may take several minutes. Please wait...
echo.

rem The -ms switch performs a silent install (no UI, no prompts)
"%INSTALLER_PATH%" -ms

rem Check if the installer ran successfully
if errorlevel 1 (
    echo.
    echo WARNING: Installer returned an error code.
    echo Please check the logs or run manually for more details.
    echo.
) else (
    echo.
    echo ==========================================
    echo Update completed successfully!
    echo Firefox %TARGET_VERSION% should now be installed.
    echo ==========================================
    echo.
)

goto :EOF

rem ============================================================
rem  HELPER FUNCTION – GET VARIABLE BY NAME
rem ============================================================

:GetVar
rem This function retrieves the value of a variable by its name
rem Usage: call :GetVar VARIABLE_NAME OUTPUT_VARIABLE
set "%~2=!%~1!"
goto :EOF