#!/bin/sh

#
# Drop the errant node from the system
#
echo "Dropping Node 2: Slon processes for Node 2 should be dead."
slonik <<_EOF_
cluster name = gb;
node 1 admin conninfo = 'dbname=gb host=localhost user=postgres port=5434';
node 2 admin conninfo = 'dbname=gb host=localhost user=postgres port=5430';
node 3 admin conninfo = 'dbname=gb_replica host=localhost user=postgres port=5430';

drop node (id = 2);
echo 'Dropped Node 2';

_EOF_
echo "Hit Return to Continue"
read x;

#
# Recreate the empty database OR use slonik uninstall node
#
echo "This is where you re-prepare the database for Node 2"
#dropdb -p 5430 gb;
#createdb -p 5430 gb;
#psql gb < gb_schema.sql;

#
# Add back the node and the paths and listens
#
echo "Add back Node 2 and paths and listens for Node 2"
slonik <<_EOF_
cluster name = gb;
node 1 admin conninfo = 'dbname=gb host=localhost user=postgres port=5434';
node 2 admin conninfo = 'dbname=gb host=localhost user=postgres port=5430';
node 3 admin conninfo = 'dbname=gb_replica host=localhost user=postgres port=5430';

store node (id=2, comment='gb 8.0 5430');
echo 'Add Node 2';

store path (server=2, client=3, conninfo='dbname=gb host=localhost port=5430 user=postgres');
store path (server=2, client=1, conninfo='dbname=gb host=localhost port=5430 user=postgres');
store path (server=3, client=2, conninfo='dbname=gb_replica host=localhost port=5430 user=postgres');
store path (server=1, client=2, conninfo='dbname=gb host=localhost port=5434 user=postgres');
echo 'Add Paths for Node 2';

store listen (origin=2, receiver=3, provider=2);
store listen (origin=2, receiver=1, provider=2);
store listen (origin=1, receiver=2, provider=1);
store listen (origin=3, receiver=2, provider=3);
echo 'Add Listen for Node 2';

_EOF_
echo "Hit Return to Continue"
read x;


#
# Restart the slon process for the newly added replica
#
echo "Restarting Slon"
slon gb "dbname=gb user=postgres port=5430 host=localhost" > slon_gb_80.out 2>&1 &
echo "Hit Return to Continue"
read x;


#
# subscribe to the set of data
#
echo "Subscribe Node 2 to the table set. Give it some time to catch up."
slonik <<_EOF_

cluster name = gb;
node 1 admin conninfo = 'dbname=gb host=localhost user=postgres port=5434';
node 2 admin conninfo = 'dbname=gb host=localhost user=postgres port=5430';
node 3 admin conninfo = 'dbname=gb_replica host=localhost user=postgres port=5430';

#
# Subscribe Node2 to the set provided by Node 1
#
subscribe set (id=1, provider=1, receiver=2, forward=yes);
echo 'Subscribing Node 2 to set 1 provided by Node 1';

_EOF_

echo "Hit Return to Continue"
read x;

#
# Change the master back to the Node 2
#
echo "Promote Node 2 back to the master Node."
slonik << _EOF_

cluster name = gb;
node 1 admin conninfo = 'dbname=gb host=localhost port=5434 user=postgres';
node 2 admin conninfo = 'dbname=gb host=localhost port=5430 user=postgres';
node 3 admin conninfo = 'dbname=gb_replica host=localhost port=5430 user=postgres';

# add listener paths if required

#
# lock and move set
#
lock set (id=1, origin=1);
move set (id=1, old origin=1, new origin=2);

# subscribe set if required
subscribe set (id=1, provider=2, receiver=1, forward=yes);

_EOF_

