@echo OFF
if not exist "%~dp0\AME-Firefox-Injection" (
	echo. & echo No supplied AME-Firefox-Injection folder detected.
	exit /b 4
)

:ENTRIES

echo. & echo Grabbing previous Firefox entries...

if not exist "%TEMP%\Firefox-Reg-Output.txt" echo No input entries detected & echo. & echo Generating random string... & goto GenRND

setlocal EnableDelayedExpansion
set /a "count1=0"
for /f "usebackq tokens=2 delims=-" %%A in (`findstr /c:"Firefox-" "%TEMP%\Firefox-Reg-Output.txt"`) do (
	set /a "count1=!count1!+1"
	set "ffBef!count1!=%%A"
	set "par=!par!)"
	set "arg=!arg!if not "%%D"=="%%A" ("
)

del /q /f "%TEMP%\Firefox-Reg-Output.txt" > NUL 2>&1

echo. & echo Comparing Firefox entries...
set /a "count2=0"
for /f "usebackq tokens=2 delims=-" %%D in (`reg query "HKLM\SOFTWARE\Clients\StartMenuInternet" /f "Firefox-"`) do (
	set /a "count2=!count2!+1"
	%arg%set "NewCode=%%D"%par%
)

if "%count1%"=="0" (if "%count2%"=="0" (set "NewCode=NULL"))
endlocal & set "NewCode=%NewCode%"

:CHECKS

set "RunEC=10"
set /a "count0=1"

for /f "usebackq tokens=2 delims=\" %%A in (`reg query "HKEY_USERS" ^| findstr /r /x /c:"HKEY_USERS\\S-.*" /c:"HKEY_USERS\\AME_UserHive_[^_]*"`) do (
	if "%%A"=="AME_UserHive_Default" (
		call :AFISCALL "%SYSTEMDRIVE%\Users\Default\AppData\Roaming"
			if errorlevel 2 set "RunEC=2"
			if errorlevel 1 set "RunEC=1"
			if errorlevel 0 set "RunEC=0"
	) else (
		for /f "usebackq tokens=2* delims= " %%B in (`reg query "HKU\%%A\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "AppData" 2^>^&1 ^| findstr /R /X /C:".*AppData[ ]*REG_SZ[ ].*"`) do (
			call :AFISCALL "%%C"
				if errorlevel 2 set "RunEC=2"
				if errorlevel 1 set "RunEC=1"
				if errorlevel 0 set "RunEC=0"
		)
	)
)
exit /b %RunEC%

:AFISCALL

setlocal

echo. & echo Generating random string...

:GenRND

setlocal EnableDelayedExpansion
set "RNDConsist=abcdefghijklmnopqrstuvwxyz0123456789"
set /a "RND=%RANDOM% %% 36"
set "RNDStr=!RNDStr!!RNDConsist:~%RND%,1!"
if "%RNDStr:~7%"=="" (goto GenRND)
endlocal & set "RNDStr=%RNDStr%"

:: Redundancy, incase the original Firefox installed check fails
if "%NewCode%"=="NULL" echo. & echo No Firefox install detected & goto PREFSONLY
if "%NewCode%"=="" echo. & echo Firefox version already installed & goto PREFSONLY
if not EXIST "%PROGRAMFILES%\Mozilla Firefox\firefox.exe" echo. & echo Can't find Firefox.exe & goto PREFSONLY
if exist "%~1\Mozilla\Firefox\profiles.ini" (
	findstr /c:"%NewCode%" "%~1\Mozilla\Firefox\profiles.ini" > NUL 2>&1
		if not errorlevel 1 echo. & echo Firefox version already in profiles.ini & goto PREFSONLY
)

:PROFILENAME

