

SET database=%1
SET server=%2
if "%3" == "NT" goto NTOptions
SET userflags=-U %4 -P %5
SET bcpflags=-U %4 -P %5
goto start

:NTOptions
SET bcpflags=-T
SET userflags=-E


:start

@echo.
@echo. Dropping previous DataBase and user
@echo.

osql -S %server% %userflags% -b -Q "if exists (SELECT name FROM master.dbo.sysdatabases WHERE name = N'%database%') drop database [%database%]"
osql -S %server% %userflags% -b -Q "sp_droplogin '%database%_login'"


@echo.
@echo. Creating DataBase...
@echo.


osql -S %server% %userflags% -b -Q "CREATE DATABASE %database% 
if "%ERRORLEVEL%" == "1" goto CreateError

@echo.
@echo. Creating User...
@echo.

osql -S %server% -d %database% -b -Q"if not exists (select * from master..syslogins where name = '%database%_login')  exec sp_addlogin '%database%_login', '%6', '%database%'"  %userflags%
osql -S %server% -d %database% -b -Q"sp_grantdbaccess '%database%_login', '%database%_login'"  %userflags%
osql -S %server% -d %database% -b -Q"sp_addrolemember 'db_owner', '%database%_login'"  %userflags%

@echo.
@echo. Creating Tables...
@echo.

osql -S %server% -d %database% -n -i sbwTable.sql %userflags%


@echo.
@echo. Creating View...
@echo.
osql -S %server% -d %database% -n -i sbwView.sql %userflags%


@echo.
@echo. Creating Stored Procedures...
@echo.
osql -S %server% -d %database% -n -i sbwsprocs.sql %userflags%
osql -S %server% -d master -n -i masterprocs.sql %userflags%





@echo.
@echo. Completed!
@echo.
