@echo off

SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION

SET ROOTDIR=%cd:\metro\install=%
SET EXENAME=firefox.exe

ECHO Working directory we'll be using: '%cd%'
ECHO Install directory we'll be using: '%ROOTDIR%'
ECHO Executable we'll be using: '%EXENAME%'

ECHO Parsing registration scripts...

SET DISTBIN=%ROOTDIR:\=\\%
ECHO Generating zipbuildregisterbrowser.reg
CALL :ParseFileAndReplace @DISTBIN@ %DISTBIN% %cd%\registerdefaultbrowser.reg.template > tmp.txt
CALL :ParseFileAndReplace @EXE_NAME@ %EXENAME% %cd%\tmp.txt > registerbrowser.reg
DEL tmp.txt
ECHO Generating zipbuildcreateshortcut
CALL :ParseFileAndReplace @DISTBIN@ %ROOTDIR% %cd%\createshortcut.bat.template > tmp.txt
CALL :ParseFileAndReplace @EXE_NAME@ %EXENAME% %cd%\tmp.txt > createshortcut.bat
DEL tmp.txt

ECHO Done parsing registration scripts.
ECHO To install, import registerbrowser.reg and run createshortcut.bat as Administrator.
PAUSE

goto :eof

::syntax: ParseFileAndReplace OldStr NewStr File
::          OldStr [in] - string to be replaced
::          NewStr [in] - string to replace with
::          File   [in] - file to be parsed
:ParseFileAndReplace
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:%~1=%~2%%"
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
    ) ELSE echo.
)
goto :eof
