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

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

use strict;
use Pg; 
use Getopt::Long;
        
$| = 1; 

my ($debug,$verbose) = (0,0);
my ($help,$masterhost,$masterport,$masteruser,$masterpassword);

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

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

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

my $minfo = "dbname=$master";
$minfo = "$minfo host=$masterhost" if (defined($masterhost));
$minfo = "$minfo port=$masterport" if (defined($masterport));
$minfo = "$minfo user=$masteruser" if (defined($masteruser));
$minfo = "$minfo password=$masterpassword" if (defined($masterpassword));

sub RollbackAndQuit {
    my $conn = shift @_;

    print STDERR "Error in query: ", $conn->errorMessage;
    $conn->exec("ROLLBACK");
    exit (-1);
}

my $conn = Pg::connectdb($minfo);
if ($conn->status != PGRES_CONNECTION_OK) {
    print STDERR "Failed opening $minfo\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);

# List of slave servers
$result = $conn->exec("drop table _RSERV_SERVERS_");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

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

# List of replicated tables
#$result = $conn->exec("DROP RULE _rserv_deltrig_");
#RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

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

$result = $conn->exec("SELECT pgc.relname FROM pg_trigger pgt, pg_class pgc WHERE tgname='_rserv_trigger_t_' AND pgt.tgrelid=pgc.oid");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_TUPLES_OK);
while (my @row = $result->fetchrow) {
    my $res = $conn->exec("DROP TRIGGER _RSERV_TRIGGER_T_ ON \"$row[0]\"");
    RollbackAndQuit($conn) if ($res->resultStatus ne PGRES_COMMAND_OK);
}

# Drop Human Log
$result = $conn->exec("DROP VIEW _RSERV_HUMAN_LOG_");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

# Bookkeeping log for row replication
$result = $conn->exec("drop table _RSERV_LOG_");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

# This is to speedup lookup of deleted tuples
## should be done automatically
#$result = $conn->exec("drop index _RSERV_LOG_INDX_DLT_ID_");
#RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

# This is to speedup cleanup
## should be done automatically
#$result = $conn->exec("drop index _RSERV_LOG_INDX_TM_ID_");
#RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

# This is to speedup trigger and lookup of updated tuples
## should be done automatically
#$result = $conn->exec("drop index _RSERV_LOG_INDX_REL_KEY_");
#RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

# Sync point for each slave server
$result = $conn->exec("drop table _RSERV_SYNC_");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

## should be done automatically
#$result = $conn->exec("drop index _RSERV_SYNC_INDX_SRV_ID_");
#RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

# Sync point reference numbers
$result = $conn->exec("drop sequence _RSERV_SYNC_SEQ_");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

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

$result = $conn->exec("DROP FUNCTION _rserv_sync_(int4)");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

$result = $conn->exec("DROP FUNCTION _rserv_debug_(int4)");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

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

exit (0);
