#!/bin/bash
set -x

#cleanup
./pg_ctl -D /tmp/test_bkp/slave -l /tmp/test_bkp/slave_logs -o "-p 5433" stop 
./pg_ctl -D /tmp/test_bkp/master -l /tmp/test_bkp/master_logs stop
rm -rf /tmp/test_bkp

mkdir /tmp/test_bkp

#master setup
./initdb -D /tmp/test_bkp/master
mkdir /tmp/test_bkp/archive_dir
echo "archive_mode='on'">>/tmp/test_bkp/master/postgresql.conf
echo "archive_command='cp %p /tmp/test_bkp/archive_dir/%f'">>/tmp/test_bkp/master/postgresql.conf
./pg_ctl -D /tmp/test_bkp/master -o "-p 5432" -l /tmp/test_bkp/master_logs start

#slave setup
./pg_basebackup -p 5432 -Fp -R -D /tmp/test_bkp/slave 
echo "primary_conninfo='host=127.0.0.1 port=5432 user=edb'">>/tmp/test_bkp/slave/postgresql.conf
echo "restore_command='cp /tmp/test_bkp/archive_dir/%f %p'">>/tmp/test_bkp/slave/postgresql.conf
echo "promote_trigger_file='/tmp/test_bkp/failover.log'">>/tmp/test_bkp/slave/postgresql.conf
./pg_ctl -D /tmp/test_bkp/slave -l /tmp/test_bkp/slave_logs -o "-p 5433" start 

#test run
mkdir /tmp/test_bkp/tblsp01
./psql postgres -p 5432 -c "create tablespace tblsp01 location '/tmp/test_bkp/tblsp01';"
./psql postgres -p 5432 -c "create table test (a text) tablespace tblsp01;"

