#!/usr/bin/perl

use strict;
use warnings;

#----------------------------------------------------------------------------
# Library Modules

use FindBin;
use Getopt::Long;
use Pod::Usage;
use lib(File::Spec->catdir($FindBin::Bin, File::Spec->updir, 'lib'));

use App::Maisha;

#----------------------------------------------------------------------------
# Application

{
    my ($config,%options);

    # Default config file is expected to be at the current directory
    $config = do {
        my $file;
        foreach my $suffix qw(ini yml yaml) {
            my $test = "config.$suffix";
            if (-f $test) {
                $file = $test;
                last;
            }
        }
        $file;
    };

    if (! GetOptions(   \%options,
                        'config|c=s',
                        'version|v',
                        'usage|u',
                        'help|h'
    )) {
        usage(1);
    }

    usage(1) if($options{usage});
    usage(0) if($options{version});

    if ($options{help}) {
        pod2usage(-verbose => 2);
        exit(0);
    }

    $options{config} ||= $config;
    usage(1,"No configuration file was specified and a default one could not be found")
                                                                unless(   $options{config});
    usage(1,"Configuration file [$options{config}] not found")  unless(-f $options{config});

    App::Maisha->new(config => $options{config})->run;
}


sub usage {
    my ($full,$mess) = @_;

    print "\n$mess\n"   if($mess);

    if($full) {
        print "\n";
        print "Usage: $0 [--config=<file>] [--help|h] [--usage|u] [--version|v] \n\n";

#              12345678901234567890123456789012345678901234567890123456789012345678901234567890
        print "maisha - Micro-blogging from your command line.\n\n";

        print "Options:\n";
        print "  [--config=<file>]  # path to config file\n";
        print "  [--version]        # program version\n";
        print "  [--usage]          # basic usage help screen\n";
        print "  [--help]           # displays the man page\n";
    }

    print "\n$0 v$App::Maisha::VERSION\n\n";
    exit(0);
}

__END__

=head1 NAME

maisha - Micro-blogging From Your Command Line

=head1 SYNOPSIS

   maisha
   maisha -c config.ini
   maisha -v
   maisha -u
   maisha -h

   # In a config file, specify the following:
   # config.ini
   [CONFIG]

   [Identica]
   username: your@email.address
   password: password

   [Twitter]
   username: your@email.address
   password: password

=head1 DESCRIPTION

maisha gives you access to micro-blogging from your command line

=head1 OPTIONS

=head2 --config | -c

Specify the config file to read from. By default, maisha attempts to read
a config file named config.ini in the current directory

=head2 --version | -v

Print out the version and exit

=head2 --usage | -u

Prints out a basic usage help screen

=head2 --help | -h

Print out the manual page.

=head1 CONFIGURATION

The configuration file must contain at least one micro-blogging service, with
your username and password for that service, all other configuration settings 
are optional.

=head2 Application Configuration

The configuration for the application itself are contained within a 'CONFIG' 
section in your configuration file. All settings are optional, and will default
to sensible values should they not be user defined.

=over 4

=item * prompt

The prompt you wish to see on the command line. This will default to 'twitter>'
if not configured.

=item * tag

The tag you wish to have added to the end of an update/say command. This will 
default to '[from maisha]' if not configured. If you do not wish a tag to
be added at all, set the tag to '.' in the configuration file.

=item * order

Describes the order for printing status messages when a timeline command is
requested. Will default to 'descending' if not configured. Setting is case
insensitive and should be set to 'ascending' or 'asc' to reverse the order.

=item * limit

Describes how many status messages should be printed when a timeline command is
requested. Will default to the last 20 if not configured.

=back

=head2 Service Configuration

For each service you have available (the default install includes 'Identica' 
and 'Twitter') include an associated section in your configuration file, 
providing a username and password for that service. If any service fails to 
connect, a warning is emitted. If the application is not able to connect to
any service, the application will terminate with an error message.

=over 4

=item * username

Your username login to your network service account

=item * password

Your password login to your network service account

=back

=head1 COMMANDS

Note that not all services offer all commands. Where commands are not
applicable for a service, and appropriate warning message will be emitted.

=over 4

=item * friends

Displays the most recent status messages from each of your friends.

=item * friends_timeline | ft [<limit>]

Displays the most recent status messages within your friends timeline.

=item * public_timeline | pt [<limit>]

Displays the most recent status messages within the public timeline.

=item * followers

Displays the most recent status messages from each of your followers.

=item * replies | re [<limit>]

Displays the most recent reply messages.

=item * direct_messages | dm [<limit>]

Displays the most recent direct messages you have received.

=item * send_message | send | sm <user> <message>

Post a direct message to another user. Include the username of the person you
wish to send a message to, followed by your message, of upto 140 characters in
length.

=item * update | say <message>

Posts a new status message, of upto 140 characters in length.

=item * exit | quit | q

Exits the program.

=item * help

Provides help for a specific command, or provides a summary of the commands
available if requested with no arguments.

=back

=head1 AUTHOR

Copyright (c) 2009 Barbie <barbie@cpan.org> Miss Barbell Productions.

=head1 LICENSE

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

See http://www.perl.com/perl/misc/Artistic.html

=cut
