#!/usr/bin/env bash

# setup publisher node
./pg_ctl stop -D ../publisher
rm -rf ../publisher publisher.log
 
mkdir ../publisher
./initdb -D ../publisher
 
cat << EOF >> ../publisher/postgresql.conf
wal_level = logical
wal_sender_timeout = 0
listen_addresses = '*'
EOF
 
./pg_ctl start -D ../publisher -l publisher.log

./psql -d postgres -c "CREATE TABLE t0 (i int);"
./psql -d postgres -c "CREATE TABLE t1 (a regclass, b regproc, c regtype);"
./psql -d postgres -c "create publication pub1 for table t1"
./psql -d postgres -c "ALTER TABLE t1 REPLICA IDENTITY FULL"
./psql -d postgres -c "INSERT INTO t1 values('t1', 64, 'int')"


# setup subscriber node
cluster_name=subscriber
./pg_ctl stop -D ../$cluster_name
 
rm -rf ../$cluster_name $cluster_name.log
 
mkdir ../$cluster_name
./initdb -D ../$cluster_name
 
cat << EOF >> ../$cluster_name/postgresql.conf
wal_level = logical
wal_receiver_timeout = 0
listen_addresses = '*'
port = 9000
EOF
./pg_ctl start -D ../$cluster_name -l $cluster_name.log

./psql -d postgres -p 9000 -c "CREATE TABLE t1 (a text, b regproc, c regtype);"
./psql -d postgres -p 9000 -c "create subscription test1 connection 'dbname=postgres host=localhost port=5432' publication pub1 with (binary = false);"
sleep 2

# operations on publisher
./psql -d postgres -c "INSERT INTO t1 values('pg_type', 64, 'int')"
./psql -d postgres -c "update t1 set c = 'text' where c = 'int'::regtype;"
./psql -d postgres -c "update t1 set a = 'pg_proc' where a = 'pg_type'::regclass"

 
