SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

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


create procedure dbo.adminAddProject
	@pProjectName varchar(30),
	@pVssShare varchar(255)
as
/*************************************************************************
Create the specified project SQL Server metadatabase if it exists in 
SourceSafe and this user has access to SourceSafe.
Note This procedure does not create the project and environment 
sub-projects in the specified SourceSafe Share. Only the SourceSafe Admin 
should have ability to create these SourceSafe Projects. All other users 
should have Read Only permissions to the project and to all Environment
projects other than the environment project to which they belong. In that
environment project, the SourceSafe Admin will grant "Add/Rename/Delete"
permissions before this Change Control subsystem can be used by that user.  
**************************************************************************/
declare @err int,
	@cmd nvarchar(1200),
	@returnstatus int,
	@procedureName varchar(100),
	@vssHierarchyRoot varchar(255),
	@vssUserName varchar(30)

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

set @vssHierarchyRoot = '$/' + @pProjectName + '/Database/'

-- make sure current node has a valid SourceSafe user
set @vssUserName = substring(system_user,charindex('\', system_user) + 1, datalength(system_user))

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

-- only verify an ini file for this user (vss 6.0 stores in 8.3 format)
-- any vss command from invalid user will try to throw a login prompt
set @cmd = 'dir ' + @pVssShare + 'users\' + substring(@vssUserName,1,8) + '\ss.ini' 
exec @returnstatus = master.dbo.xp_cmdshell @cmd, no_output
if @returnstatus <> 0 
	begin
		raiserror (59005,16,1, @procedurename, 	@vssUserName, @pVssShare)
		goto ErrorHandler
	end

-- verify the hierarchy
set @cmd = @pVssShare + 'win32\ss Cp ' 
         + @vssHierarchyRoot + ' -I-N -Y' + @vssUserName 
exec @returnstatus = master.dbo.xp_cmdshell @cmd, no_output
if @returnstatus <> 0 
	begin		
		raiserror (59008, 16, 1, @procedureName, @vssHierarchyRoot, @pVssShare)
		goto ErrorHandler
	end

-- populate project
insert Admin.dbo.Project (projectName, vssShare, vssHierarchyRoot)
values (@pProjectName, @pVssShare, @vssHierarchyRoot)
set @err = @@error
if @err <> 0
	goto ErrorHandler

-- audit
insert Admin.dbo.ActivityLog (activity)
select @procedureName + ' ' + @pProjectName + ' ' + @pVssShare + ' complete successfully'

return

ErrorHandler:
if @err is not null
	begin
		insert Admin.dbo.ActivityLog (activity)
		select @procedureName + ' ' + @pProjectName + ' ' + @pVssShare 
			+ ' failed with err: ' + cast(@err as varchar(11)) 
		raiserror(59001,16,1,@procedureName)
	end
else
	raiserror(59000,16,1,@procedureName)

return -1


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

