#!/usr/bin/perl

use strict;
use warnings;

use Config::Tiny;
use Curses;
use Error qw(:try);
use Getopt::Long;
use Pod::Usage;
use RT::Client::Console;

main() unless caller;

sub main {
    my %options;
    GetOptions(\%options,
               'server=s',
               'user=s',
               'pass=s',
               'config-file=s',
               'generate-config',
               'help',
               'version|V',
               'debug',
              ) or usage();

    $options{help} and usage();
    $options{version} and print "rtconsole v$RT::Client::Console::VERSION\n" and exit;

    # Unless debug is set, we redirect STDERR => /dev/null
    if (!$options{debug}) {
        close STDERR;
        open STDERR, ">>/dev/null";
    }

    my $config = {};
    exists $options{$_} and $config->{connection}{$_} = $options{$_} foreach (qw(server user pass));

    my $config_filename = $ENV{HOME} . '/.rtconsolerc';
    exists $options{'config-file'} && length $options{'config-file'} and $config_filename = $options{'config-file'};

    my $config_ini;
    if ($options{'generate-config'}) {
        if (! -e $config_filename) {
            $config_ini = Config::Tiny->new() or
              die "Couldn't open $config_filename to save the configuration. The error was : $Config::Tiny::errstr \n";
            
        } else {
            $config_ini = Config::Tiny->read($config_filename) or
              die "Couldn't open $config_filename to save the configuration. The error was : $Config::Tiny::errstr \n";
        }
        $config_ini->{$_} = $config->{$_} foreach keys %$config;
        $config_ini->write($config_filename) or
          die "Couldn't open $config_filename to save the configuration. The error was : $Config::Tiny::errstr \n";
        print "wrote configuration to file $config_filename\n";
        return;
    }
    
    if (-e $config_filename) {
        $config_ini = Config::Tiny->read($config_filename) or
          die "Couldn't open $config_filename to save the configuration. The error was : $Config::Tiny::errstr \n";
        $config->{$_} = $config_ini->{$_} foreach keys %$config_ini;
    }
    
    
    my $curses_handler = new Curses;
    noecho();
    nodelay(1);
    $curses_handler->keypad(1);
    $curses_handler->syncok(1);
    curs_set(0);
    leaveok(1);
    
    # Erase the main window
    
    $curses_handler->erase();
    
    try {
        RT::Client::Console->run(curses_handler => $curses_handler,
                                 rt_servername => $config->{connection}{server},
                                 rt_username => $config->{connection}{user},
                                 rt_password => $config->{connection}{pass},
                                );
        endwin;
    } otherwise {
        endwin;
        print STDERR "\n\n ---- Main Error Message :\n$@\n";
    };
}

sub usage {
    pod2usage( {
                -verbose => 1,
               }
             );
}

__END__

=head1 NAME

rtconsole - RT text client console

=head1 VERSION

version 0.1

=head1 USAGE

    rtconsole [options]

=head1 OPTIONS

Options can be set --like=this, --like this, or -l this

=over

=item  --server server_name

Specify the RT server

=item  --user user_name

Specify the user to connect to the server

=item  --pass password

Specify the password to connect to the server

=item  --config-file filename

Specify the config file to read. Default $HOME/.rtconsolerc

=item --generate-config

Generates the config file

=item --help

Print this help

=item --version

Print the program name and version, then exit

=item --debug

Outputs errors and debug messages on STDERR. enable this to see what's going
wrong. I recommend redirecting STDERR to a temp file to avoid breaking the
display

=back

=head1 DESCRIPTION

rtconsole is a text client to RT using ncurses.

=head1 FILES

The config file $HOME/.rtconsolerc (see the --config-file option) can be use to
set options. The format is .ini file style. Here is an example with all
possible keys/values for now :

  [connection]
  server=rt.cpan.org
  user=dams
  pass=my_password

For now there is only one section cannled Connection. More will be added in
the next versions of this software.

You can generate the config file by using --generate-config. It'll be saved at
the location sp--config-file, or ad the default location

=head1 AUTHOR

Damien "dams" Krotkine (DAMS@CPAN.org)

=head1 BUGS

There are undoubtedly serious bugs lurking somewhere in this code.
Bug reports and other feedback are most welcome.

=head1 COPYRIGHT

Copyright (c) 2007, Damien Krotkine. All Rights Reserved.
This module is free software. It may be used, redistributed
and/or modified under the terms of Perl itself

=cut
