#!/usr/bin/perl -w
# Call a PostgreSQL client program with the version, cluster and default
# database specified in ~/.postgresqlrc or
# /etc/postgresql-common/user_clusters.
#
# (C) 2005 Martin Pitt <mpitt@debian.org>

use strict;

use lib '/usr/share/postgresql-common';
use PgCommon;

my ($version, $cluster, $db, $port, $host);

$host = $ENV{'PGHOST'};

# Check for PGCLUSTER in %ENV
if (defined $ENV{'PGCLUSTER'}) {
    ($version, $cluster) = split ('/', $ENV{'PGCLUSTER'}, 2);
    error 'Invalid version specified with $PGCLUSTER' unless version_exists $version;
    error 'No cluster specified with $PGCLUSTER' unless $cluster;
}

# Check for --cluster argument and filter it out
for (my $i = 0; $i <= $#ARGV; ++$i) {
    if ($ARGV[$i] eq '--cluster') {
        error '--cluster option needs an argument (<version>/<cluster>)' if ($i >= $#ARGV);

        ($version, $cluster) = split ('/', $ARGV[$i+1], 2);
	$host = undef; # --cluster overrides $PGHOST env var
        error 'Invalid version specified with --cluster' unless version_exists $version;
        error 'No cluster specified with --cluster' unless $cluster;

        splice @ARGV, $i, 2;
        last;
    }
}

# Determine $version, $cluster, $db, $port from map files
($version, $cluster, $db) = user_cluster_map() unless $cluster;

# check if we have a network cluster
if (!$host && $cluster && !cluster_exists $version, $cluster) {
    if ($cluster =~ /^(\S+):(\d*)$/) {
	$host = $1;
	$port = $2 || 5432;
    } else {
	error 'Specified cluster does not exist locally and does not specify a remote cluster';
    }
}

if (!$host && $cluster) {
    $port = get_cluster_port($version, $cluster);

    unless ($ENV{'PGHOST'}) {
        # default to cluster specific Unix socket directory
        $ENV{'PGHOST'} = get_cluster_socketdir $version, $cluster;
    }
}

$ENV{'PGSYSCONFDIR'} = '/etc/postgresql-common' if !$ENV{'PGSYSCONFDIR'};
$ENV{'PGPORT'} = $port if $port && !$ENV{'PGPORT'};
$ENV{'PGDATABASE'} = $db if $db && !$ENV{'PGDATABASE'};
$ENV{'PGHOST'} = $host if $host;

unless ($version) {
    if (get_versions) {
	error 'No existing local cluster is suitable as a default target. Please see man pg_wrapper(1) how to specify one.';
    } else {
	error 'You must install at least one postgresql-client-<version> package.';
    }
}

error 'Invalid PostgreSQL cluster version' unless -d "/usr/lib/postgresql/$version";
my $cmd = get_program_path (((split '/', $0)[-1]), $version);
error 'pg_wrapper: invalid command name' unless $cmd;
unshift @ARGV, $cmd;
exec @ARGV;

__END__

=head1 NAME

pg_wrapper - wrapper for PostgreSQL client commands

=head1 SYNOPSIS

I<client-program> [B<--cluster> I<version>/I<cluster>] [...]

(I<client-program>: B<psql>, B<createdb>, B<dropuser>, and all other client
programs installed in C</usr/lib/postgresql/>I<version>C</bin>).

=head1 DESCRIPTION

This program is run only as a link to names which correspond to PostgreSQL
programs in C</usr/lib/postgresql/>I<version>C</bin>. It determines the
configured cluster and database for the user and calls the appropriate version
of the desired program to connect to that cluster and database, supplying any
specifed options to that command.

The target cluster is selected by the following means, in descending order of
precedence:

=over

=item 1.

explicit specification with the B<--cluster> option

=item 2.

explicit specification with the B<PGCLUSTER> environment variable

=item 3.

matching entry in C<~/.postgresqlrc> (see L<postgresqlrc(5)>), if that
file exists

=item 4.

matching entry in C</etc/postgresql-common/user_clusters> (see
L<user_clusters(5)>), if that file exists

=item 5.

If only one local cluster exists, that one will be selected.

=item 6.

If several local clusters exist, the one listening on the default port 5432
will be selected.

=back

If none of these rules match, B<pg_wrapper> aborts with an error.

=head1 OPTIONS

=over

=item B<--cluster> I<version>B</>I<cluster>

I<cluster> is either the name of a local cluster, or takes the form
I<host>:I<port> for a remote cluster. If I<port> is left empty (i. e. you just
specify I<host:>), it defaults to 5432.

=back

=head1 ENVIRONMENT

=over

=item B<PGCLUSTER>

If C<$PGCLUSTER> is set, its value (of the form I<version>/I<cluster>)
specifies the desired cluster, similar to the B<--cluster> option. However, if
B<--cluster> is specified, it overrides the value of C<$PGCLUSTER>.

=back

=head1 FILES

=over

=item C</etc/postgresql-common/user_clusters>

stores the default cluster and database for users and groups as set by
the administrators. 

=item C<$HOME/.postgresqlrc>

stores defaults set by the user himself.

=back

=head1 SEE ALSO

L<user_clusters(5)>, L<postgresqlrc(5)>

=head1 AUTHOR

Martin Pitt L<E<lt>mpitt@debian.orgE<gt>>
