#!/usr/bin/env perl
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;

$|++;
binmode STDOUT, ':utf8';


# my $config = parse_config $ENV{HOME} . "/.gitconfig";
# use Data::Dumper; warn Dumper( $config );

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

if( ! $act 
    || $act eq 'help' 
    || $act eq 'search'
    || $act eq 'clone'
    || $act eq 'all'
    || $act eq 'cloneall'
    || $act eq 'pull'
    || $act eq 'fork'
    || $act eq 'network'
) 
{
    App::gh::Command->invoke( ($act || 'help') , @ARGV );
    exit;
}
elsif( $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;
}
elsif( $act eq 'network' ) {
    die "Git config not found." if ( ! -e ".git/config" );
    App::gh::Command->dispatch('network', @_ );
}
