SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[vssAdminInitProject]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[vssAdminInitProject]
GO


create procedure dbo.vssAdminInitProject
	@pProjectName varchar(30),
	@pVssShare varchar(255),
	@pVssAdminName varchar(30),
	@pVssAdminPassword varchar(30)
as
/*****************************************************************************
Populate the SQL Server metadatabase entries for the specified node, add the 
Root SourceSafe project hierarchy level and the database level
******************************************************************************/
declare @cmd nvarchar(1200),
	@returnstatus int,
	@procedureName varchar(100)

set nocount on

set @procedureName = db_name() + '.' 
		+ user_name(objectproperty(@@procid,'OwnerId')) 
		+ '.' + object_name(@@procid)

if (substring(@pVssShare,datalength(@pVssShare), 1) <> '\')
	set @pVssShare = @pVssShare + '\'

set @cmd = @pVssShare + 'win32\ss Cp ' 
         + '$/ -I-N -Y' + @pVssAdminName + ',' + @pVssAdminPassword 
exec @returnstatus = master.dbo.xp_cmdshell @cmd, no_output
if @returnstatus <> 0 
	goto ErrorHandler

-- add SourceSafe Project Hierarchy root 
-- even objects and subprojects outside the database can live within this project 
set @cmd = @pVssShare + 'win32\ss Cp ' 
         + @pProjectName + ' -I-N -Y' + @pVssAdminName + ',' + @pVssAdminPassword 
exec @returnstatus = master.dbo.xp_cmdshell @cmd, no_output
if @returnstatus <> 0 
	begin
		set @Cmd = @pVssShare + 'win32\ss Create ' 
			+ ' -C' + @@servername + '.' + @procedureName + ' '
			+ @pProjectName + ' -I-N -Y' + @pVssAdminName + ',' + @pVssAdminPassword 
		exec @returnstatus = master.dbo.xp_cmdshell @cmd--, no_output
		if @returnstatus <> 0 
			goto ErrorHandler

		set @cmd = @pVssShare + 'win32\ss Cp ' 
		         + @pProjectName + ' -I-N -Y' + @pVssAdminName + ',' + @pVssAdminPassword 
		exec @returnstatus = master.dbo.xp_cmdshell @cmd--, no_output
		if @returnstatus <> 0 
			goto ErrorHandler
	end


-- add database level
set @cmd = @pVssShare + 'win32\ss Cp ' 
         + 'Database/ -I-N -Y' + @pVssAdminName + ',' + @pVssAdminPassword  
exec @returnstatus = master.dbo.xp_cmdshell @cmd, no_output
if @returnstatus <> 0 
	begin
		set @Cmd = @pVssShare + 'win32\ss Create ' 
			+ ' -C' + @@servername + '.' + @procedureName + ' ' +
			'Database/ -I-N -Y' + @pVssAdminName + ',' + @pVssAdminPassword  
		exec @returnstatus = master.dbo.xp_cmdshell @cmd--, no_output
		if @returnstatus <> 0 
			goto ErrorHandler
	
		set @cmd = @pVssShare + 'win32\ss Cp ' 
		         + 'Database/ -I-N -Y' + @pVssAdminName + ',' + @pVssAdminPassword 
		exec @returnstatus = master.dbo.xp_cmdshell @cmd--, no_output
		if @returnstatus <> 0 
			goto ErrorHandler
	end

-- add environments
set @cmd = @pVssShare + 'win32\ss Cp ' 
         + 'Unit/ -I-N -Y' + @pVssAdminName + ',' + @pVssAdminPassword  
exec @returnstatus = master.dbo.xp_cmdshell @cmd, no_output
if @returnstatus <> 0 
	begin
		set @Cmd = @pVssShare + 'win32\ss Create ' 
			+ ' -C' + @@servername + '.' + @procedureName + ' ' +
			'Unit/ -I-N -Y' + @pVssAdminName + ',' + @pVssAdminPassword  
		exec @returnstatus = master.dbo.xp_cmdshell @cmd--, no_output
		if @returnstatus <> 0 
			goto ErrorHandler
	
	end

-- now add database level
set @cmd = @pVssShare + 'win32\ss Cp ' 
         + 'System/ -I-N -Y' + @pVssAdminName + ',' + @pVssAdminPassword  
exec @returnstatus = master.dbo.xp_cmdshell @cmd, no_output
if @returnstatus <> 0 
	begin
		set @Cmd = @pVssShare + 'win32\ss Create ' 
			+ ' -C' + @@servername + '.' + @procedureName + ' ' +
			'System/ -I-N -Y' + @pVssAdminName + ',' + @pVssAdminPassword  
		exec @returnstatus = master.dbo.xp_cmdshell @cmd--, no_output
		if @returnstatus <> 0 
			goto ErrorHandler
	
	end

-- now add database level
set @cmd = @pVssShare + 'win32\ss Cp ' 
         + 'QA/ -I-N -Y' + @pVssAdminName + ',' + @pVssAdminPassword  
exec @returnstatus = master.dbo.xp_cmdshell @cmd, no_output
if @returnstatus <> 0 
	begin
		set @Cmd = @pVssShare + 'win32\ss Create ' 
			+ ' -C' + @@servername + '.' + @procedureName + ' ' +
			'QA/ -I-N -Y' + @pVssAdminName + ',' + @pVssAdminPassword  
		exec @returnstatus = master.dbo.xp_cmdshell @cmd--, no_output
		if @returnstatus <> 0 
			goto ErrorHandler	
	end

-- now add database level
set @cmd = @pVssShare + 'win32\ss Cp ' 
         + 'Production/ -I-N -Y' + @pVssAdminName + ',' + @pVssAdminPassword  
exec @returnstatus = master.dbo.xp_cmdshell @cmd, no_output
if @returnstatus <> 0 
	begin
		set @Cmd = @pVssShare + 'win32\ss Create ' 
			+ ' -C' + @@servername + '.' + @procedureName + ' ' +
			'Production/ -I-N -Y' + @pVssAdminName + ',' + @pVssAdminPassword  
		exec @returnstatus = master.dbo.xp_cmdshell @cmd--, no_output
		if @returnstatus <> 0 
			goto ErrorHandler
	end

return

ErrorHandler:
insert Admin.dbo.ActivityLog (activity)
select @procedureName + ' ' + @pProjectName + ' failed with returnstatus ' 
	+ cast(@returnstatus as varchar(10)) + ' at: 
	' + @cmd
raiserror(59001,16,1,@procedureName)

return -1


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

