#!/bin/sh

 # Copyright (c) 2008 Pierre-Emmanuel Andre <pea@raveland.org>
 # All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions
 # are met:
 #
 # 1. Redistributions of source code must retain the above copyright
 #    notice, this list of conditions and the following disclaimer.
 # 2. The name of the author may not be used to endorse or promote products
 #    derived from this software without specific prior written permission.
 #
 # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 # AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 # THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 # EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #

usage="usage: xloginfo [-H host]\n\
+ Write_Location: displays the current transaction log write location\n\
+ Insert_Location: displays the current transaction log insertion point\n\
The insertion point is the \"logical\" end of the transaction log at any\n\
instant, while the write location is the end of what has actually been\n\
written out from the server's internal buffers. The write location is\n\
the end of what can be examined from outside the server, and is usually\n\
what you want if you are interested in archiving partially-complete\n\
transaction log files.\n\
The insertion point is made available primarily for server debugging purposes.
"

HOST=""
DATABASE="template1"

while getopts "H:h" options; do
  case $options in
    H ) HOST="-h $OPTARG";;
    h ) echo -e $usage
         exit 1;;
    \? ) echo -e $usage
         exit 1;;
    * ) echo -e $usage
          exit 1;;
  esac
done

Q="select pg_xlogfile_name(pg_current_xlog_location()) as Filename, \
pg_current_xlog_location() as Write_Location, \
pg_current_xlog_insert_location() as Insert_Location"

psql $HOST -d $DATABASE -c "$Q"
RETURN=$?

if [ $RETURN -ne 0 ]
then
    echo "\nTry $(basename $0) -h to see help"
fi

exit $RETURN

=head1 NAME

xloginfo - Returns xlog informations

=head1 SYNOPSIS

xloginfo [-H hostname]

=head1 OPTIONS

=over

=item -H hostname

connect to database server B<hostname>

=back

=head1 ENVIRONMENT

B<PGDATABASE>, B<PGHOST>, B<PGPORT>, B<PGUSER> as described in psql(1).

=head1 AUTHOR

the "xloginfo" program: Pierre-Emmanuel Andre < pea at raveland.org>

=cut
