#!/bin/bash -x

BASEDIR=/usr/lib/postgresql
. /etc/sysconfig/postgresql

if test -n "$POSTGRES_DEFAULTVERSION"; then
    PGBINDIR=$BASEDIR$POSTGRES_DEFAULTVERSION/bin
else
    for f in $(ls -d $BASEDIR[1-8]?/bin); do
	if test -x $f/psql; then
	    PGBINDIR=$f
	fi
    done
fi

BIN=$PGBINDIR/"${0##*/}"

test -x $BIN && exec $BIN ${1+"$@"}

echo Cannot find a suitable executable for "${0##*/}".
exit 1