if %count0% GTR 50 echo. & echo Default-release count exceeded 50 & goto PREFSONLY
if exist "%~1\Mozilla\Firefox\profiles.ini" (
	findstr /c:"Name=default-release" "%~1\Mozilla\Firefox\profiles.ini" > NUL 2>&1
		if not errorlevel 1 (
			findstr /c:"Name=default-release-%count0%" "%~1\Mozilla\Firefox\profiles.ini"
				if not errorlevel 1 (
					set /a "count0=%count0%+1"
					goto PROFILENAME
				) else (
					set "profileName=default-release-%count0%"
				)
		) else (
			set "profileName=default-release"
		)
) else (
	set "profileName=default-release"
)


echo. & echo Injecting profile...
@echo ON

:: This could also be set manually in the profiles.ini file
mkdir "%~1\Mozilla\Firefox\Profiles\%RNDStr%.%profileName%"
robocopy "%~dp0\AME-Firefox-Injection" "%~1\Mozilla\Firefox\Profiles\%RNDStr%.%profileName%" /E /xf "3647222921wleabcEoxlt-eengsairo.sqlite" > NUL
mkdir "%~1\Mozilla\Firefox\Profiles\%RNDStr%.%profileName%\storage\default\moz-extension+++41087662-660a-4251-8c0c-38aa4da5b325^userContextId=4294967295\idb"
copy /y "%~dp0\AME-Firefox-Injection\3647222921wleabcEoxlt-eengsairo.sqlite" "%~1\Mozilla\Firefox\Profiles\%RNDStr%.%profileName%\storage\default\moz-extension+++41087662-660a-4251-8c0c-38aa4da5b325^userContextId=4294967295\idb"

:: Sets profile as the default
echo [Install%NewCode%]>> "%~1\Mozilla\Firefox\profiles.ini"
echo Default=Profiles/%RNDStr%.%profileName%>> "%~1\Mozilla\Firefox\profiles.ini"
echo Locked=^1>> "%~1\Mozilla\Firefox\profiles.ini"
echo.>> "%~1\Mozilla\Firefox\profiles.ini"
echo [Profile0]>> "%~1\Mozilla\Firefox\profiles.ini"
echo Name=%profileName%>> "%~1\Mozilla\Firefox\profiles.ini"
echo IsRelative=^1>> "%~1\Mozilla\Firefox\profiles.ini"
echo Path=Profiles/%RNDStr%.%profileName%>> "%~1\Mozilla\Firefox\profiles.ini"

:: Add prefs to any other Firefox profiles in all users

