The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl -X
use strict;
use warnings;
use AI::MicroStructure;
use Getopt::Long;
use Data::Dumper;
use JSON::XS;
our ($new,$debug, $write,$drop) =(0,0,0,0); 
if( grep{/\bnew\b/} @ARGV ){ $new = 1; cleanArgs("new"); }
if( grep{/\bdebug\b/} @ARGV ){$debug = 1; cleanArgs("debug"); }; 
if( grep{/\bwrite\b/} @ARGV ){ $write = 1; cleanArgs("write");  }; 
if( grep{/\bdrop\b/} @ARGV ){ $write = 1; cleanArgs("drop");  }; 



sub cleanArgs{
    my ($key) = @_; 
    my @tmp=();
    foreach(@ARGV){ 
    push @tmp,$_ unless($_=~/$key/);}
    
    @ARGV=@tmp;
}

my $usage = << 'EOT';
EOT

my %conf = ( );
GetOptions( \%conf, "whitespace|ws!", "version","themes", "help", "remote","new",
                    "check", "category=s", "sources" ,"drop");

my $theme;


if(defined($ARGV[0]) && $ARGV[0] =~ m/themes/){
  
    my $json = encode_json([grep{!/any/}AI::MicroStructure->new()->themes()]);
    print $json;
    exit;


}

if($conf{"new"} and $ARGV[0] !~/themes/  and $ARGV[0] =~ m{^([^/]+)/(.*)}s) {
    $theme          = $1;
    $conf{category} = $2;

    my $meta = AI::MicroStructure->new( $theme, category => "new" );
    exit;
}



if(!$conf{"drop"} && !$conf{"write"}and $ARGV[0] !~/themes/){



# find out the theme name
$theme = shift || $AI::MicroStructure::Theme;

if (!length $conf{category} && $theme =~ m{^([^/]+)/(.*)}s) {
    $theme          = $1;
    $conf{category} = $2;
}


AI::MicroStructure->new( $theme, category => "new" )
 unless AI::MicroStructure->has_theme( $theme );

my $module = "AI::MicroStructure::$theme";


# load the remote theme if needed
if ( $conf{remote} || $conf{check} || $conf{sources}) {
    eval "require $module;";
    die "Theme '$theme' is not updatable!\n"
        unless $module->has_remotelist();
}

# informative options
print STDERR
"meta, a simple front-end to AI::MicroStructure version $AI::MicroStructure::VERSION\n"
  if $conf{version};
print STDERR $usage if $conf{help};
print map "$_\n", AI::MicroStructure->themes if $conf{themes};
if ( $conf{sources} ) {
    my @sources = $module->sources( $conf{category} );
    print map "$_\n", @sources;
}
exit if $conf{themes} || $conf{version} || $conf{help} || $conf{sources};

# real processing starts here
$\ = $/;
my $sep = $conf{whitespace} ? ' ' : $\;

my $meta = AI::MicroStructure->new( $theme, category => $conf{category} );

my (@remote, @local);
@remote = $module->remote_list( $conf{category} )
    if $conf{remote} || $conf{check};
if ( !$conf{remote} ) {
    my $count = shift;
    $count = 1 unless defined $count;
    $count = 0 if $conf{check};
    @local = $meta->name($count);
}
if ( $conf{check} ) {
    my %seen;
    $seen{$_}++ for @remote;
    $seen{$_}-- for @local;
    foreach my $key ( sort keys %seen ) {
        next unless $seen{$key};
        print $seen{$key} > 0 ? "+ $key" : "- $key";
    }
}
else {
    print join $sep, @local, @remote;
}

}

END{
#    print Dumper  [$theme,@ARGV,%conf];
}