@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
)

REM Stop the NS Service
echo Stopping the NS Service (may fail if the service doesn't exist or isn't started)
net stop ns$MusicStore

REM Unregister the NS Instance
echo Unregistering the NS Instance (may fail if the instance isn't registered)
nscontrol unregister -n MusicStore

REM Delete the NS instance
echo Deleting the NS Instance (may fail if the instance doesn't exist)
nscontrol delete -n MusicStore -S %SQLINSTANCE% -force

REM Drop the database
echo Dropping the database (may fail if the database doesn't exist)
sqlcmd -S %SQLINSTANCE% -d master -E -i DropMusicStore.sql

REM Stop the SMTP service
echo Stopping the SMTP Service (may fail if the service isn't started)
net stop SMTPSvc

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

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

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

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

REM Remove the RulesWebControls bits from the MusicStoreWeb bin directory
del /q MusicStoreWeb\Bin\Microsoft.SqlServer.NotificationServices.Rules.Web.*

REM Restore the source files back to their original state.
echo Restoring source files to their original state.
copy IntermediateFiles\InstanceConfiguration-0.xml InstanceConfiguration.xml
copy IntermediateFiles\ApplicationDefinition-0.xml ApplicationDefinition.xml
del /q MusicStoreWeb\images\*.*
copy /Y PreLoadedCoverArt\*.* MusicStoreWeb\images

REM Remove any items in mail queue.
echo Removing existing items from mail queue.
del /q %SMTPQUEUE%\*.*

REM Blank out the notifications output file (copy over a blank file)
echo Blanking out the notifications output file
copy Output\Notifications\blank.txt Output\Notifications\FileNotifications.txt

REM Remove the NS Service user from the Administrators group and delete the user account.
echo Removing the NS Service user from the Administrators group and deleting the user account
net localgroup Administrators NSTestUser /DELETE
net user %NSSERVICE_USERNAME% /DELETE

echo Successfully cleaned up demo.
endlocal
exit /b 0

:errorexit
endlocal
exit /b 1