#!/usr/bin/perl -w
# SlaveInit
# Vadim Mikheev, (c) 2000, PostgreSQL Inc.

eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
    & eval 'exec perl -S $0 $argv:q'
    if 0;

BEGIN {
	my $basedir = $0; $basedir =~ s#/[^/]+$##;
	unshift(@INC, "$basedir/../share");
}

use strict;
use Pg;
use Getopt::Long;
use RServ;

$| = 1;

my ($debug,$verbose,$quiet) = (0,0,0);
my ($help,$masterhost,$masterport,$masteruser,$masterpassword,
	$slavehost,$slaveport,$slaveuser,$slavepassword);

my $result = GetOptions(
	"debug!" => \$debug, "verbose!" => \$verbose,
	"quiet!" => \$quiet, "help" => \$help,
	"masterhost=s" => \$masterhost, "masterport=i" => \$masterport,
	"masteruser=s" => \$masteruser, "masterpassword=s" => \$masterpassword,
	"slavehost=s" => \$slavehost, "slaveport=i" => \$slaveport,
	"slaveuser=s" => \$slaveuser, "slavepassword=s" => \$slavepassword,
	);

if (defined($help) || (scalar(@ARGV) < 2)) {
    print "Usage: $0 [options] masterdb slavedb
Options:
	--masterhost=hostname --masterport=port
	--masteruser=username --masterpassword=string
	--slavehost=hostname --slaveport=port
	--slaveuser=username --slavepassword=string
";
    exit ((scalar(@ARGV) < 2)? 1:0);
}

my $master = $ARGV[0] || "master";
my $slave = $ARGV[1] || "slave";

my $minfo = MkInfo($master,$masterhost,$masterport,$masteruser,$masterpassword);
my $sinfo = MkInfo($slave,$slavehost,$slaveport,$slaveuser,$slavepassword);

# First, lets add the needed information in the slave database

print("Connecting to $sinfo\n") if ($debug || $verbose);
my $conn = Pg::connectdb($sinfo);
if ($conn->status != PGRES_CONNECTION_OK) {
    print STDERR "Failed opening $sinfo\n";
    exit 1;
}

$result = $conn->exec("BEGIN");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

$result = $conn->exec("set transaction isolation level serializable");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

$result = $conn->exec("create table _RSERV_SLAVE_TABLES_" .
		      " (tname name not null, cname name not null, reloid oid not null, key int4 not null)");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

$result = $conn->exec("create table _RSERV_SLAVE_SYNC_" .
		      " (syncid int4 not null, synctime timestamp)");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);


# next, let's tell the master that the slave is set up.

print("Connecting to $minfo\n") if ($debug || $verbose);
my $mconn = Pg::connectdb($minfo);
if ($mconn->status != PGRES_CONNECTION_OK) {
    print STDERR "Failed opening $minfo\n";
    RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
#    exit 1;
}

$slavehost="localhost" if (! $slavehost);

$result = $mconn->exec("INSERT INTO _RSERV_SERVERS_ (host,dbase) VALUES ('$slavehost','$slave')");
if ($result->resultStatus ne PGRES_COMMAND_OK) {
    print STDERR $mconn->errorMessage;
    RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
#    exit (-1);
}


# then commit the slave transaction

$result = $conn->exec("COMMIT");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);



exit (0);
