CCCPaste

open_in_crowbar_add

@echo off
setlocal enabledelayedexpansion
title Add Crowbar context menu entries (manual)

set default_extensions=mdl,qc

:check_elevation
net session >nul 2>&1
if not %errorlevel%==0 echo   & echo Error: Must run as administrator. & pause>nul & goto :eof

cd /d "%~dp0"

set foundcount=0

:check nearby
if exist "%~dp0crowbar.exe" ( CALL :LogFoundFolder "%~dp0Crowbar.exe" & goto :ConfirmFolder)

:hintcustomfolder
echo.
echo Please enter Crowbar folder
echo.
:AskCustomFolder
set installpath=
set /p installpath="Enter Crowbar.exe folder "
for %%P in (%installpath%) do goto :next
goto :AskCustomFolder
:next
set installpath=%installpath:"=%
if "%installpath:~-1%"=="\" set "installpath=%installpath:~0,-1%"
if "%installpath:~-1%"=="/" set "installpath=%installpath:~0,-1%"
for %%P in (%installpath%) do (
	if exist "%installpath%" (
	if not "%%~xP"=="" if /i "%%~nxP"=="Crowbar.exe" ( CALL :SetCustomFolder "%installpath%" & goto :ConfirmFolder)
	if exist "%installpath%\Crowbar.exe" ( CALL :SetCustomFolder "%installpath%\Crowbar.exe" & goto :ConfirmFolder)
	)
)
echo  & echo Error^: Could not find installation in provided folder. & echo. & goto :AskCustomFolder

:hintextensions
echo.
echo Please enter file extensions to add, divided by comma, lowercase, no dots or just press enter for default list
echo.
:askextensions
set /p extensions="What extensions to add for (default %default_extensions%): "
if not "!extensions!"=="" (
	set "extensions=!extensions: =!"
	for /f "tokens=1 delims=abcdefghijklmnopqrstuvwxyz1234567890," %%A in ('echo !extensions!') do (
		set "extensions=%default_extensions%"
		echo Warning: input incorrect, will use default setting ^(%default_extensions%^)
		pause
	)
) else ( set "extensions=%default_extensions%" )

:: exec for all extensions
for %%E in (%extensions%) do CALL :SetKeys %%E

:: results
echo.
echo -------------------------------------------------------
if "!processedcount!"=="" ( echo  No entries added ) else ( echo  !processedcount! entries added: !processed! )
echo -------------------------------------------------------
echo.
pause & goto :eof


:SetKeys
reg add "HKCR\SystemFileAssociations\.%1\shell\Crowbar.open" /f /v "" /t REG_SZ /d "Open in Crowbar">nul 2>&1
reg add "HKCR\SystemFileAssociations\.%1\shell\Crowbar.open" /f /v "Icon" /t REG_SZ /d "\"%installpath%\",0">nul 2>&1
reg add "HKCR\SystemFileAssociations\.%1\shell\Crowbar.open\command" /f /v "" /t REG_SZ /d "\"%installpath%\" \"%%1\"">nul 2>&1
if !errorlevel!==0 CALL :LogFileType %1
goto :eof

:LogFileType
if "!processedcount!"=="" ( set "processedcount=1" ) else ( set /a "processedcount=!processedcount!+1" ) 
if "!processed!"=="" ( set "processed=%1" ) else ( set "processed=!processed!,%1" )
goto :eof

:LogFoundFolder
echo.
echo Crowbar installation found: "%~1"
set "foundpath1=%~1"
goto :eof

:SetCustomFolder
echo.
echo Crowbar custom installation: "%~1" & echo.
set "foundpath1=%~1"
goto :eof

:ConfirmFolder
set proceed=
set /p "proceed=Use this installation (Y/N)?"
if not "!proceed!"=="" (
	set "proceed=!proceed: =!"
	for /f "delims=yYnN" %%A in ('echo !proceed!') do goto :ConfirmFolder
)
if /i !proceed!==y ( set "installpath=%foundpath1%" & goto :hintextensions)
if /i !proceed!==n ( echo. & goto :hintcustomfolder)
goto :ConfirmFolder