# -*- perl -*-
# ers_remove
# 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 Getopt::Long;
use DBI;

$| = 1;

$result = GetOptions("debug!", "quiet!", "help", "pgsuperuser=s", "pgsuperpass=s",
                     "masterserver=s", "masteruser=s", "masterpass=s", "masterdb=s", "masterport=s",
                     "slaveserver=s", "slaveuser=s", "slavepass=s", "slavedb=s", "slaveport=s");

$RServ::debug = $opt_debug || 0;

if(! -f "@prefix@/etc/replication.cfg") {
  printf"
WARNING: @prefix@/etc/replication.cfg doesn't exist
         which means that this server has already been
         unconfigured\n\n";
  exit(1);
}

if (defined($opt_help)  || !defined($opt_masteruser)
			|| !defined($opt_masterdb)
			|| ( !defined($opt_slavedb) &&  !defined($opt_slaveserver) ) ) {
    print "Usage: $0 
		--masterserver=<server> [--pgsuperuser=<user>]
                --masteruser=<user>     [--pgsuperpass=<pass>]
                --masterpass=<pass>     [--slaveserver=<server>]
                --masterdb=<database>   [--slaveuser=<user>]
                --masterport=<port>     [--slavepass=<user>]
                                        [--slavedb=<database>]
                                        [--slaveport=<port>]\n";
    exit ((scalar(@ARGV) < 1)? 1:0);
}

my $quiet	 = 1 if $opt_quiet;

my $masterdb     = $opt_masterdb;
my $masterserver = $opt_masterserver || "localhost";
my $masteruser   = $opt_masteruser;
my $masterport   = $opt_masterport || "5432";
my $masterpass   = $opt_masterpass || "";

my $superuser    = $opt_pgsuperuser || $opt_masteruser;
my $superpass    = $opt_pgsuperpass || $opt_masterpass;

my $slavedb = $opt_slavedb || $opt_masterdb;
my $slaveserver = $opt_slaveserver || $opt_masterserver;
my $slaveuser = $opt_slaveuser || $opt_masteruser;
my $slaveport = $opt_slaveport || "5432";
my $slavepass = "";
if(defined($opt_slavepass)) {
  $slavepass = $opt_slavepass;
} elsif(defined($opt_masterpass)) {
  $slavepass = $opt_masterpass;
} 

# Test that the connection parameters given for connecting to the databases are correct
my $functions = DBI->connect("dbi:Pg:dbname=$masterdb;host=$masterserver;port=$masterport", $superuser, $superpass)
	|| die "Connection to master server failed: ", $DBI::errstr, "\n";
$functions->{AutoCommit} = 0;

my $master = DBI->connect("dbi:Pg:dbname=$masterdb;host=$masterserver;port=$masterport", $masteruser, $masterpass)
	|| die "Connection to master server failed: ", $DBI::errstr, "\n";
$master->{AutoCommit} = 0;

my $slave = DBI->connect("dbi:Pg:dbname=$slavedb;host=$slaveserver;port=$slaveport", $slaveuser, $slavepass)
	|| die "Connection to slave server failed: ", $DBI::errstr, "\n";
$slave->{AutoCommit} = 0;

# Display a banner to the user
if(!$quiet) {
  print "\nPostgreSQL Enterprise Replication Server 1.20\n";
  print "   Copyright (C) 2001-2002 PostgreSQL, Inc\n";
  print "*********************************************\n";
  print "\nUnconfiguring eRServer...\n\n";
}

# Show Master database parameters unless quietness is requested
if(!$quiet) {
  print "Master database\n";
  print "***************\n";
  print "Server                : ", $masterserver, "\n";
  print "Port                  : ", $masterport, "\n";
  if($opt_pgsuperuser ne "") {
    print "PG Superuser          : ", $superuser, "\n";
  }
  if($opt_pgsuperpass ne "") {
    print "PG Superuser password : ", $superpass, "\n";
  }
  print "Database              : ", $masterdb, "\n";
  print "Username              : ", $masteruser, "\n";
  if($masterpass ne "") {
    print "Password              : ", $masterpass, "\n";
  }
  print "\n";
}

# Remove Master Tables

# List of slave servers
$master->do("DROP TABLE _rserv_servers_")
	|| die $master->errstr;

# List of replicated tables
$master->do("DROP TABLE _rserv_tables_")
	|| die $master->errstr;

# Bookkeeping log for row replication
$master->do("DROP TABLE _rserv_log_1_")
	|| die $master->errstr;

# Second log
$master->do("DROP TABLE _rserv_log_2_")
	|| die $master->errstr;

# Sync point for each slave server
$master->do("DROP TABLE _rserv_sync_")
	|| die $master->errstr;

# Sync point reference numbers
$master->do("DROP SEQUENCE _rserv_sync_seq_")
	|| die $master->errstr;

# Active log id
$master->do("DROP SEQUENCE _rserv_active_log_id_")
	|| die $master->errstr;

# Old log status
$master->do("DROP SEQUENCE _rserv_old_log_status_")
	|| die $master->errstr;

# Slave servers
$master->do("DROP SEQUENCE _rserv_servers__server_seq")
	|| die $master->errstr;

# Notify user where things are up to, unless requested
if (!$quiet) {
  print "Master database replication removal completed\n";
}

# Remove the eRServer Functions
$functions->do("set transaction isolation level serializable")
	|| die $master->errstr;
$functions->do("DROP FUNCTION _rserv_log_()")
	|| die $master->errstr;
$functions->do("DROP FUNCTION _rserv_sync_(int4)")
	|| die $master->errstr;
$functions->do("DROP FUNCTION _rserv_debug_(int4)")
	|| die $master->errstr;
$functions->do("DROP FUNCTION _rserv_lock_altid_(int4)")
	|| die $master->errstr;
$functions->do("DROP FUNCTION _rserv_unlock_altid_(int4)")
	|| die $master->errstr;

# PTE funcs
$functions->do("DROP FUNCTION _pte_get_snapshot_()")
	|| die $master->errstr;
$functions->do("DROP FUNCTION _pte_set_snapshot_(text)")
	|| die $master->errstr;

# Notify user where things are up to, unless requested
if (!$quiet) {
  print "Replication function removal completed\n\n";
}

# Display the parameters for the slave database
if(!$quiet) {
  print "Slave database\n";
  print "**************\n";
  print "Server   : ", $slaveserver, "\n";
  print "Port     : ", $slaveport, "\n";
  print "Database : ", $slavedb, "\n";
  print "Username : ", $slaveuser, "\n";
  if($slavepass ne "") {
    print "Password : ", $slavepass, "\n";
  }
    print "\n";
}

# Slave Tables
$slave->do("set transaction isolation level serializable")
	|| die $slave->errstr;

$slave->do("DROP TABLE _rserv_slave_tables_")
	|| die $slave->errstr;

$slave->do("DROP TABLE _rserv_slave_sync_")
	|| die $slave->errstr;

# Commit the transactions and close the database connections
$slave->commit();
$functions->commit();
$master->commit();
$slave->disconnect();
$functions->disconnect();
$master->disconnect();

# Notify user where things are up to, unless requested
if (!$quiet) {
  print "Slave database replication removal completed\n";
}

# Remove configuration files
system('rm -f @prefix@/bin/ers_setvars');
system('rm -f @prefix@/etc/replication.cfg');
if (!$quiet) {
  print "Configuration files removed\n\n";
}

print "eRServer is now unconfigured\n\n";

exit(0);
