BEGIN
DECLARE v_rdsrepl INT;
DECLARE v_mysql_version VARCHAR(20);
DECLARE v_threads_running INT;
DECLARE v_called_by_user VARCHAR(50);
DECLARE v_sleep int;
DECLARE v_rep_status int;
DECLARE v_rep_status_stop int;
DECLARE sql_logging BOOLEAN;
select @@sql_log_bin into sql_logging;
Select count(1) into v_rep_status_stop from mysql.rds_replication_status where action = 'stop slave';
Select count(1) into v_rep_status from mysql.rds_replication_status;
Select user() into v_called_by_user;
Select version() into v_mysql_version;
Select count(1) into v_rdsrepl from mysql.rds_history where action = 'disable set master' and master_user = 'rdsrepladmin';
Select count(1) into v_threads_running FROM information_schema.processlist where user = 'system user';

IF v_called_by_user != 'rdsadmin@localhost'
  THEN
  Select 'You do not have privileges to call this procedure' as message;
ELSEIF v_rep_status = 0
Then 
 set @@sql_log_bin=off;
 insert into mysql.rds_replication_status(called_by_user, action, mysql_version)values (v_called_by_user,'disable set master',v_mysql_version);
 insert into mysql.rds_history(called_by_user, action, master_user, mysql_version)values (v_called_by_user,'disable set master','rdsrepladmin',v_mysql_version);
 commit;
ELSEIF phase not in ('enable','disable')
then
select 'please use the values enable or disable' as message;
 

ElseIF v_rdsrepl > 0 and phase = 'disable'
  THEN
   Select 'MYSQL.RDS_SET_EXTERNAL_MASTER is disabled' as message;

ElseIF v_rdsrepl = 0 and phase = 'disable'
   THEN
       if v_threads_running > 0
         then
             call mysql.rds_stop_replication();
             select sleep(1) into v_sleep;
             update mysql.rds_replication_status set called_by_user=v_called_by_user, action='disable set master', mysql_version=v_mysql_version where action is not null;
             commit;
             select sleep(1) into v_sleep;
             INSERT into mysql.rds_history(called_by_user, action, mysql_version, master_user) values (v_called_by_user,'disable set master', v_mysql_version, 'rdsrepladmin');
             commit;
             select 'Replication has been stopped and MYSQL.RDS_SET_EXTERNAL_MASTER has been disabled' as message;
         else
             update mysql.rds_replication_status set called_by_user=v_called_by_user, action='disable set master', mysql_version=v_mysql_version where action is not null;
             commit;
             select sleep(1) into v_sleep;
             INSERT into mysql.rds_history(called_by_user, action, mysql_version, master_user) values (v_called_by_user,'disable set master', v_mysql_version, 'rdsrepladmin');
             commit;
             Select 'MYSQL.RDS_SET_EXTERNAL_MASTER has been disabled' as message;
         end if;

ELSEIF v_rdsrepl = 0 and phase = 'enable'
    THEN
      if v_threads_running > 0
         then
            update mysql.rds_replication_status set called_by_user=v_called_by_user,action='enable set master', mysql_version=v_mysql_version where action is not null;
            commit;
            select 'MYSQL.RDS_SET_EXTERNAL_MASTER is enabled.' as message;
      end if;
ELSEIF v_rdsrepl > 0 and phase = 'enable'
    THEN
        update mysql.rds_replication_status set called_by_user=v_called_by_user,action='enable set master', mysql_version=v_mysql_version where action is not null;
        commit;
        select sleep(1) into v_sleep;
        UPDATE mysql.rds_history set action = 'enable set master' where action = 'disable set master' and master_user = 'rdsrepladmin';
        commit;
        select 'MYSQL.RDS_SET_EXTERNAL_MASTER has been enabled' as message;
END IF;
IF v_rep_status_stop = 1
  Then 
   update mysql.rds_replication_status set called_by_user=v_called_by_user,action='stop slave', mysql_version=v_mysql_version where action is not null;
   commit;
END IF;
set @@sql_log_bin=sql_logging;
END