@echo off
setlocal

REM These are all the defaults for this script that can be changed.
REM
set "INIFILE=_setup.ini"
set "COPYCMD=xcopy /siq"
set "SRCROOT=%~dp0"

set "INSTALL_REG_FILE=_assessment_inst_.reg"
set "UNINSTALL_FILE=uninstall.cmd"

REM Make this work for both x86 and AMD
REM
set "PROGS=%ProgramFiles(x86)%"
if "%PROGS%"=="" set "PROGS=%ProgramFiles%"

REM Process the ini file which defines variables we will use.
REM
set NAME=
set PROGNAME=
set FOLDER=
set VERSION=
set SUBDIR=
for /f "eol=; tokens=1-2 usebackq delims==" %%a in ("%SRCROOT%%INIFILE%") do set "%%a=%%b"
if "%NAME%"=="" goto :ERR_MISSINGVAR
if "%PROGNAME%"=="" goto :ERR_MISSINGVAR
if "%VERSION%"=="" goto :ERR_MISSINGVAR

set "ASMT_SRCROOT=%SRCROOT:~0,-1%"
if not "%SUBDIR%"=="" set "ASMT_SRCROOT=%ASMT_SRCROOT%\%SUBDIR%"

set "DSTROOT=%SRCROOT%"
if not "%FOLDER%"=="" set "DSTROOT=%PROGS%\%FOLDER%\"
set "ASMT_DSTROOT=%DSTROOT:~0,-1%"
if not "%SUBDIR%"=="" set "ASMT_DSTROOT=%ASMT_DSTROOT%\%SUBDIR%"

REM Need to find regasmt in the ADK.
REM
set "REGASMT=%SRCROOT%..\..\x86\regasmt.exe"
if exist "%REGASMT%" goto :REGASMT_FOUND
echo Searching for the installed ADK folder under "%PROGS%"...
for /r "%PROGS%" %%f in (regasmt.exe*) do set "REGASMT=%%f"
if not exist "%REGASMT%" goto :ERR_NOADK
:REGASMT_FOUND

REM Setup the default installation folder.
REM
if "%DSTROOT%"=="%SRCROOT%" goto :DONEDEST
if exist "%DSTROOT%" goto :ERR_DSTEXISTS
echo Installing to "%DSTROOT%"

REM Need to create the destination folder.
REM
echo md "%DSTROOT%"
md "%DSTROOT%"
if ERRORLEVEL 1 goto :ERR_MAKEDST
if "%DSTROOT%"=="%ASMT_DSTROOT%\" goto :DONEDEST
md "%ASMT_DSTROOT%"
if ERRORLEVEL 1 goto :ERR_MAKEDST2
:DONEDEST


REM Create the start of the uninstall.cmd file in the destination folder.
REM

REM Create the start of the %UNINSTALL_FILE% file in the destination folder.
REM
echo @echo off>"%DSTROOT%%UNINSTALL_FILE%"
echo echo Uninstalling "%NAME%"...>>"%DSTROOT%%UNINSTALL_FILE%"
echo echo.>>"%DSTROOT%%UNINSTALL_FILE%"

REM Install all the assessments.
REM
for /d %%d in ("%ASMT_SRCROOT%\*") do call :COPY_ASMT "%%d"

REM Create the .reg file in the temp folder to add the uninstall key.
REM
set "DSTROOT_DBLBS=%DSTROOT:\=\\%"
echo Windows Registry Editor Version 5.00>"%TEMP%\%INSTALL_REG_FILE%"
echo.>>"%TEMP%\%INSTALL_REG_FILE%"
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%PROGNAME%]>>"%TEMP%\%INSTALL_REG_FILE%"
echo "DisplayName"="%NAME%">>"%TEMP%\%INSTALL_REG_FILE%"
echo "DisplayVersion"="%VERSION%">>"%TEMP%\%INSTALL_REG_FILE%"
echo "InstallLocation"="%DSTROOT_DBLBS%">>"%TEMP%\%INSTALL_REG_FILE%"
echo "Publisher"="Microsoft Corporation">>"%TEMP%\%INSTALL_REG_FILE%"
echo "UninstallString"="%DSTROOT_DBLBS%%UNINSTALL_FILE%">>"%TEMP%\%INSTALL_REG_FILE%"
regedit /s "%TEMP%\%INSTALL_REG_FILE%"
REM if ERRORLEVEL 1 
del "%TEMP%\%INSTALL_REG_FILE%"

