#!env bash

set -e
#set -x
# show used binaries
type pg_ctl
type pg_basebackup
type initdb
type pgbench

export PGHOST=/tmp/
export PGUSER=postgres

#cleanup
rm -rf /tmp/pgbt-master
rm -rf /tmp/pgbt-standby
rm -rf /tmp/pgbt-standby-tar
rm -rf /tmp/pgbt-standby-xlogdir
rm -rf /tmp/pgbt-standby-standby
rm -rf /tmp/pgbt-xlog

# create primary data directory
initdb -U postgres /tmp/pgbt-master > /dev/null 2>&1
mkdir /tmp/pgbt-master/archive

cat > /tmp/pgbt-master/pg_hba.conf <<EOF
local   all             all                                     trust
local   replication     all                                trust
EOF

options="-c wal_level=hot_standby -c max_wal_senders=10 -c wal_keep_segments=10 -c archive_mode=on -c archive_command='cp -v %p archive/%f' -c logging_collector=on -c log_filename=postgresql.log -c log_error_verbosity=verbose -c log_min_messages=debug1 -c client_min_messages=warning"
pg_ctl -w -D /tmp/pgbt-master -o "${options} -c port=5431" --log /tmp/pgbt-master/startup.log start > /dev/null
trap "pg_ctl stop -w -D /tmp/pgbt-master > /dev/null" INT QUIT TERM EXIT

echo "plain basebackup without xlog"
pg_basebackup -h /tmp -p 5431 -D /tmp/pgbt-standby
rm -rf /tmp/pgbt-standby

echo "basebackup in -x/X fetch mode"
pg_basebackup -h /tmp -p 5431 -D /tmp/pgbt-standby -x

echo "verifying basebackup starts directly"
pg_ctl -w -D /tmp/pgbt-standby -o "${options} -c port=5432" --log /tmp/pgbt-master/startup.log start > /dev/null
pg_ctl -w -D /tmp/pgbt-standby stop > /dev/null
rm -rf /tmp/pgbt-standby

if [ "$1" -gt 91 ]; then
    echo "basebackup in stream mode"
    pg_basebackup -h /tmp -p 5431 -D /tmp/pgbt-standby -X stream

    echo "verifying basebackup starts directly"
    pg_ctl -w -D /tmp/pgbt-standby -o "${options} -c port=5432"  --log /tmp/pgbt-standby/startup.log start > /dev/null
    pg_ctl -w -D /tmp/pgbt-standby stop > /dev/null
    rm -rf /tmp/pgbt-standby
fi

if [ "$1" -gt 92 ]; then
    echo "Verifying pg_receivexlog works correctly"
    mkdir -p /tmp/pgbt-xlog
    pg_receivexlog -n -h /tmp -p 5431 -D /tmp/pgbt-xlog &
    sleep 1 #wait for file to be streamed
    psql -q -X -p 5431 -c 'create table somewal1()'
    psql -q -X -p 5431 -c 'SELECT pg_switch_xlog()'
    psql -q -X -p 5431 -c 'create table somewal2()'
    sleep 1 #wait for file to be streamed
    kill -INT %1
    wait %1
    find /tmp/pgbt-xlog
fi

echo "verifying basebackup -x/X fetch basebackup starts with recovery.conf"
pg_basebackup -h /tmp -p 5431 -D /tmp/pgbt-standby -x
cat > /tmp/pgbt-standby/recovery.conf <<EOF
primary_conninfo='port=5431'
standby_mode='on'
EOF
pg_ctl -w -D /tmp/pgbt-standby -o "${options} -c port=5432 -c hot_standby=on"  --log /tmp/pgbt-standby/startup.log start > /dev/null
pg_ctl -w -D /tmp/pgbt-standby stop > /dev/null
rm -rf /tmp/pgbt-standby

if [ "$1" -gt 91 ]; then
    echo "verifying basebackup -X stream basebackup starts with recovery.conf"
    pg_basebackup -h /tmp -p 5431 -D /tmp/pgbt-standby -X stream
    cat > /tmp/pgbt-standby/recovery.conf <<EOF
primary_conninfo='port=5431'
standby_mode='on'
EOF
    pg_ctl -w -D /tmp/pgbt-standby -o "${options} -c port=5432 -c hot_standby=on"  --log /tmp/pgbt-standby/startup.log start > /dev/null
    pg_ctl -w -D /tmp/pgbt-standby stop > /dev/null > /dev/null
    rm -rf /tmp/pgbt-standby
fi

