#!/usr/bin/env perl
use lib 'lib';
use LWP::Simple;
use JSON;
use utf8;
use warnings;
use constant debug => $ENV{DEBUG};
use App::gh;
use App::gh::Utils;
use App::gh::Command;

$|=1;

binmode STDOUT, ':utf8';

my $act = shift;
$SIG{INT} = sub {
    exit;
};

unless( $act ) {
    print <<END;
App::gh

Available commands:

    search [keyword]                - search repositories
    clone [userid] [repository]     - clone repository
    all [userid]                    - clone all repositories from one
    pull [userid]                   - pull changes from one's fork
    fork [userid]                   - fork one's repository
    list [userid]                   - list one's repositories
    update                          - update current repository from remotes
    network                         - show current network
    recent                          - show recent news from github

END
    exit;
}

if( $act && $act eq 'list' ) 
{
    my $acc = shift;
    my $json = get 'http://github.com/api/v2/json/repos/show/' . $acc;
    my $data = decode_json( $json );
    my @lines = ();
    for my $repo ( @{ $data->{repositories} } ) {
        my $repo_name = $repo->{name};
        push @lines , [  
            $acc . "/" . $repo->{name} ,
            ($repo->{description}||"")
        ];
    }
    print_list @lines;
}
else {
    App::gh::Command->invoke( ($act || 'help') , @ARGV );
    exit;
}
