USE [msdb];
GO

IF NOT EXISTS (SELECT 1 FROM master.cfg.InstanceContext WHERE Business IN ('TPPHAR','TPCENT'))
BEGIN

    IF EXISTS (SELECT 1 FROM sys.databases WHERE name = 'distribution')
    BEGIN

        DECLARE @OperatorName sysname,
                @AlertName    sysname;

        SET @OperatorName = N'DBA_operator';

        IF NOT EXISTS (   SELECT name
                            FROM msdb.dbo.sysoperators
                           WHERE name = @OperatorName)
            EXEC msdb.dbo.sp_add_operator @name = @OperatorName,
                                          @enabled = 1,
                                          @weekday_pager_start_time = 90000,
                                          @weekday_pager_end_time = 180000,
                                          @saturday_pager_start_time = 90000,
                                          @saturday_pager_end_time = 180000,
                                          @sunday_pager_start_time = 90000,
                                          @sunday_pager_end_time = 180000,
                                          @pager_days = 0,
                                          @email_address = N'Gal_SE_DBA@galenica.com',
                                          @category_name = N'[Uncategorized]';

        SET @AlertName = 'Replication Warning: Transactional replication latency (Threshold: latency)';

        IF EXISTS (   SELECT name
                        FROM msdb.dbo.sysalerts
                       WHERE (   name             = @AlertName
                           AND   enabled          = 0)
                          OR (   name             = @AlertName
                           AND   has_notification = 0))
            EXEC msdb.dbo.sp_delete_alert @name = @AlertName;

        IF NOT EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = @AlertName)
            EXEC msdb.dbo.sp_add_alert @name = @AlertName,
                                       @message_id = 14161,
                                       @severity = 0,
                                       @enabled = 1,
                                       @delay_between_responses = 3600,
                                       @include_event_description_in = 5,
                                       @category_name = N'Replication',
                                       @job_id = N'00000000-0000-0000-0000-000000000000';

        IF NOT EXISTS (   SELECT      name
                            FROM      msdb.dbo.sysalerts AS sa
                           INNER JOIN msdb.dbo.sysnotifications AS sn
                              ON sa.id = sn.alert_id
                           WHERE      sa.name = @AlertName)
            EXEC msdb.dbo.sp_add_notification @alert_name = @AlertName,
                                              @operator_name = @OperatorName,
                                              @notification_method = 1;

        SET @AlertName = 'Replication Warning: Subscription expiration (Threshold: expiration)';

        IF EXISTS (   SELECT name
                        FROM msdb.dbo.sysalerts
                       WHERE (   name             = @AlertName
                           AND   enabled          = 0)
                          OR (   name             = @AlertName
                           AND   has_notification = 0))
            EXEC msdb.dbo.sp_delete_alert @name = @AlertName;

        IF NOT EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = @AlertName)
            EXEC msdb.dbo.sp_add_alert @name = @AlertName,
                                       @message_id = 14160,
                                       @severity = 0,
                                       @enabled = 1,
                                       @delay_between_responses = 3600,
                                       @include_event_description_in = 5,
                                       @category_name = N'Replication',
                                       @job_id = N'00000000-0000-0000-0000-000000000000';

        IF NOT EXISTS (   SELECT      name
                            FROM      msdb.dbo.sysalerts AS sa
                           INNER JOIN msdb.dbo.sysnotifications AS sn
                              ON sa.id = sn.alert_id
                           WHERE      sa.name = @AlertName)
            EXEC msdb.dbo.sp_add_notification @alert_name = @AlertName,
                                              @operator_name = @OperatorName,
                                              @notification_method = 1;

        SET @AlertName = 'Replication Warning: Slow merge over LAN connection (Threshold: mergefastrunspeed)';

        IF EXISTS (   SELECT name
                        FROM msdb.dbo.sysalerts
                       WHERE (   name             = @AlertName
                           AND   enabled          = 0)
                          OR (   name             = @AlertName
                           AND   has_notification = 0))
            EXEC msdb.dbo.sp_delete_alert @name = @AlertName;

        IF NOT EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = @AlertName)
            EXEC msdb.dbo.sp_add_alert @name = @AlertName,
                                       @message_id = 14164,
                                       @severity = 0,
                                       @enabled = 1,
                                       @delay_between_responses = 3600,
                                       @include_event_description_in = 5,
                                       @category_name = N'Replication',
                                       @job_id = N'00000000-0000-0000-0000-000000000000';

        IF NOT EXISTS (   SELECT      name
                            FROM      msdb.dbo.sysalerts AS sa
                           INNER JOIN msdb.dbo.sysnotifications AS sn
                              ON sa.id = sn.alert_id
                           WHERE      sa.name = @AlertName)
            EXEC msdb.dbo.sp_add_notification @alert_name = @AlertName,
                                              @operator_name = @OperatorName,
                                              @notification_method = 1;

        SET @AlertName = 'Replication Warning: Slow merge over dialup connection (Threshold: mergeslowrunspeed)';

        IF EXISTS (   SELECT name
                        FROM msdb.dbo.sysalerts
                       WHERE (   name             = @AlertName
                           AND   enabled          = 0)
                          OR (   name             = @AlertName
                           AND   has_notification = 0))
            EXEC msdb.dbo.sp_delete_alert @name = @AlertName;

        IF NOT EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = @AlertName)
            EXEC msdb.dbo.sp_add_alert @name = @AlertName,
                                       @message_id = 14165,
                                       @severity = 0,
                                       @enabled = 1,
                                       @delay_between_responses = 3600,
                                       @include_event_description_in = 5,
                                       @category_name = N'Replication',
                                       @job_id = N'00000000-0000-0000-0000-000000000000';

        IF NOT EXISTS (   SELECT      name
                            FROM      msdb.dbo.sysalerts AS sa
                           INNER JOIN msdb.dbo.sysnotifications AS sn
                              ON sa.id = sn.alert_id
                           WHERE      sa.name = @AlertName)
            EXEC msdb.dbo.sp_add_notification @alert_name = @AlertName,
                                              @operator_name = @OperatorName,
                                              @notification_method = 1;

        SET @AlertName = 'Replication Warning: Long merge over LAN connection (Threshold: mergefastrunduration)';

        IF EXISTS (   SELECT name
                        FROM msdb.dbo.sysalerts
                       WHERE (   name             = @AlertName
                           AND   enabled          = 0)
                          OR (   name             = @AlertName
                           AND   has_notification = 0))
            EXEC msdb.dbo.sp_delete_alert @name = @AlertName;

        IF NOT EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = @AlertName)
            EXEC msdb.dbo.sp_add_alert @name = @AlertName,
                                       @message_id = 14162,
                                       @severity = 0,
                                       @enabled = 1,
                                       @delay_between_responses = 3600,
                                       @include_event_description_in = 5,
                                       @category_name = N'Replication',
                                       @job_id = N'00000000-0000-0000-0000-000000000000';

        IF NOT EXISTS (   SELECT      name
                            FROM      msdb.dbo.sysalerts AS sa
                           INNER JOIN msdb.dbo.sysnotifications AS sn
                              ON sa.id = sn.alert_id
                           WHERE      sa.name = @AlertName)
            EXEC msdb.dbo.sp_add_notification @alert_name = @AlertName,
                                              @operator_name = @OperatorName,
                                              @notification_method = 1;

        SET @AlertName = 'Replication Warning: Long merge over dialup connection (Threshold: mergeslowrunduration)';

        IF EXISTS (   SELECT name
                        FROM msdb.dbo.sysalerts
                       WHERE (   name             = @AlertName
                           AND   enabled          = 0)
                          OR (   name             = @AlertName
                           AND   has_notification = 0))
            EXEC msdb.dbo.sp_delete_alert @name = @AlertName;

        IF NOT EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = @AlertName)
            EXEC msdb.dbo.sp_add_alert @name = @AlertName,
                                       @message_id = 14163,
                                       @severity = 0,
                                       @enabled = 1,
                                       @delay_between_responses = 3600,
                                       @include_event_description_in = 5,
                                       @category_name = N'Replication',
                                       @job_id = N'00000000-0000-0000-0000-000000000000';

        IF NOT EXISTS (   SELECT      name
                            FROM      msdb.dbo.sysalerts AS sa
                           INNER JOIN msdb.dbo.sysnotifications AS sn
                              ON sa.id = sn.alert_id
                           WHERE      sa.name = @AlertName)
            EXEC msdb.dbo.sp_add_notification @alert_name = @AlertName,
                                              @operator_name = @OperatorName,
                                              @notification_method = 1;

        SET @AlertName = 'Replication: agent custom shutdown';

        IF EXISTS (   SELECT name
                        FROM msdb.dbo.sysalerts
                       WHERE (   name             = @AlertName
                           AND   enabled          = 0)
                          OR (   name             = @AlertName
                           AND   has_notification = 0))
            EXEC msdb.dbo.sp_delete_alert @name = @AlertName;

        IF NOT EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = @AlertName)
            EXEC msdb.dbo.sp_add_alert @name = @AlertName,
                                       @message_id = 20578,
                                       @severity = 0,
                                       @enabled = 1,
                                       @delay_between_responses = 3600,
                                       @include_event_description_in = 5,
                                       @category_name = N'Replication',
                                       @job_id = N'00000000-0000-0000-0000-000000000000';

        IF NOT EXISTS (   SELECT      name
                            FROM      msdb.dbo.sysalerts AS sa
                           INNER JOIN msdb.dbo.sysnotifications AS sn
                              ON sa.id = sn.alert_id
                           WHERE      sa.name = @AlertName)
            EXEC msdb.dbo.sp_add_notification @alert_name = @AlertName,
                                              @operator_name = @OperatorName,
                                              @notification_method = 1;

        SET @AlertName = 'Replication: agent failure';

        IF EXISTS (   SELECT name
                        FROM msdb.dbo.sysalerts
                       WHERE (   name             = @AlertName
                           AND   enabled          = 0)
                          OR (   name             = @AlertName
                           AND   has_notification = 0))
            EXEC msdb.dbo.sp_delete_alert @name = @AlertName;

        IF NOT EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = @AlertName)
            EXEC msdb.dbo.sp_add_alert @name = @AlertName,
                                       @message_id = 14151,
                                       @severity = 0,
                                       @enabled = 1,
                                       @delay_between_responses = 3600,
                                       @include_event_description_in = 5,
                                       @category_name = N'Replication',
                                       @job_id = N'00000000-0000-0000-0000-000000000000';

        IF NOT EXISTS (   SELECT      name
                            FROM      msdb.dbo.sysalerts AS sa
                           INNER JOIN msdb.dbo.sysnotifications AS sn
                              ON sa.id = sn.alert_id
                           WHERE      sa.name = @AlertName)
            EXEC msdb.dbo.sp_add_notification @alert_name = @AlertName,
                                              @operator_name = @OperatorName,
                                              @notification_method = 1;

        SET @AlertName = 'Replication: agent retry';

        IF EXISTS (   SELECT name
                        FROM msdb.dbo.sysalerts
                       WHERE (   name             = @AlertName
                           AND   enabled          = 0)
                          OR (   name             = @AlertName
                           AND   has_notification = 0))
            EXEC msdb.dbo.sp_delete_alert @name = @AlertName;

        IF NOT EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = @AlertName)
            EXEC msdb.dbo.sp_add_alert @name = @AlertName,
                                       @message_id = 14152,
                                       @severity = 0,
                                       @enabled = 1,
                                       @delay_between_responses = 3600,
                                       @include_event_description_in = 5,
                                       @category_name = N'Replication',
                                       @job_id = N'00000000-0000-0000-0000-000000000000';

        IF NOT EXISTS (   SELECT      name
                            FROM      msdb.dbo.sysalerts AS sa
                           INNER JOIN msdb.dbo.sysnotifications AS sn
                              ON sa.id = sn.alert_id
                           WHERE      sa.name = @AlertName)
            EXEC msdb.dbo.sp_add_notification @alert_name = @AlertName,
                                              @operator_name = @OperatorName,
                                              @notification_method = 1;

        SET @AlertName = 'Replication: agent success';

        IF EXISTS (   SELECT name
                        FROM msdb.dbo.sysalerts
                       WHERE (   name             = @AlertName
                           AND   enabled          = 0)
                          OR (   name             = @AlertName
                           AND   has_notification = 0))
            EXEC msdb.dbo.sp_delete_alert @name = @AlertName;

        IF NOT EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = @AlertName)
            EXEC msdb.dbo.sp_add_alert @name = @AlertName,
                                       @message_id = 14150,
                                       @severity = 0,
                                       @enabled = 1,
                                       @delay_between_responses = 3600,
                                       @include_event_description_in = 5,
                                       @category_name = N'Replication',
                                       @job_id = N'00000000-0000-0000-0000-000000000000';

        IF NOT EXISTS (   SELECT      name
                            FROM      msdb.dbo.sysalerts AS sa
                           INNER JOIN msdb.dbo.sysnotifications AS sn
                              ON sa.id = sn.alert_id
                           WHERE      sa.name = @AlertName)
            EXEC msdb.dbo.sp_add_notification @alert_name = @AlertName,
                                              @operator_name = @OperatorName,
                                              @notification_method = 1;

        SET @AlertName = 'Replication: expired subscription dropped';

        IF EXISTS (   SELECT name
                        FROM msdb.dbo.sysalerts
                       WHERE (   name             = @AlertName
                           AND   enabled          = 0)
                          OR (   name             = @AlertName
                           AND   has_notification = 0))
            EXEC msdb.dbo.sp_delete_alert @name = @AlertName;

        IF NOT EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = @AlertName)
            EXEC msdb.dbo.sp_add_alert @name = @AlertName,
                                       @message_id = 14157,
                                       @severity = 0,
                                       @enabled = 1,
                                       @delay_between_responses = 3600,
                                       @include_event_description_in = 5,
                                       @category_name = N'Replication',
                                       @job_id = N'00000000-0000-0000-0000-000000000000';

        IF NOT EXISTS (   SELECT      name
                            FROM      msdb.dbo.sysalerts AS sa
                           INNER JOIN msdb.dbo.sysnotifications AS sn
                              ON sa.id = sn.alert_id
                           WHERE      sa.name = @AlertName)
            EXEC msdb.dbo.sp_add_notification @alert_name = @AlertName,
                                              @operator_name = @OperatorName,
                                              @notification_method = 1;

        SET @AlertName = 'Replication: Subscriber has failed data validation';

        IF EXISTS (   SELECT name
                        FROM msdb.dbo.sysalerts
                       WHERE (   name             = @AlertName
                           AND   enabled          = 0)
                          OR (   name             = @AlertName
                           AND   has_notification = 0))
            EXEC msdb.dbo.sp_delete_alert @name = @AlertName;

        IF NOT EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = @AlertName)
            EXEC msdb.dbo.sp_add_alert @name = @AlertName,
                                       @message_id = 20574,
                                       @severity = 0,
                                       @enabled = 1,
                                       @delay_between_responses = 3600,
                                       @include_event_description_in = 5,
                                       @category_name = N'Replication',
                                       @job_id = N'00000000-0000-0000-0000-000000000000';

        IF NOT EXISTS (   SELECT      name
                            FROM      msdb.dbo.sysalerts AS sa
                           INNER JOIN msdb.dbo.sysnotifications AS sn
                              ON sa.id = sn.alert_id
                           WHERE      sa.name = @AlertName)
            EXEC msdb.dbo.sp_add_notification @alert_name = @AlertName,
                                              @operator_name = @OperatorName,
                                              @notification_method = 1;

        SET @AlertName = 'Replication: Subscriber has passed data validation';

        IF EXISTS (   SELECT name
                        FROM msdb.dbo.sysalerts
                       WHERE (   name             = @AlertName
                           AND   enabled          = 0)
                          OR (   name             = @AlertName
                           AND   has_notification = 0))
            EXEC msdb.dbo.sp_delete_alert @name = @AlertName;

        IF NOT EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = @AlertName)
            EXEC msdb.dbo.sp_add_alert @name = @AlertName,
                                       @message_id = 20575,
                                       @severity = 0,
                                       @enabled = 1,
                                       @delay_between_responses = 3600,
                                       @include_event_description_in = 5,
                                       @category_name = N'Replication',
                                       @job_id = N'00000000-0000-0000-0000-000000000000';

        IF NOT EXISTS (   SELECT      name
                            FROM      msdb.dbo.sysalerts AS sa
                           INNER JOIN msdb.dbo.sysnotifications AS sn
                              ON sa.id = sn.alert_id
                           WHERE      sa.name = @AlertName)
            EXEC msdb.dbo.sp_add_notification @alert_name = @AlertName,
                                              @operator_name = @OperatorName,
                                              @notification_method = 1;

        SET @AlertName = 'Replication: Subscription reinitialized after validation failure';

        IF EXISTS (   SELECT name
                        FROM msdb.dbo.sysalerts
                       WHERE (   name             = @AlertName
                           AND   enabled          = 0)
                          OR (   name             = @AlertName
                           AND   has_notification = 0))
            EXEC msdb.dbo.sp_delete_alert @name = @AlertName;

        IF NOT EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = @AlertName)
            EXEC msdb.dbo.sp_add_alert @name = @AlertName,
                                       @message_id = 20572,
                                       @severity = 0,
                                       @enabled = 1,
                                       @delay_between_responses = 3600,
                                       @include_event_description_in = 5,
                                       @category_name = N'Replication',
                                       @job_id = N'00000000-0000-0000-0000-000000000000';

        IF NOT EXISTS (   SELECT      name
                            FROM      msdb.dbo.sysalerts AS sa
                           INNER JOIN msdb.dbo.sysnotifications AS sn
                              ON sa.id = sn.alert_id
                           WHERE      sa.name = @AlertName)
            EXEC msdb.dbo.sp_add_notification @alert_name = @AlertName,
                                              @operator_name = @OperatorName,
                                              @notification_method = 1;
    END;
END;
GO
