#!/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);
my $usage = << 'EOT';
there are no structures created yet
$ micro new ufo
$ micro
$ micro ufo
$ micro ufo 5
$ micro any 10
$ micro drop ufo
EOT
grep{/\b$_\b/} @ARGV ? eval "\$$_ = 1; cleanArgs($_);" : "" for(qw(new debug write drop));
my @themes = AI::MicroStructure->new()->themes;
if(!$#themes || $#themes<=1){
print $usage;
exit;
}
sub cleanArgs{
my ($key) = @_;
my @tmp=();
foreach(@ARGV){
push @tmp,$_ unless($_=~/$key/);}
@ARGV=@tmp;
}
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/){
print join("\n",@themes);
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];
}