@if "%_echo%" == "" echo off

setlocal 

call setdemovars.cmd

IF DEFINED VS80COMNTOOLS (
	call "%VS80COMNTOOLS%vsvars32.bat"
) ELSE (
	echo VS80COMNTOOLS environment variable wasn't set. Visual Studio 8.0 may not be installed. 
	echo VS80COMNTOOLS must be set to the location of the vsvars32.bat script.
	goto :errorexit
)

call cleanupdemo.cmd

REM Create the NSService user
echo Creating the NS Service user
net user %NSSERVICE_USERNAME% %NSSERVICE_PASSWORD% /ADD

REM Add the NSService user to the Administrators group (needed for SMTP delivery).
echo Adding the NSService user to the Administrators group (needed for SMTP delivery)
net localgroup Administrators NSTestUser /ADD

REM Grant permissions to notifications output directory
echo Granting permissions to notifications output directory
cacls Output\Notifications /T /E /G %COMPUTERNAME%\NSTestUser:F

REM Rebuild all the VS solutions on which the demo depends.
MSBuild BootstrapDemo\BootstrapDemo.sln /t:Rebuild 
if errorlevel 1 goto :errorexit

MSBuild RulesControls\RulesControls.csproj /t:Rebuild
if errorlevel 1 goto :errorexit

MSBuild RulesWebControls\RulesWebControls\RulesWebControls.csproj /t:Rebuild
if errorlevel 1 goto :errorexit

MSBuild SubscriptionBuilder\SubscriptionBuilder.sln /t:Rebuild
if errorlevel 1 goto :errorexit

REM Copy the RulesWebControls bits into the MusicStoreWeb bin directory
copy /y RulesWebControls\RulesWebControls\bin\debug\Microsoft.SqlServer.NotificationServices.Rules.Web.* MusicStoreWeb\Bin


REM Create Server Logins
echo Creating service logins...
SET LOGIN=%ASPNET_USER%
SET QUERY=IF NOT EXISTS (SELECT * FROM syslogins WHERE name LIKE '%LOGIN%') CREATE LOGIN [%LOGIN%] FROM WINDOWS
echo Executing "%QUERY%"
sqlcmd -S %SQLINSTANCE% -d master -E -Q "%QUERY%"

SET LOGIN=%NSSERVICE_QUALIFIED_USERNAME%
SET QUERY=IF NOT EXISTS (SELECT * FROM syslogins WHERE name LIKE '%LOGIN%') CREATE LOGIN [%LOGIN%] FROM WINDOWS
echo Executing "%QUERY%"
sqlcmd -S %SQLINSTANCE% -d master -E -Q "%QUERY%"

SET LOGIN=%LOWPRIVEVAL_USER%
SET QUERY=IF NOT EXISTS (SELECT * FROM syslogins WHERE name LIKE '%LOGIN%') CREATE LOGIN [%LOGIN%] WITH PASSWORD='%LOWPRIVEVAL_PASSWORD%'
echo Executing "%QUERY%"
sqlcmd -S %SQLINSTANCE% -d master -E -Q "%QUERY%"


REM Create the database
echo Creating the music store database (expect some warning messages from ASP.NET generated code)
sqlcmd -S %SQLINSTANCE% -d master -E -i CreateMusicStore.sql

REM Create the database users
echo Creating database users...

SET UNAME=%ASPNET_USER%
SET QUERY=CREATE USER [%UNAME%]
echo Executing "%QUERY%"
sqlcmd -S %SQLINSTANCE% -d MusicStore -E -Q "%QUERY%"

SET UNAME=%NSSERVICE_QUALIFIED_USERNAME%
SET QUERY=CREATE USER [%UNAME%]
echo Executing "%QUERY%"
sqlcmd -S %SQLINSTANCE% -d MusicStore -E -Q "%QUERY%"

SET UNAME=%LOWPRIVEVAL_USER%
SET QUERY=CREATE USER [%UNAME%] FOR LOGIN [%LOWPRIVEVAL_USER%]
echo Executing "%QUERY%"
sqlcmd -S %SQLINSTANCE% -d MusicStore -E -Q "%QUERY%"


REM Populate the music store database
echo Populating the music store database.
sqlcmd -S %SQLINSTANCE% -d master -E -i PopulateMusicStore.sql

REM Bootstrapping demo database with admin user account.
BootstrapDemo\bin\Debug\BootStrapDemo.exe /nowait

echo Successfully reset demo.
endlocal
exit /b 0

:errorexit
endlocal
exit /b 1