@echo OFF
for /f "usebackq delims=" %%B in (`dir /B /A:d "%~1\Mozilla\Firefox\Profiles" ^| findstr /v /x /c:"%RNDStr%.%profileName%"`) do (
	if exist "%~1\Mozilla\Firefox\Profiles\%%B\prefs.js" (
		:: Removes lines containing these entries from the profiles prefs.js. This way any old prefs don't overlap with the new prefs
		echo findstr /V /C:""""app.shield.optoutstudies.enabled"""" /C:""""browser.aboutwelcome.enabled"""" /C:""""browser.disableResetPrompt"""" /C:""""browser.newtabpage.activity-stream.asrouter.userprefs.cfr.addons"""" /C:""""browser.newtabpage.activity-stream.asrouter.userprefs.cfr.features"""" /C:""""browser.newtabpage.activity-stream.feeds.section.topstories"""" /C:""""browser.newtabpage.activity-stream.feeds.topsites"""" /C:""""browser.newtabpage.activity-stream.section.highlights.includePocket"""" /C:""""browser.newtabpage.activity-stream.section.highlights.includeVisited"""" /C:""""browser.newtabpage.activity-stream.showSponsored"""" /C:""""browser.newtabpage.activity-stream.showSponsoredTopSites"""" /C:""""browser.urlbar.placeholderNam"""" /C:""""browser.urlbar.suggest.quicksuggest.nonsponsored"""" /C:""""browser.urlbar.suggest.quicksuggest.sponsored"""" /C:""""browser.urlbar.suggest.topsites"""" /C:""""datareporting.healthreport.uploadEnabled"""" /C:""""dom.security.https_only_mode"""" /C:""""dom.security.https_only_mode_ever_enabled"""" "%~1\Mozilla\Firefox\Profiles\%%B\prefs.js "^>^> "%TEMP%\prefs.js.tmp"
		findstr /V /C:""""app.shield.optoutstudies.enabled"""" /C:""""browser.aboutwelcome.enabled"""" /C:""""browser.disableResetPrompt"""" /C:""""browser.newtabpage.activity-stream.asrouter.userprefs.cfr.addons"""" /C:""""browser.newtabpage.activity-stream.asrouter.userprefs.cfr.features"""" /C:""""browser.newtabpage.activity-stream.feeds.section.topstories"""" /C:""""browser.newtabpage.activity-stream.feeds.topsites"""" /C:""""browser.newtabpage.activity-stream.section.highlights.includePocket"""" /C:""""browser.newtabpage.activity-stream.section.highlights.includeVisited"""" /C:""""browser.newtabpage.activity-stream.showSponsored"""" /C:""""browser.newtabpage.activity-stream.showSponsoredTopSites"""" /C:""""browser.urlbar.placeholderNam"""" /C:""""browser.urlbar.suggest.quicksuggest.nonsponsored"""" /C:""""browser.urlbar.suggest.quicksuggest.sponsored"""" /C:""""browser.urlbar.suggest.topsites"""" /C:""""datareporting.healthreport.uploadEnabled"""" /C:""""dom.security.https_only_mode"""" /C:""""dom.security.https_only_mode_ever_enabled"""" "%~1\Mozilla\Firefox\Profiles\%%B\prefs.js ">> "%TEMP%\prefs.js.tmp"
	)
	:: Filters out a few prefs from AME-Firefox-Injectiont\prefs.js and adds them to prefs.js.tmp
	echo findstr /V /C:""""browser.toolbars.bookmarks.visibility"""" /C:""""extensions.webextensions.uuids"""" /C:""""extensions.webextensions.uuids"""" "%~dp0\AME-Firefox-Injection\prefs.js"^>^> "%TEMP%\prefs.js.tmp"
	findstr /V /C:""""browser.toolbars.bookmarks.visibility"""" /C:""""extensions.webextensions.uuids"""" /C:""""extensions.webextensions.uuids"""" "%~dp0\AME-Firefox-Injection\prefs.js">> "%TEMP%\prefs.js.tmp"
	echo move /y "%TEMP%\prefs.js.tmp" "%~1\Mozilla\Firefox\Profiles\%%B\prefs.js"
	move /y "%TEMP%\prefs.js.tmp" "%~1\Mozilla\Firefox\Profiles\%%B\prefs.js"
	if exist "%~1\Mozilla\Firefox\Profiles\%%B\search.json.mozlz4" echo del /Q /F "%~1\Mozilla\Firefox\Profiles\%%B\search.json.mozlz4" & del /Q /F "%~1\Mozilla\Firefox\Profiles\%%B\search.json.mozlz4"
	echo robocopy "%~dp0\AME-Firefox-Injection" "%~1\Mozilla\Firefox\Profiles\%%B" search.json.mozlz4 /E ^> NUL
	robocopy "%~dp0\AME-Firefox-Injection" "%~1\Mozilla\Firefox\Profiles\%%B" search.json.mozlz4 /E > NUL
	echo PowerShell -NoP -C "%~1\Mozilla\Firefox\Profiles' | Set-Acl '%~1\Mozilla\Firefox\Profiles\%%B\prefs.js'" ^> NUL 2^>^&1
	PowerShell -NoP -C "%~1\Mozilla\Firefox\Profiles' | Set-Acl '%~1\Mozilla\Firefox\Profiles\%%B\prefs.js'" > NUL 2>&1
	echo PowerShell -NoP -C "Get-Acl '%~1\Mozilla\Firefox\Profiles' | Set-Acl '%~1\Mozilla\Firefox\Profiles\%%B\search.json.mozlz4'" ^> NUL 2^>^&1
	PowerShell -NoP -C "Get-Acl '%~1\Mozilla\Firefox\Profiles' | Set-Acl '%~1\Mozilla\Firefox\Profiles\%%B\search.json.mozlz4'" > NUL 2>&1
)

