#!/bin/bash

# Check if this is the standby acting as primary. If so,
# open outside access to port 5432

# This script is NOT SUPPORTED by Red Hat Global Support Services.

set -eu

. /usr/libexec/pgsql-replication.sh

exec >>/var/log/pgsql-standby-check-access.log 2>&1

if [ "$HOSTNAME" != "$STANDBY_HOST" ]; then
    log INFO "Not on $STANDBY_HOST, exiting"
    exit 0
fi

if ! [ -e /opt/rh/postgresql92/root/var/lib/pgsql/data/recovery.conf ]; then
    if ! iptables -L -n | grep -q 5432; then
	log INFO "Opening port 5432"
	iptables_open
    else
	log WARNING "Port 5432 already open"
    fi
else
    log INFO "Running as standby, not opening port 5432"
fi
