#!/usr/bin/env perl

use strict;
use warnings FATAL => 'all';

use Getopt::Long;

my (
    $command,          $ssh_user,        $orig_master_host, $orig_master_ip,
    $orig_master_port, $new_master_host, $new_master_ip,    $new_master_port
);

my $vip = '10.0.0.178/24';
my $inetf = 'em4';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig $inetf:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig $inetf:$key down";

my $inetf2 = 'eth1';
my $ssh_start_vip2 = "/sbin/ifconfig $inetf2:$key $vip";
my $ssh_stop_vip2 = "/sbin/ifconfig $inetf2:$key down";

GetOptions(
    'command=s'          => \$command,
    'ssh_user=s'         => \$ssh_user,
    'orig_master_host=s' => \$orig_master_host,
    'orig_master_ip=s'   => \$orig_master_ip,
    'orig_master_port=i' => \$orig_master_port,
    'new_master_host=s'  => \$new_master_host,
    'new_master_ip=s'    => \$new_master_ip,
    'new_master_port=i'  => \$new_master_port,
);

exit &main();

sub main {

    print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";

    if ( $command eq "stop" || $command eq "stopssh" ) {

        my $exit_code = 1;
        eval {
            print "Disabling the VIP on old master: $orig_master_host \n";
            &stop_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn "Got Error: $@\n";
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "start" ) {

        my $exit_code = 10;
        eval {
            print "Enabling the VIP - $vip on the new master - $new_master_host \n";
            &start_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn $@;
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "status" ) {
        print "Checking the Status of the script.. OK \n";
        exit 0;
    }
    else {
        &usage();
        exit 1;
    }
}

sub start_vip() {
     my $atlas_bad_id = `mysql -uuser -ppwd -h 127.0.0.1 -P2345 -e 'select * from backends' |awk /$new_master_host/'{print \$1}'`;
    `ssh -p 29050 -o ConnectTimeout=2 $orig_master_host \" /sbin/ifconfig $inetf:1 down \"`;
    `ssh -p 29050 $new_master_host \" $ssh_start_vip; $ssh_start_vip2; /sbin/arping -A 10.0.0.178 -c 2 -I $inetf; /sbin/arping -A 10.0.0.178 -c 2 -I $inetf2; \"`;
     system "ssh -p 29050 -t 192.168.1.\$(echo $orig_master_host | awk -F'.' '{print \$4}') '/usr/local/mysql-proxy/bin/mysql-proxyd test stop'";
    `ssh -p 29050 192.168.1.175 \" mysql -uuser -ppwd -h 127.0.0.1 -P2345 -e 'remove backend $atlas_bad_id; SAVE CONFIG;' \"`;
    `ssh -p 29050 192.168.1.176 \" mysql -uuser -ppwd -h 127.0.0.1 -P2345 -e 'remove backend $atlas_bad_id; SAVE CONFIG;' \"`;
    `ssh -p 29050 192.168.1.177 \" mysql -uuser -ppwd -h 127.0.0.1 -P2345 -e 'remove backend $atlas_bad_id; SAVE CONFIG;' \"`;
    `ssh -p 29050 192.168.1.180 \" mysql -uuser -ppwd -h 127.0.0.1 -P2345 -e 'remove backend $atlas_bad_id; SAVE CONFIG;' \"`;
    `ssh -p 29050 192.168.1.201 \" /etc/init.d/keepalived restart \"`;
}
sub stop_vip() {
     return 0  unless  ($ssh_user);
    `ssh -p 29050 $ssh_user\@$orig_master_host \" $ssh_stop_vip; $ssh_stop_vip2; \"`;
}

sub usage {
    print
    "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}
