#!/bin/sh
#
# Copyright (c) 2025 Ken McDonell.  All Rights Reserved.
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
# 
# Get the PostgreSQL server port
#

# the historical default
#
port=5432

if [ -d /etc/postgresql ]
then
    # punt on highest active port number
    #
    active=`grep -r '^port *=' /etc/postgresql 2>/dev/null \
	| sed -e 's/#.*//' -e 's/ //g' -e 's/^.*port=//' \
	| LC_COLLATE=POSIX sort -nr \
	| head -1`
    [ -n "$active" ] && port="$active"
fi

echo $port
