#!/bin/bash

set -e

PRIMARY_PGDATA=$HOME/data/primary
STANDBY_PGDATA=$HOME/data/standby
PRIMARY_TBSPACE=$HOME/data/tbspace
STANDBY_TBSPACE=$HOME/data/tbspace_standby

rm -rf $PRIMARY_PGDATA $STANDBY_PGDATA
rm -rf $PRIMARY_TBSPACE
rm -rf $STANDBY_TBSPACE

mkdir $PRIMARY_TBSPACE

# Create primary with its tablespace
initdb --noclean --nosync -D $PRIMARY_PGDATA
cat >> $PRIMARY_PGDATA/postgresql.conf <<EOF
logging_collector = on
log_directory = 'log'
wal_keep_segments=20
wal_log_hints=on
port=5432
EOF
pg_ctl start -D $PRIMARY_PGDATA
createdb $USER
# Counts for 350MB of data
psql -p 5432 <<EOF
create tablespace toto location '$PRIMARY_TBSPACE';
create table aa (a int) tablespace toto;
insert into aa values (generate_series(1,1000000));
EOF

# Create standby with its tablespace
pg_basebackup -D $STANDBY_PGDATA --wal-method=stream \
			  -T $PRIMARY_TBSPACE=$STANDBY_TBSPACE \
			  -c fast

cat >> $STANDBY_PGDATA/postgresql.conf <<EOF
port=5433
EOF
cat > $STANDBY_PGDATA/recovery.conf <<EOF
standby_mode = on
recovery_target_timeline = 'latest'
primary_conninfo = 'port=5432 application_name=node_5433'
EOF
pg_ctl start -D $STANDBY_PGDATA/

# Enforce streaming of some data
psql -p 5432 <<EOF
insert into aa values (generate_series(1,1));
EOF

# Promote and diverge, prepare for rewind
pg_ctl promote -D $STANDBY_PGDATA/
psql -p 5433 -c 'checkpoint;'

# Diverge data between both bodes
psql -p 5432 <<EOF
insert into aa values (generate_series(1,1));
EOF
psql -p 5433 <<EOF
insert into aa values (generate_series(1,4));
create table ab tablespace toto as select generate_series (1,100000) as b;
EOF

# Rewind and replug
pg_ctl stop -D $PRIMARY_PGDATA
pg_rewind -D $PRIMARY_PGDATA --source-server="port=5433" \
 --debug > $HOME/data/rewind_logs.log
echo "port=5432" >> $PRIMARY_PGDATA/postgresql.conf
cat > $PRIMARY_PGDATA/recovery.conf <<EOF
standby_mode = 'on'
primary_conninfo = 'port=5433'
EOF
pg_ctl start -D $PRIMARY_PGDATA
