-- SETUP/RESTORE USERS IF OTHER DATABASES ARE PRESENT

USE [VIGIL-POS]
GO

IF EXISTS (SELECT * FROM sys.databases WHERE name=N'VIGIL-DVR')
BEGIN
	CREATE USER [vigil] FOR LOGIN [vigil]
	ALTER ROLE [db_owner] ADD MEMBER [vigil]

	EXEC [VIGIL-DVR]..sp_executesql N'IF EXISTS (SELECT * FROM sys.database_principals WHERE name = N''vpos'') DROP USER [vpos]'
	EXEC [VIGIL-DVR]..sp_executesql N'CREATE USER [vpos] FOR LOGIN [vpos]'
	EXEC [VIGIL-DVR]..sp_executesql N'ALTER ROLE [db_datareader] ADD MEMBER [vpos]'
	EXEC [VIGIL-DVR]..sp_executesql N'GRANT EXECUTE ON [dbo].[sp_insert_email] TO [vpos]'
	EXEC [VIGIL-DVR]..sp_executesql N'GRANT EXECUTE ON [dbo].[sp_insert_email_notification] TO [vpos]'
	EXEC [VIGIL-DVR]..sp_executesql N'GRANT EXECUTE ON [dbo].[sp_delete_vpos_email_notification] TO [vpos]'
	EXEC [VIGIL-DVR]..sp_executesql N'GRANT EXECUTE ON [dbo].[sp_save_alarm_event] TO [vpos]'
END
GO

IF EXISTS (SELECT * FROM sys.databases WHERE name=N'VIGIL-Audit')
BEGIN
	CREATE USER [audit] FOR LOGIN [audit]
	ALTER ROLE [db_datareader] ADD MEMBER [audit]
END
GO


-- MISC. FINAL ACTIONS

USE [master]
GO

ALTER DATABASE [VIGIL-POS] SET  READ_WRITE 
GO
