# -*- perl -*-
# ers_masterremovetables
# Copyright (C) 2002 Digital Distribution GFS P/L
# Derived from code Copyright (C) 2000 PostgreSQL, Inc.

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

use Pg;
use Getopt::Long;

$| = 1;

$result = GetOptions("debug!", "quiet!", "help",
		     "host=s", "user=s", "password=s");

$RServ::debug = $opt_debug || 0;
$RServ::quiet = 1 if $opt_quiet;

if (defined($opt_help) || (scalar(@ARGV) < 1)) {
    print "Usage: $0 --host=name --user=name --password=string masterdb\n";
    exit ((scalar(@ARGV) < 1)? 1:0);
}

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

my $minfo = "dbname=$master";
$minfo = "$minfo host=$opt_host" if (defined($opt_host));
$minfo = "$minfo user=$opt_user" if (defined($opt_user));
$minfo = "$minfo password=$opt_password" if (defined($opt_password));

sub RollbackAndQuit {
    $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;
}

my $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);

# Remove the list of slave servers
$result = $conn->exec("DROP TABLE _rserv_servers_");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

# Remove the list of replicated tables
$result = $conn->exec("DROP TABLE _rserv_tables_");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

# Remove the bookkeeping log for row replication
$result = $conn->exec("DROP TABLE _rserv_log_1_");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

# Remove the second log
$result = $conn->exec("DROP TABLE _rserv_log_2_");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

# Remove the sync point for each slave server
$result = $conn->exec("DROP TABLE _rserv_sync_");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

# Remove the sequence for the list of slave servers
$result = $conn->exec("DROP SEQUENCE _rserv_servers__server_seq");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

# Remove the sequence for the sync point reference number
$result = $conn->exec("DROP SEQUENCE _rserv_sync_seq_");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

# Remove the sequence for the active log id
$result = $conn->exec("DROP SEQUENCE _rserv_active_log_id_");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

# Remove the sequence for the old log status
$result = $conn->exec("DROP SEQUENCE _rserv_old_log_status_");
RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);

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

exit (0);