#verify things work with the --xlogdir option
if [ "$1" -gt 93 ]; then
    echo "basebackup -X fetch basebackup combines with --xlogdir"
    pg_basebackup -h /tmp -p 5431 -D /tmp/pgbt-standby -X fetch --xlogdir /tmp/pgbt-standby-xlogdir
    rm -rf /tmp/pgbt-standby-xlogdir
    rm -rf /tmp/pgbt-standby

    echo "basebackup -X stream basebackup combines with --xlogdir"
    pg_basebackup -h /tmp -p 5431 -D /tmp/pgbt-standby -X stream --xlogdir /tmp/pgbt-standby-xlogdir
    rm -rf /tmp/pgbt-standby-xlogdir
    rm -rf /tmp/pgbt-standby
fi

# verify -x/-X fetch combines well with tar mode
echo "basebackup -x/-X fetch basebackup combines --format=tar"
pg_basebackup -h /tmp -p 5431 -D /tmp/pgbt-standby-tar -x --format=t
mkdir /tmp/pgbt-standby
chmod 700 /tmp/pgbt-standby
(cd /tmp/pgbt-standby && tar xf /tmp/pgbt-standby-tar/base.tar)
echo "verify -x/-X fetch basebackup combines --format=tar starts"
pg_ctl -w -D /tmp/pgbt-standby -o "${options} -c port=5432 -c hot_standby=on"  --log /tmp/pgbt-standby/startup.log start > /dev/null
pg_ctl -w -D /tmp/pgbt-standby stop > /dev/null
rm -rf /tmp/pgbt-standby
rm -rf /tmp/pgbt-standby-tar

# verify we can create a base backup from a base backup and start it
if [ "$1" -gt 91 ]; then
    echo "verifying basebackup from basebackup starts with recovery.conf"
    pg_basebackup -h /tmp -p 5431 -D /tmp/pgbt-standby -X fetch
    cat > /tmp/pgbt-standby/recovery.conf <<EOF
primary_conninfo='port=5431'
standby_mode='on'
EOF
    pg_ctl -w -D /tmp/pgbt-standby -o "${options} -c port=5432 -c hot_standby=on"  --log /tmp/pgbt-standby/startup.log start > /dev/null

    pg_basebackup -h /tmp -p 5432 -D /tmp/pgbt-standby-standby -X fetch
    cat > /tmp/pgbt-standby-standby/recovery.conf <<EOF
primary_conninfo='port=5432'
standby_mode='on'
EOF

    pg_ctl -w -D /tmp/pgbt-standby-standby -o "${options} -c port=5433 -c hot_standby=on"  --log /tmp/pgbt-standby-standby/startup.log start > /dev/null

    pg_ctl -w -D /tmp/pgbt-standby stop > /dev/null > /dev/null
    pg_ctl -w -D /tmp/pgbt-standby-standby stop > /dev/null > /dev/null

    rm -rf /tmp/pgbt-standby-standby
    rm -rf /tmp/pgbt-standby
fi

echo "verifying promoting a standby"
pg_basebackup -h /tmp -p 5431 -D /tmp/pgbt-standby -x
cat > /tmp/pgbt-standby/recovery.conf <<EOF
primary_conninfo='port=5431'
standby_mode='on'
EOF
pg_ctl -w -D /tmp/pgbt-standby -o "${options} -c port=5432 -c hot_standby=on"  --log /tmp/pgbt-standby/startup.log start > /dev/null
pg_ctl -w -D /tmp/pgbt-standby promote > /dev/null > /dev/null
sleep 1
psql -q -X -p 5432 -c 'create table frak()'

echo 'verify taking a basebackup from a promoted standby'
pg_basebackup -h /tmp -p 5432 -D /tmp/pgbt-standby-standby -x
cat > /tmp/pgbt-standby-standby/recovery.conf <<EOF
primary_conninfo='port=5432'
standby_mode='on'
EOF

pg_ctl -w -D /tmp/pgbt-standby-standby -o "${options} -c port=5433 -c hot_standby=on"  --log /tmp/pgbt-standby-standby/startup.log start > /dev/null
psql -q -X -p 5432 -c 'create table frak2()'
psql -q -X -p 5433 -c '\d frak'
sleep 1
psql -q -X -p 5433 -c '\d frak2'

pg_ctl -w -D /tmp/pgbt-standby-standby promote > /dev/null > /dev/null
sleep 1
psql -q -X -p 5433 -c '\d frak2'
psql -q -X -p 5433 -c 'create table frak3()'

pg_ctl -w -D /tmp/pgbt-standby stop > /dev/null > /dev/null
pg_ctl -w -D /tmp/pgbt-standby-standby stop > /dev/null > /dev/null
rm -rf /tmp/pgbt-standby
