#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;

eval "use App::Mypp; 1" or die "Could not load App::Mypp: $@";

my $app = App::Mypp->new;
my $action = shift @ARGV or exit $app->help;
my $method = $action;

$action =~ s/^-+//;
$method =~ s/^-+//;
$method =~ s/-/_/g;

if($action eq 'update') {
    $app->clean;
    $app->t_load;
    $app->t_pod;
    $app->generate_readme;
}
elsif($action eq 'test') {
    $app->clean;
    $app->t_load;
    $app->t_pod;
    $app->make('test');
}
elsif($action eq 'build') {
    $app->clean;
    $app->t_load;
    $app->t_pod;
    $app->timestamp_to_changes;
    $app->update_version_info;
    $app->generate_readme;
    $app->manifest;
    $app->tag_and_commit;
    eval { $app->_system('rm ' .$app->name .'* 2>/dev/null') }; # don't care if this fail
    $app->make('dist');
}
elsif($action eq 'share') {
    $app->share_via_extension;
    $app->share_via_git;
}
elsif($app->can($method)) {
    if(my $res = $app->$method(@ARGV)) {
        if(ref $res) {
            local $Data::Dumper::Indent = 1;
            local $Data::Dumper::Sortkeys = 1;
            local $Data::Dumper::Terse = 1;
            print Dumper $res;
        }
        elsif($res eq '1') {
            exit 0;
        }
        else {
            print $res, "\n";
        }
    }
    else {
        die "Failed to execute $app->$method\n";
    }
}
elsif($action eq 'version') {
    print "App-Mypp version: ", $App::Mypp::VERSION, "\n";
}
elsif($action eq 'man') {
    if($0 eq '-') {
        print "Read manual online: http://jhthorsen.github.com/app-mypp\n"
    }
    else {
        exec perldoc => $0;
    }
}
else {
    exit $app->help;
}

exit 0;
