#!/bin/bash
#
# This script add/remove slony nodes
#       Usage: ... [-r] dbname
#               -r: remove (default is to add)
#
#. "${HOME}/bin/tm_tables.sh"
. "${HOME}/bin/tm_slony_setting.sh"


progname=$(basename $0)
function usage() {
	echo "Usage: $progname [-r]|[-d] "
	echo "      -r: remove triggers, default is to add triggers"
}

tm_tables=`psql -tAc "SELECT table_name FROM information_schema.table_constraints WHERE table_catalog='${MASTERDBNAME}' AND table_schema NOT IN ('pg_catalog', 'information_schema') AND constraint_type='PRIMARY KEY' ORDER BY table_name" "${MASTERDBNAME}"`
#echo $tm_tables

tm_sequences=`psql -tAc "SELECT sequence_name FROM information_schema.sequences WHERE sequence_catalog='${MASTERDBNAME}' AND sequence_schema NOT IN ('pg_catalog', 'information_schema') ORDER BY sequence_name;" "${MASTERDBNAME}"`
#echo $tm_sequences

action="add"
if [ "$1" = "-r" ]
then
	shift
	action="del"
fi

gen_table_settings(){
	a=1
	for table in $tm_tables
	do
		table_settings="$table_settings set add table (set id=1, origin=1, id=$a, fully qualified name = 'public.$table', comment='table $table');"
		a=`expr $a + 1`      
	done
}

gen_sequence_settings(){
	a=1
	for sequence in $tm_sequences
	do
		sequence_settings="$sequence_settings set add sequence (set id=1, origin=1, id=$a, fully qualified name = 'public.$sequence', comment='sequence $sequence');"
		a=`expr $a + 1`      
	done
}

if [ "$action" = "del" ]
then
	slonik <<_EOF_
		cluster name = $CLUSTERNAME;
		node 1 admin conninfo = 'dbname=$MASTERDBNAME host=$MASTERHOST user=$REPLICATIONUSER_ON_MASTERHOST port=${MASTERPORT}';
		node 2 admin conninfo = 'dbname=$SLAVEDBNAME host=$SLAVEHOST user=$REPLICATIONUSER_ON_SLAVEHOST port=${SLAVEPORT}';
		uninstall node (id=1);
		uninstall node (id=2);
_EOF_
	exit 0
fi

if [ "$action" = "add" ]
then

		gen_table_settings
		
		gen_sequence_settings
		
		slonik <<_EOF_
			#--
			# define the namespace the replication system uses in our example it is
			# slony_example
			#--
			cluster name = $CLUSTERNAME;
		
			#--
			# admin conninfo's are used by slonik to connect to the nodes one for each
			# node on each side of the cluster, the syntax is that of PQconnectdb in
			# the C-API
			# --
			node 1 admin conninfo = 'dbname=$MASTERDBNAME host=$MASTERHOST user=$REPLICATIONUSER_ON_MASTERHOST port=${MASTERPORT}';
			node 2 admin conninfo = 'dbname=$SLAVEDBNAME host=$SLAVEHOST user=$REPLICATIONUSER_ON_SLAVEHOST port=${SLAVEPORT}';
		
			#--
			# init the first node.  Its id MUST be 1.  This creates the schema
			# _$CLUSTERNAME containing all replication system specific database
			# objects.
		
			#--
			init cluster ( id=1, comment = 'Master Node');
		
			#--
			# Slony-I organizes tables into sets.  The smallest unit a node can
			# subscribe is a set.  The following commands create one set containing
			# all 4 pgbench tables.  The master or origin of the set is node 1.
			#--
			create set (id=1, origin=1, comment='tm tables and sequences');
		
			$table_settings
			$sequence_settings
		
			#--
			# Create the second node (the slave) tell the 2 nodes how to connect to
			# each other and how they should listen for events.
			#--
		
			store node (id=2, comment = 'Slave node', event node=1);
			store path (server = 1, client = 2, conninfo='dbname=$MASTERDBNAME host=$MASTERHOST user=$REPLICATIONUSER_ON_MASTERHOST port=${MASTERPORT}');
			store path (server = 2, client = 1, conninfo='dbname=$SLAVEDBNAME host=$SLAVEHOST user=$REPLICATIONUSER_ON_SLAVEHOST port=${SLAVEPORT}');	
_EOF_
		exit 0

fi
