#!/usr/bin/perl -l

use lib qw( ../blib/lib ) ;
use Curses;
use Pg::Pcurse;
use Pg::Pcurse::Widget;
use Data::Dumper;
#use Getopt::Compact;
use strict;
use warnings;

use Pg::Pcurse::Misc;
use Pg::Pcurse::Query0;
use Pg::Pcurse::Query1;
use Pg::Pcurse::Query2;

*VERSION= *Pg::Pcurse::VERSION;

*addstr = \&Curses::Widgets::addstr;

$opt = process_options( get_getopt(), @ARGV);


## Test connection 
unless (my $dh = dbconnect ( $opt, form_dsn($opt, '' ) ) ) {
	print 'Could not connect to database. Exiting...';
	print Dumper $opt;
	init_screen; endwin;
	exit(0);
}


#print Dumper pgbuffercache($opt, 'template1') ; exit;

init_screen;
$::mwh  = create_root;
my $dbs  = [ databases2 $opt ];
my $main = form_dbmenu ($dbs);
undef $::db;
$::mode='buffers';
$::mode='tables';

init_mini_root;

while(2) {
	$main->draw($::mwh,1);
	$main->execute($::mwh);

	# $main::db is initialized from  _Database_Menu_Choice()
        $::dbname  = $main::db || $opt->{dbname} || $dbs->[0] || 'template1';

	# display database name
        $::mwh->addstr(5,10, sprintf 'Mode  : %-13s', ' '   );
        $::mwh->addstr(6,10, sprintf 'DBase : %-13s' , ' '   );
        $::mwh->addstr(5,10, sprintf 'Mode  : %-13s', ucfirst$::mode );
        $::mwh->addstr(6,10, sprintf 'DBase : %-8s', $::dbname       );
        $::mwh->addstr(7,10, sprintf 'User  : %-8s', $opt->{user}    );
        $::mwh->addstr(8,10, sprintf 'Host  : %-8s', $opt->{host}    );
	$main->draw($::mwh,0);

	# act according to ::mode
	execute_mode( $::mode);
}




END { 
	if (defined $::mwh) {
		$::mwh->addstr(23,1,'Thank you for flying Pcurse!');
		$::mwh->refresh;  
	}
        endwin() 
}
1;
__END__

=head1 NAME

pcurse - Monitor  Postgres  databases

=head1 SYNOPSIS

 $ pcurse  [options]   [dbname]   [user]
 for example:
 $ pcurse  template1  john  --passwd apple

=head2 options

      --dbname      Default is template1
      --host        Default is localhost
      --user        Default is getlogin() 
      --passwd      Default is read from ~/.pgpass
      --port        Default is 5432


=head1 DESCRIPTION

pcurse(1) monitors a Postgres database cluster. 
It shows statistics on tables, indexes, vacuuming, buffers, etc,.

=head1 INVOCATION




=head1 USAGE


It is too simple:

1. Select a 'MODE' from the menu.
   To navigate, use arrow keys, the vi keys (j ,k , m ), or ENTER .
   TAB (or 'l') moves you to the next window.

2. Most modes requires to select a namespace from the the 'Schemas' listbox.
   Use arrows, vi keys, ENTER (or SPACE, or ' ' ) seals the selection.
   Press 'd' to view more acl permissions on schema
   TAB (or 'l') moves you to the button window.

3. The results are displayed in the bottom window.
   Use arrows, vi keys, ENTER (or SPACE, or ' ' ) seals the selection.
   Keep pressing 'd' to view more details on results.
   'D'  key displays records of a table (CAUTION: no safe for long tables)
   'A'  key attemps to analyze the table, or database
   'V'  key attemps to vacume  the table, or database
   TBB (or 'l') takes you back to the starting menu.

4. Press 'q' to quit the program



=head1 SEE ALSO

=head1 AUTHOR

Ioannis Tambouras, E<lt>ioannis@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2008 by Ioannis Tambouras

This library is free software; you can redistribute it and/or modify
it under the same terms of GPLv3


=cut

