#!/bin/bash
echo "Cleaning.."
rm -rf *_logfile
rm -rf /tmp/archive_dir
mkdir /tmp/archive_dir

export PGPORT=5432			#MASTER PORT
PGSQL_DIR=$HOME/pgsql
PGSQL_BIN=$PGSQL_DIR/bin
PGSQL_MASTER=$HOME/data/master		#DATA FOLDER FOR PRIMARY/MASTER SERVER
PGSQL_STANDBY=$HOME/data/standby	#DATA FOLDER FOR BACKUP/STANDBY SERVER

#Cleanup the master and slave data directory and create a new one.
rm -rf $PGSQL_MASTER $PGSQL_STANDBY
mkdir $PGSQL_MASTER $PGSQL_STANDBY
chmod 700 $PGSQL_MASTER
chmod 700 $PGSQL_STANDBY

#Initialize MASTER
$PGSQL_BIN/initdb -D $PGSQL_MASTER
echo "wal_level = hot_standby" >> $PGSQL_MASTER/postgresql.conf
echo "max_wal_senders = 3" >> $PGSQL_MASTER/postgresql.conf
echo "max_wal_size = 5GB" >> $PGSQL_MASTER/postgresql.conf
echo "min_wal_size = 5GB" >> $PGSQL_MASTER/postgresql.conf
echo "wal_keep_segments = 10" >> $PGSQL_MASTER/postgresql.conf
echo "hot_standby = on" >> $PGSQL_MASTER/postgresql.conf
echo "wal_consistency_checking = 'all'" >> $PGSQL_MASTER/postgresql.conf
echo "archive_mode = on" >> $PGSQL_MASTER/postgresql.conf
echo "archive_command = 'cp %p /tmp/archive_dir/%f'" >> $PGSQL_MASTER/postgresql.conf
echo "wal_log_hints=on" >> $PGSQL_MASTER/postgresql.conf

#Setup replication settings
echo "local   replication     $USER                               trust" >> $PGSQL_MASTER/pg_hba.conf
echo "host    replication     $USER       127.0.0.1/32            trust" >> $PGSQL_MASTER/pg_hba.conf
echo "host    replication     $USER       ::1/128                 trust" >> $PGSQL_MASTER/pg_hba.conf

#Start Master
export PGPORT=5432
echo "Starting Master.."
$PGSQL_BIN/pg_ctl -D $PGSQL_MASTER -c -w -l master_logfile start

#Perform Backup in the Standy Server
$PGSQL_BIN/pg_basebackup -D $PGSQL_STANDBY
cp $PGSQL_DIR/share/recovery.conf.sample $PGSQL_STANDBY/recovery.conf
echo "standby_mode = on" >> $PGSQL_STANDBY/recovery.conf
echo "primary_conninfo = 'host=localhost port=5432'" >> $PGSQL_STANDBY/recovery.conf
echo "restore_command = 'cp /tmp/archive_dir/%f %p'" >> $PGSQL_STANDBY/recovery.conf

#Start STANDBY
export PGPORT=5433
echo "Starting Slave.."
$PGSQL_BIN/pg_ctl -D $PGSQL_STANDBY -c -w -l slave_logfile start

# Do some WAL-logging
# Create table with large relation file, it needs to be higher than
# 4GB. Use fillfactor to accelerate the data fillup. This gets 4.9GB
# of data in.
$PGSQL_BIN/psql -p 5432 -c "CREATE TABLE t2(a int) WITH (fillfactor=10);" postgres
$PGSQL_BIN/psql -p 5432 -c "SELECT pg_current_wal_lsn() AS lsn_before_insert;" postgres
$PGSQL_BIN/psql -p 5432 -c "INSERT INTO t2 SELECT * FROM generate_series(1,14000000);" postgres
$PGSQL_BIN/psql -p 5432 -c "SELECT pg_current_wal_lsn() AS lsn_after_insert;" postgres

# Issue checkpoint to ease future WAL segment lookup on target node
$PGSQL_BIN/psql -p 5432 -c "CHECKPOINT;CHECKPOINT;" postgres
$PGSQL_BIN/psql -p 5432 -c "SELECT pg_current_wal_lsn() AS lsn_after_checkpoint;" postgres

# Wait for replication to complete, hopefully that should be enough.
sleep 10

#Promote standby
$PGSQL_BIN/pg_ctl promote -w -D $PGSQL_STANDBY/

# Wait a bit more
sleep 2

# Update tuple at the last page on the master to copy it during rewind.
$PGSQL_BIN/psql -p 5432 -c "UPDATE t2 SET a = 14000001 WHERE a = 14000000;" postgres

#Stop master
export PGPORT=5432
echo "Stoping Master.."
$PGSQL_BIN/pg_ctl -D $PGSQL_MASTER -c -w -l master_logfile stop

# Create disturbing data on the standby with some extra WAL.
echo "Creating large file"
#dd if=/dev/zero of=$PGSQL_STANDBY/large.file bs=1024 count=4000000
$PGSQL_BIN/psql -p 5433 -c "INSERT INTO t2 SELECT * FROM generate_series(1,10);" postgres

# Checkpoint on standby to enforce timeline update in control file
$PGSQL_BIN/psql -p 5433 -c "CHECKPOINT;" postgres

# Perform the rewind.
#echo "Rewinding master now"
#$PGSQL_BIN/pg_rewind -D $PGSQL_MASTER --debug --progress \
#		--source-server="port=5433 dbname=postgres"
