#!/usr/bin/env perl
use Net::Twitter;
use Getopt::Long;
use App::Twpin ':all';
use Carp qw/carp croak/;

our $VERSION = 0.003;

my @commands = qw/config update/;
my $cmd      = shift @ARGV;

die "Not implemented on Win32 yet" if $^O eq "MSWin32";
my $twpin;

if (!defined $cmd) {
    usage();
} elsif ($cmd eq 'config') {
    my ($user, $password, $apiurl, %opt, $encode);
    GetOptions(
        "user=s"   => \$user,
        "pass=s"   => \$pass,
        "apiurl=s" => \$apiurl,
        "encode=s" => \$encode,
    ) or usage();

    !$user && !$pass && !$apiurl && !$encode && usage();
    $user   and $opt{user}   = $user;
    $pass   and $opt{pass}   = $pass;
    $apiurl and $opt{apiurl} = $apiurl;
    $encode and $opt{encode} = $encode;

    tw_config(%opt);
} elsif ($cmd eq 'follow') {
    my $user;
    usage() unless $user = shift;
    tw_follow(get_instance(), $user);
} elsif ($cmd eq 'unfollow') {
    my $user;
    usage() unless $user = shift;
    tw_unfollow(get_instance(), $user);
} elsif ($cmd eq 'update') {
    tw_update(get_instance(), @ARGV);
} elsif ($cmd eq 'status') {
    tw_list(get_instance());
} elsif ($cmd eq 'following') {
    tw_get_following(get_instance());
} elsif ($cmd eq 'follower') {
    tw_get_follower(get_instance());
} else {
    usage();
}

sub usage {
    print STDERR <<"usage";

twpin version $VERSION
twpin config [-u|--user] [-p|--pass] [-a|--apiurl]
        build or rebuild configuration file
      status 
        list recent tweets
      update "tweets"
        update tweets to twitter
      follow screen_name
        follow somebody on twitter
      follower
        list your followers
      following
        list your followings
      help
        show this message

usage
    exit;
}

sub get_instance {

    if (!$twpin) {
        $twpin = Net::Twitter->new(
            traits => [qw/API::REST/],
            tw_load_config(),
        ) or croak "error";
    }
    return $twpin;
}

__END__

=head1 NAME

twpin - Just Another Command Line Twitter Client

=head1 VERSION

version 0.003

=head1 SYNOPSIS
    
    twpin config -u username -p password
    twpin config -a 'http://url/twip'
    twpin update "hello twitter"
    twpin status
    twpin follow perl_api
    twpin help

=head1 DESCRIPTION

C<twpin> is a script for you to access twitter from command line

twip L<http://code.google.com/p/twip/> is a twitter API proxy in PHP. This script is created mainly because I can not find a good twitter client that supports this proxy 

Configration file is located at $HOME/.twpinrc, you can just edit this file and add your username/password there.

By default, the term encoding is set to utf8. If you are using other encodings, set encode = STH in the configuration file(or use 'twpin config -e').

=head1 AUTHOR

woosley.xu<redicaps@gmail.com>

=head1 COPYRIGHT & LICENSE

This software is copyright (c) 2010 by woosley.xu.

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