#!/bin/sh

PGBIN=/home/euler/pg/bin

PGDATA2=/tmp/restored
PGARCHIVE=/tmp/archive

# clean up
$PGBIN/pg_ctl stop -D $PGDATA2

rm -rf $PGDATA2

# restored PGDATA
mkdir $PGDATA2
chmod 0700 $PGDATA2

# testing named restore points
cd $PGDATA2 && tar -zxf $PGARCHIVE/bkp.tgz && cd ~

cp $PGDATA2/postgresql.nrp $PGDATA2/postgresql.conf

# configuration changes
sed -e 's/^max_connections = [0-9][0-9]*/max_connections = 20/' \
	-e 's/^#logging_collector = off/logging_collector = on/' \
	-e 's/^#log_checkpoints = off/log_checkpoints = on/' \
	-e "s/^#log_line_prefix = ''/log_line_prefix = '[%t %u@%r %d] '/" \
	-i $PGDATA2/postgresql.conf

# create recovery.conf
cat <<EOF > $PGDATA2/recovery.conf
restore_command = 'cp $PGARCHIVE/%f %p'
recovery_target_name = 'nrp test 3'
EOF

# start restored cluster
$PGBIN/pg_ctl start -D $PGDATA2

sleep 10

cat $PGDATA2/pg_log/postgresql*

exit 0

