pkill -9 postgres
rm -rf data
rm -rf standby
rm standby.log
rm logfile
rm Results.log

LOGFILE="Results.log"

./initdb -D data -c "wal_level=logical" -c "max_prepared_transactions = 10" -c "listen_addresses=*" -c "max_slot_wal_keep_size=300"

cp pg_hba_primary.conf data/pg_hba.conf

./pg_ctl -D data -l logfile -c start

./psql -d postgres -c "CREATE ROLE replication WITH REPLICATION PASSWORD 'password' LOGIN;"
./psql -d postgres -c "SELECT * FROM pg_create_physical_replication_slot('replica_1', true);"

./pg_ctl -D data -l logfile -c restart

./psql -d postgres -c "create database db1;"
./psql -d postgres -c "create database db2;"
./psql -d postgres -c "CREATE TABLE public.t1 (id int, val text);"
./psql -d db1 -c "CREATE TABLE public.t2 (id int, val text);"
./psql -d db2 -c "CREATE TABLE public.t3 (id int, val text, extra int);"

./psql -d postgres -c "CREATE PUBLICATION pub_existsA FOR table public.t1;"
./psql -d postgres -c "CREATE PUBLICATION pub_existsB FOR table public.t1;"
./psql -d postgres -c "CREATE PUBLICATION pub_existsC FOR table public.t1;"
./psql -d postgres -c "CREATE PUBLICATION pub_existsD FOR table public.t1;"

./psql -d db1 -c "CREATE PUBLICATION pub_existsa FOR table public.t2;"
./psql -d db1 -c "CREATE PUBLICATION pub_existsb FOR table public.t2;"
./psql -d db1 -c "CREATE PUBLICATION pub_existsc FOR table public.t2;"
./psql -d db1 -c "CREATE PUBLICATION pub_existsd FOR table public.t2;"

./psql -d db2 -c "CREATE PUBLICATION pub_exists1 FOR table public.t3;"
./psql -d db2 -c "CREATE PUBLICATION pub_exists2 FOR table public.t3;"
./psql -d db2 -c "CREATE PUBLICATION pub_exists3 FOR table public.t3;"
./psql -d db2 -c "CREATE PUBLICATION pub_exists4 FOR table public.t3;"

./pg_basebackup -h 127.0.0.1 -D /home/shubham/Project/Git/postgres/inst/bin/standby -P -U replication -R

cp standby.signal standby/

echo "wal_level=logical
\n listen_addresses='*'
\n Port=9999
\n max_logical_replication_workers=5
\n max_sync_workers_per_subscription=1" >> standby/postgresql.conf

sleep 3

./pg_ctl -D standby -l standby.log -o "-p 9999" -c start

sleep 2

./psql -d postgres -c "ALTER SYSTEM SET wal_level = 'logical';"
./psql -d postgres -c "ALTER SYSTEM SET listen_addresses = '*';"
./psql -d postgres -c "ALTER SYSTEM SET port = 9999;"
./psql -d postgres -c "ALTER SYSTEM SET max_logical_replication_workers = 5;"
./psql -d postgres -c "ALTER SYSTEM SET max_sync_workers_per_subscription = 1;"

./pg_ctl -D standby -l standby.log -c stop

# ---------------------------------------------------------------------
# Test Cases
# ---------------------------------------------------------------------

run_case() {
  local case_no=$1
  shift
  echo "==================== TEST CASE $case_no ====================" | tee -a "$LOGFILE"
  echo "Command: $*" | tee -a "$LOGFILE"
  eval "$@" >> "$LOGFILE" 2>&1
  echo >> "$LOGFILE"
}

run_case 1 "./pg_createsubscriber -D standby/ -P \"host=localhost port=5432 dbname=postgres\" -d db2 --publication pub_new2 --clean=publications --dry-run --verbose"
run_case 2 "./pg_createsubscriber -D standby/ -P \"host=localhost port=5432 dbname=postgres\" -d db2 --publication pub_new2 --dry-run --verbose"
run_case 3 "./pg_createsubscriber -D standby/ -P \"host=localhost port=5432 dbname=postgres\" -d db1 --publication pub_existsa --clean=publications --dry-run --verbose"
run_case 4 "./pg_createsubscriber -D standby/ -P \"host=localhost port=5432 dbname=postgres\" -d db1 --publication pub_existsa --dry-run --verbose"
run_case 5 "./pg_createsubscriber -D standby/ -P \"host=localhost port=5432 dbname=postgres\" --clean=publications --dry-run --verbose"
run_case 6 "./pg_createsubscriber -D standby/ -P \"host=localhost port=5432 dbname=postgres\" --dry-run --verbose"
echo "========== All test cases completed ==========" | tee -a "$LOGFILE"