REM Finalize the uninstall command.
REM
echo echo Windows Registry Editor Version 5.00^>"%%TEMP%%\%INSTALL_REG_FILE%">>"%DSTROOT%%UNINSTALL_FILE%"
echo echo.^>^>"%%TEMP%%\%INSTALL_REG_FILE%">>"%DSTROOT%%UNINSTALL_FILE%"
echo echo [-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%PROGNAME%]^>^>"%%TEMP%%\%INSTALL_REG_FILE%">>"%DSTROOT%%UNINSTALL_FILE%"
echo regedit /s "%%TEMP%%\%INSTALL_REG_FILE%">>"%DSTROOT%%UNINSTALL_FILE%"
echo del "%%TEMP%%\%INSTALL_REG_FILE%">>"%DSTROOT%%UNINSTALL_FILE%"
echo echo.>>"%DSTROOT%%UNINSTALL_FILE%"
if "%ASMT_DST%"=="%ASMT_SRC%" goto :LEAVEDIR
echo echo About to completely remove folder "%DSTROOT%">>"%DSTROOT%%UNINSTALL_FILE%"
echo pause>>"%DSTROOT%%UNINSTALL_FILE%"
echo rd /q/s "%DSTROOT%">>"%DSTROOT%%UNINSTALL_FILE%"
goto :DONE
:LEAVEDIR
echo echo Uninstallation Complete!>>"%DSTROOT%%UNINSTALL_FILE%"
echo pause>>"%DSTROOT%%UNINSTALL_FILE%"
echo del "%%~f0">>"%DSTROOT%%UNINSTALL_FILE%"
goto :DONE

:DONE
REM Done!
REM
echo.
echo Installation Complete!
goto :DONE


:COPY_ASMT
set "ASMT_NAME=%~nx1"
set "ASMT_SRC=%ASMT_SRCROOT%\%ASMT_NAME%"
set "ASMT_DST=%ASMT_DSTROOT%\%ASMT_NAME%"
echo.
echo Installing "%ASMT_NAME%"...
if "%ASMT_DST%"=="%ASMT_SRC%" goto :NOCOPY
REM echo %COPYCMD% "%ASMT_SRC%\*.*" "%ASMT_DST%\"
%COPYCMD% "%ASMT_SRC%\*.*" "%ASMT_DST%\">NUL
:NOCOPY
for %%f in ("%ASMT_DST%\*.asmtx") do call :REG_ASMT "%%f"
goto :EOF

:REG_ASMT
"%REGASMT%" "%~1"
echo "%REGASMT%" /u "%~1">>"%DSTROOT%%UNINSTALL_FILE%"
goto :EOF


:ERR_MISSINGVAR
echo The source folder is missing required information (see setup script for more details).
goto :DONE

:ERR_DSTEXISTS
echo The installation folder "%DSTROOT%" already exists.  Uninstall any existing instance before installing again.
goto :DONE

:ERR_MAKEDST
echo Unable to create "%DSTROOT%" installation folder.  Ensure you are running this script as administrator.
goto :DONE

:ERR_MAKEDST2
echo Unable to create "%ASMT_DSTROOT%" installation folder.  Ensure you are running this script as administrator.
goto :DONE

:ERR_NOADK
echo Unable to locate the installed ADK under the "%PROGS%" folder.
goto :DONE


:DONE
echo.
pause
goto :EOF