#!/bin/sh
unset PATH
path_rest='/opt/perl/bin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/rijkers/bin'
unset PGDATA PGPORT PGDATABASE
export PGPASSFILE=$HOME/.pg_$USER
/usr/bin/env | /bin/grep ^PG

project1=vanilla_1
project2=vanilla_2

primary_port=6564
standby_port=6664
 primary_dir=/var/data1/pg_stuff/pg_installations/pgsql.$project1
 standby_dir=/var/data1/pg_stuff/pg_installations/pgsql.$project2
primary_data=$primary_dir/data
standby_data=$standby_dir/data
 primary_bin=$primary_dir/bin
 standby_bin=$standby_dir/bin

# stop any remaing standby
export PATH=$standby_bin:$path_rest
pg_ctl -D $standby_data -l $standby_dir/logfile stop

echo "local   replication		rijkers								md5
local   all             all                                     md5
host    replication		rijkers		    127.0.0.1/32			md5
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5
" > $primary_dir/data/pg_hba.conf

# primary:
perl -iold -ne "
s{^(#?data_directory\s*=\s*)}           {data_directory = '/var/data1/pg_stuff/pg_installations/pgsql.$project1/data'     ### \\1};
s{^(#?listen_addresses\s*=\s*)}         {listen_addresses = '*'            ### \\1};
s{^(#?wal_level\s*=\s*)}                {wal_level = hot_standby           ### \\1};
s{^(#?max_wal_senders\s*=\s*)}          {max_wal_senders = 5               ### \\1};
s{^(#?archive_mode\s*=\s*)}	            {archive_mode = on                 ### \\1};
s{^(#?archive_command\s*=\s*)}          {archive_command = 'cp %p /var/data1/pg_stuff/archive_dir/%f < /dev/null'   ### \\1};
s{^(#?synchronous_replication\s*=\s*)}  {synchronous_replication = on      ### \\1};
print;
" $primary_dir/data/postgresql.conf

export PATH=$primary_bin:$path_rest
pg_ctl -D $primary_data -l $primary_dir/logfile -w restart
rm -rf /var/data1/pg_stuff/pg_installations/pgsql.$project2/data
mkdir -p ${standby_dir}/data
chmod 0700 ${standby_dir}/data
rm -vrf /var/data1/pg_stuff/archive_dir/*
cd . ;
which pg_basebackup
pg_basebackup -h /tmp -p $primary_port -D $standby_dir/data
	rc=$?; if [[ $rc -ne 0 ]]; then echo "--  pg_basebackup returned [$rc]"; exit $rc; fi
mkdir -p ${standby_dir}/data/pg_xlog

# standby:
perl -iold -ne "
s{^(#?port\s*=\s*)}                     {port = $standby_port              ### \\1};
s{^(#?data_directory\s*=\s*)}           {data_directory = '/var/data1/pg_stuff/pg_installations/pgsql.$project2/data'  ### \\1};
s{^(#?hot_standby\s*=\s*)}              {hot_standby = on                 ### \\1};
s{^(#?synchronous_replication\s*=\s*)}  {synchronous_replication = off    ### \\1};
print;
" $standby_dir/data/postgresql.conf

cp /var/data1/pg_stuff/archive_dir/* $standby_dir/data/pg_xlog

echo "primary_conninfo = 'host=127.0.0.1 port=6564 user=rijkers password=xxxxxxx'
standby_mode = 'on'
restore_command = 'cp /var/data1/pg_stuff/archive_dir/%f %p < /dev/null'
" > $standby_dir/data/recovery.conf

echo
export PGPORT=$primary_port
export PGDATA=$primary_data
export PATH=$primary_bin:$path_rest
pg_config | grep BINDIR
env | grep ^PG
which pg_ctl
pg_ctl -D $primary_data -l $primary_dir/logfile -w restart

echo
export PGPORT=$standby_port
export PGDATA=$standby_data
export PATH=$standby_bin:$path_rest
pg_config | grep BINDIR
env | grep ^PG
which pg_ctl
echo "pg_ctl -D $standby_data -l $standby_dir/logfile -w restart;"
      pg_ctl -D $standby_data -l $standby_dir/logfile -w restart;     # <-- HERE

echo
echo "-- logfile 1:"
tail -n 4 $primary_dir/logfile
echo
echo "-- logfile 2:"
tail -n 4 $standby_dir/logfile
echo
