#!/usr/bin/perl
use strict;
use warnings;

my $VERSION='5.0.83';

my $BINDIR="$ENV{HOME}/opt/mysql/$VERSION/bin";

# 10.23.1.26 master   (OSX 10.5)
# 10.23.1.22  slave 1 (OSX 10.5)
# 10.23.1.24  Slave 2 (Linux CentOS 5.3)

my $master_port = 15000;
my $base_port   = 1500;
my $master_ip   = '10.23.1.26';
my @slave_ips   = ('10.23.1.22', '10.23.1.24');

print "installing master\n";
my $MASTER_SANDBOX=
    "5.0.83 --no_show"
       . " -c log-bin=mysql-bin "
       . " -c server-id=100 "
       . " --sandbox_port=$master_port";
 
#my $ret = system $MASTER_SANDBOX;
my $ret = qx(echo "$MASTER_SANDBOX" | gearman -h $master_ip -f make_sandbox ) ;
if ($?) {
    die "master creation failed ($!)\n"
} 
chomp $ret;
if ($ret ne 'ok') {
    die "master creation failed ($ret)\n"
}

open my $ALL , q{>}, 'use_all'
    or die "can't create 'use_all'\n";

open my $FH , q{>}, 'm'
    or die "can't create 'm'\n";
print $FH "#!/bin/sh\n";

print $ALL "#!/bin/sh\n";
print $ALL "$BINDIR/mysql --prompt='master > ' -u msandbox "
         . "-pmsandbox -h $master_ip -P $master_port \$\@ \n";

print $FH  "$BINDIR/mysql --prompt='master > ' -u msandbox "
         . "-pmsandbox -h $master_ip -P $master_port \$\@ \n";
close $FH;
chmod 0755, "m";

open my $CHECK , q{>}, 'check_slaves'
    or die "can't create 'check_slaves'\n";
print $CHECK "#!/bin/sh\n";

for my $slaveno ( 0 .. 1 ) {
    my $SLAVE = $slaveno +1;
    my $IP = $slave_ips[$slaveno];
    my $slave_port = "$base_port$SLAVE";
    my $SLAVE_SANDBOX = "5.0.83 --no_show"
       . " -c log-bin=mysql-bin "
       . " -c server-id=10$SLAVE "
       . " --sandbox_port=$slave_port"
       # . " --no_load_grants"
       . " -c report-host=SBslave$SLAVE"
       . " -c report-port=$slave_port" ;
     my $QUERY =   
      qq( CHANGE MASTER TO )
           . qq( master_host="$master_ip", )
           . qq( master_port=$master_port, )
           . qq( master_user="msandbox", )
           . qq( master_password="msandbox" );
    
    print "installing slave $slaveno\n";
    my $ret = qx(echo "$SLAVE_SANDBOX" | gearman -h $IP -f make_sandbox ) ;
    if ($?) {
        die "slave creation failed ($!)\n"
    } 
    chomp $ret;
    if ($ret ne 'ok') {
        die "slave creation failed ($ret)\n"
    }
    print "initializing slave $slaveno\n";
    system    "$BINDIR/mysql -u msandbox -pmsandbox -h $IP "
            . " -P $slave_port -e '$QUERY' ";
    system    "$BINDIR/mysql -u msandbox -pmsandbox -h $IP "
            .  "-P $slave_port -e 'start slave' ";
    open my $FH , q{>}, "s$SLAVE"
        or die "can't create 's$SLAVE'\n";
    print $FH "#!/bin/sh\n";

    print $ALL "echo slave $SLAVE:\n";
    print $ALL "$BINDIR/mysql --prompt='slave $SLAVE > ' "
        . " -u msandbox -pmsandbox -h $IP -P $slave_port \$\@ \n";

    print $CHECK "echo slave $SLAVE:  \n";
    print $CHECK "$BINDIR/mysql --prompt='slave $SLAVE > ' "
        . " -u msandbox -pmsandbox -h $IP -P $slave_port "
        . "-e 'show slave status \\G' | grep _Running \n";

    print $FH "$BINDIR/mysql --prompt='slave $SLAVE > ' "
        . " -u msandbox -pmsandbox -h $IP -P $slave_port \$\@ \n";
    close $FH;
    chmod 0755, "s$SLAVE";
}

chmod 0755, "use_all";
chmod 0755, "check_slaves";
close $ALL;
close $CHECK;