echo. & echo Successfully injected custom Firefox configs.
endlocal & exit /b 0

:PREFSONLY

:: uBlock Origin could be installed automatically here, however that would involve overwriting the old extension.json etc
:: files, which, if the user had previous extensions, could cause issues.

@echo. & echo Injecting config files...

if exist "%TEMP%\prefs.js.tmp" del /q /f "%TEMP%\prefs.js.tmp" > NUL

set /a "count3=0"

@echo OFF
for /f "usebackq delims=" %%A in (`dir /B /A:d "%~1\Mozilla\Firefox\Profiles"`) do (
	set /a "count3=%count3%+1"
	if exist "%~1\Mozilla\Firefox\Profiles\%%A\prefs.js" (
		:: Removes lines containing these entries from the profiles prefs.js. This way any old prefs don't overlap with the new prefs
		echo findstr /V /C:""""app.shield.optoutstudies.enabled"""" /C:""""browser.aboutwelcome.enabled"""" /C:""""browser.disableResetPrompt"""" /C:""""browser.newtabpage.activity-stream.asrouter.userprefs.cfr.addons"""" /C:""""browser.newtabpage.activity-stream.asrouter.userprefs.cfr.features"""" /C:""""browser.newtabpage.activity-stream.feeds.section.topstories"""" /C:""""browser.newtabpage.activity-stream.feeds.topsites"""" /C:""""browser.newtabpage.activity-stream.section.highlights.includePocket"""" /C:""""browser.newtabpage.activity-stream.section.highlights.includeVisited"""" /C:""""browser.newtabpage.activity-stream.showSponsored"""" /C:""""browser.newtabpage.activity-stream.showSponsoredTopSites"""" /C:""""browser.urlbar.placeholderNam"""" /C:""""browser.urlbar.suggest.quicksuggest.nonsponsored"""" /C:""""browser.urlbar.suggest.quicksuggest.sponsored"""" /C:""""browser.urlbar.suggest.topsites"""" /C:""""datareporting.healthreport.uploadEnabled"""" /C:""""dom.security.https_only_mode"""" /C:""""dom.security.https_only_mode_ever_enabled"""" "%~1\Mozilla\Firefox\Profiles\%%A\prefs.js "^>^> "%TEMP%\prefs.js.tmp"
		findstr /V /C:""""app.shield.optoutstudies.enabled"""" /C:""""browser.aboutwelcome.enabled"""" /C:""""browser.disableResetPrompt"""" /C:""""browser.newtabpage.activity-stream.asrouter.userprefs.cfr.addons"""" /C:""""browser.newtabpage.activity-stream.asrouter.userprefs.cfr.features"""" /C:""""browser.newtabpage.activity-stream.feeds.section.topstories"""" /C:""""browser.newtabpage.activity-stream.feeds.topsites"""" /C:""""browser.newtabpage.activity-stream.section.highlights.includePocket"""" /C:""""browser.newtabpage.activity-stream.section.highlights.includeVisited"""" /C:""""browser.newtabpage.activity-stream.showSponsored"""" /C:""""browser.newtabpage.activity-stream.showSponsoredTopSites"""" /C:""""browser.urlbar.placeholderNam"""" /C:""""browser.urlbar.suggest.quicksuggest.nonsponsored"""" /C:""""browser.urlbar.suggest.quicksuggest.sponsored"""" /C:""""browser.urlbar.suggest.topsites"""" /C:""""datareporting.healthreport.uploadEnabled"""" /C:""""dom.security.https_only_mode"""" /C:""""dom.security.https_only_mode_ever_enabled"""" "%~1\Mozilla\Firefox\Profiles\%%A\prefs.js ">> "%TEMP%\prefs.js.tmp"
	)
	:: Filters out a few prefs from AME-Firefox-Injectiont\prefs.js and adds them to prefs.js.tmp
	echo findstr /V /C:""""browser.toolbars.bookmarks.visibility"""" /C:""""extensions.webextensions.uuids"""" /C:""""extensions.webextensions.uuids"""" "%~dp0\AME-Firefox-Injection\prefs.js"^>^> "%TEMP%\prefs.js.tmp"
	findstr /V /C:""""browser.toolbars.bookmarks.visibility"""" /C:""""extensions.webextensions.uuids"""" /C:""""extensions.webextensions.uuids"""" "%~dp0\AME-Firefox-Injection\prefs.js">> "%TEMP%\prefs.js.tmp"
	echo move /y "%TEMP%\prefs.js.tmp" "%~1\Mozilla\Firefox\Profiles\%%A\prefs.js"
	move /y "%TEMP%\prefs.js.tmp" "%~1\Mozilla\Firefox\Profiles\%%A\prefs.js"
	if exist "%~1\Mozilla\Firefox\Profiles\%%A\search.json.mozlz4" echo del /Q /F "%~1\Mozilla\Firefox\Profiles\%%A\search.json.mozlz4" & del /Q /F "%~1\Mozilla\Firefox\Profiles\%%A\search.json.mozlz4"
	echo robocopy "%~dp0\AME-Firefox-Injection" "%~1\Mozilla\Firefox\Profiles\%%A" search.json.mozlz4 /E ^> NUL
	robocopy "%~dp0\AME-Firefox-Injection" "%~1\Mozilla\Firefox\Profiles\%%A" search.json.mozlz4 /E > NUL
)
for /f "usebackq delims=" %%A in (`dir /B /A:d "%SYSTEMDRIVE%\Users"`) do (
	if exist "%~1\Mozilla\Firefox\Profiles" (
		for /f "usebackq delims=" %%B in (`dir /B /A:d "%~1\Mozilla\Firefox\Profiles"`) do (
			set /a "count3=%count3%+1"
			if exist "%~1\Mozilla\Firefox\Profiles\%%B\prefs.js" (
				:: Removes lines containing these entries from the profiles prefs.js. This way any old prefs don't overlap with the new prefs
				echo findstr /V /C:""""app.shield.optoutstudies.enabled"""" /C:""""browser.aboutwelcome.enabled"""" /C:""""browser.disableResetPrompt"""" /C:""""browser.newtabpage.activity-stream.asrouter.userprefs.cfr.addons"""" /C:""""browser.newtabpage.activity-stream.asrouter.userprefs.cfr.features"""" /C:""""browser.newtabpage.activity-stream.feeds.section.topstories"""" /C:""""browser.newtabpage.activity-stream.feeds.topsites"""" /C:""""browser.newtabpage.activity-stream.section.highlights.includePocket"""" /C:""""browser.newtabpage.activity-stream.section.highlights.includeVisited"""" /C:""""browser.newtabpage.activity-stream.showSponsored"""" /C:""""browser.newtabpage.activity-stream.showSponsoredTopSites"""" /C:""""browser.urlbar.placeholderNam"""" /C:""""browser.urlbar.suggest.quicksuggest.nonsponsored"""" /C:""""browser.urlbar.suggest.quicksuggest.sponsored"""" /C:""""browser.urlbar.suggest.topsites"""" /C:""""datareporting.healthreport.uploadEnabled"""" /C:""""dom.security.https_only_mode"""" /C:""""dom.security.https_only_mode_ever_enabled"""" "%~1\Mozilla\Firefox\Profiles\%%B\prefs.js "^>^> "%TEMP%\prefs.js.tmp"
				findstr /V /C:""""app.shield.optoutstudies.enabled"""" /C:""""browser.aboutwelcome.enabled"""" /C:""""browser.disableResetPrompt"""" /C:""""browser.newtabpage.activity-stream.asrouter.userprefs.cfr.addons"""" /C:""""browser.newtabpage.activity-stream.asrouter.userprefs.cfr.features"""" /C:""""browser.newtabpage.activity-stream.feeds.section.topstories"""" /C:""""browser.newtabpage.activity-stream.feeds.topsites"""" /C:""""browser.newtabpage.activity-stream.section.highlights.includePocket"""" /C:""""browser.newtabpage.activity-stream.section.highlights.includeVisited"""" /C:""""browser.newtabpage.activity-stream.showSponsored"""" /C:""""browser.newtabpage.activity-stream.showSponsoredTopSites"""" /C:""""browser.urlbar.placeholderNam"""" /C:""""browser.urlbar.suggest.quicksuggest.nonsponsored"""" /C:""""browser.urlbar.suggest.quicksuggest.sponsored"""" /C:""""browser.urlbar.suggest.topsites"""" /C:""""datareporting.healthreport.uploadEnabled"""" /C:""""dom.security.https_only_mode"""" /C:""""dom.security.https_only_mode_ever_enabled"""" "%~1\Mozilla\Firefox\Profiles\%%B\prefs.js ">> "%TEMP%\prefs.js.tmp"
			)
			:: Filters out a few prefs from AME-Firefox-Injectiont\prefs.js and adds them to prefs.js.tmp
			echo findstr /V /C:""""browser.toolbars.bookmarks.visibility"""" /C:""""extensions.webextensions.uuids"""" /C:""""extensions.webextensions.uuids"""" "%~dp0\AME-Firefox-Injection\prefs.js"^>^> "%TEMP%\prefs.js.tmp"
			findstr /V /C:""""browser.toolbars.bookmarks.visibility"""" /C:""""extensions.webextensions.uuids"""" /C:""""extensions.webextensions.uuids"""" "%~dp0\AME-Firefox-Injection\prefs.js">> "%TEMP%\prefs.js.tmp"
			echo move /y "%TEMP%\prefs.js.tmp" "%~1\Mozilla\Firefox\Profiles\%%B\prefs.js"
			move /y "%TEMP%\prefs.js.tmp" "%~1\Mozilla\Firefox\Profiles\%%B\prefs.js"
			if exist "%~1\Mozilla\Firefox\Profiles\%%B\search.json.mozlz4" echo del /Q /F "%~1\Mozilla\Firefox\Profiles\%%B\search.json.mozlz4" & del /Q /F "%~1\Mozilla\Firefox\Profiles\%%B\search.json.mozlz4"
			echo robocopy "%~dp0\AME-Firefox-Injection" "%~1\Mozilla\Firefox\Profiles\%%B" search.json.mozlz4 /E ^> NUL
			robocopy "%~dp0\AME-Firefox-Injection" "%~1\Mozilla\Firefox\Profiles\%%B" search.json.mozlz4 /E > NUL
			echo PowerShell -NoP -C "Get-Acl '%~1\Mozilla\Firefox\Profiles' | Set-Acl '%~1\Mozilla\Firefox\Profiles\%%B\prefs.js'" ^> NUL 2^>^&1
			PowerShell -NoP -C "Get-Acl '%~1\Mozilla\Firefox\Profiles' | Set-Acl '%~1\Mozilla\Firefox\Profiles\%%B\prefs.js'" > NUL 2>&1
			echo PowerShell -NoP -C "Get-Acl '%~1\Mozilla\Firefox\Profiles' | Set-Acl '%~1\Mozilla\Firefox\Profiles\%%B\search.json.mozlz4'" ^> NUL 2^>^&1
			PowerShell -NoP -C "Get-Acl '%~1\Mozilla\Firefox\Profiles' | Set-Acl '%~1\Mozilla\Firefox\Profiles\%%B\search.json.mozlz4'" > NUL 2>&1
		)
	)
)
if %count3% EQU 0 (
	echo. & echo Failed! No profiles detected
	endlocal & exit /b 2
) else (
	echo. & echo Successfully injected custom Firefox configs.
	endlocal & exit /b 1
)