#!/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' ) 
{
    App::gh::Command->dispatch( 'help' , @ARGV );
    exit;
}
elsif( $act eq 'search' || $act eq 's' ) {
    App::gh::Command->dispatch( 'search' , @ARGV );
    exit;
}
elsif( $act eq 'clone' || $act eq 'c' ) {
    App::gh::Command->dispatch( 'clone' , @ARGV );
    exit;
}
elsif( $act eq 'pull' )
{
    App::gh::Command->dispatch('pull', @ARGV );
}
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 'cloneall' || $act eq 'ca' ) {
    my $acc = shift;
    my $attr = shift || 'ro';

    _info "Getting repository list from github: $acc";
    my $json = get 'http://github.com/api/v2/json/repos/show/' . $acc;
    my $data = decode_json( $json );

    _info "Will clone repositories below:";
    for my $repo ( @{ $data->{repositories} } ) {
        print "  " . $repo->{name} . "\n";
    }


    for my $repo ( @{ $data->{repositories} } ) {
        my $repo_name = $repo->{name};
        my $local_repo_name = $repo_name;
        $local_repo_name =~ s/\.git$//;

        my $uri;
        if( $attr eq 'ro' ) {
            $uri = sprintf "git://github.com/%s/%s.git" , $acc , $repo_name;
        }
        elsif( $attr eq 'ssh' ) {
            $uri = sprintf "git\@github.com:%s/%s.git" , $acc , $repo_name;
        }
        print $uri . "\n";

        if( -e $local_repo_name ) {
            print "Updating " . $local_repo_name . " ...\n";
            qx{ cd $local_repo_name ; git pull origin master };
        }
        else {
            print "Cloning " . $repo->{name} . " ...\n";
            qx{ git clone -q $uri };
        }
    }
}
elsif( $act eq 'network' ) {
    die "Git config not found." if ( ! -e ".git/config" );
    App::gh::Command->dispatch('network', @_ );
}
elsif( $act eq 'fork' ) {
    App::gh::Command->dispatch('fork', @_ );
}
