#!/usr/bin/env perl
use Applify;

option bool => init => 'Alias for --update';
option str => update => 'Update repository files' => n_of => '0,';
option bool => test => 'Run unittests';
option bool => build => 'Build a distribution';
option bool => share => 'Push built distribution to CPAN and origin git repo';
option bool => clean => 'Remove generated files by make';
option bool => force => 'Force action, such as overwriting files';

documentation 'App::Mypp';
version 'App::Mypp';
extends 'App::Mypp';

app {
    my $self = shift;
    my $action = shift || '__UNDEF__';

    if($action and $self->can($action)) {
        $self->$action($action eq 'update' ? [keys %{ $self->_templates }] : 1);
    }

    if(@{ $self->update } or $self->init) {
        $self->update([keys %{ $self->_templates }]) unless(grep { /\w/ } @{ $self->update });
        $self->_generate_file_from_template($_) for reverse sort @{ $self->update };
        $self->_system(sprintf '%s %s > %s', 'perldoc -tT', $self->top_module, 'README');
    }
    elsif($self->test) {
        $self->_make('reset');
        $self->_generate_file_from_template($_) for grep { m!^t/! } keys %{ $self->_templates };
        $self->_make('test');
    }
    elsif($self->build) {
        $self->_build;
    }
    elsif($self->share) {
        my $branch = (qx/git branch/ =~ /\* (.*)$/m)[0];
        chomp $branch;
        $self->_share_via_extension;
        $self->_git(push => origin => $branch);
        $self->_git(push => '--tags' => 'origin');
    }
    elsif($self->clean) {
        $self->_make('reset');
    }
    else {
        $self->_script->print_help;
    }
 
    return 0;
};
