#!/usr/bin/env perl

use strict; use warnings;

our $VERSION='0.3';

use Getopt::Long 'GetOptionsFromArray';
use Mojo::Util qw/dumper spurt/;
use Mojolicious::Command '';
use Mojo::JSON qw/decode_json/;
use Test::AnsibleModule;
use App::FatPacker;
use Cwd qw(cwd);
use File::Spec::Functions qw(catdir);

sub _args {
  my $args
    = Getopt::Long::Configure(qw/no_auto_abbrev no_ignore_case pass_through auto_version auto_help/);
  GetOptionsFromArray shift,
    'h|help'          => \$ENV{ANSIBLE_HELP},   
    'a|args=s'        => \$ENV{ANSIBLE_ARGS},
    'p|pack'          => \$ENV{ANSIBLE_PACK},
    'm|module_path=s' => \$ENV{ANSIBLE_MODULE},
    'c|check'         => \$ENV{ANSIBLE_CHECK_MODE};
  Getopt::Long::Configure($args);
}

BEGIN { _args([@ARGV]) };

exit print Mojolicious::Command->extract_usage() if $ENV{ANSIBLE_HELP} || ! $ENV{ANSIBLE_MODULE};

if($ENV{ANSIBLE_PACK}) {
  my $fp=App::FatPacker->new;
  my @modules = split /\r?\n/, $fp->trace(args => [$ENV{ANSIBLE_MODULE}]);
  push @modules,'AnsibleModule.pm';
  my @packlists = $fp->packlists_containing(\@modules);
  my $base = catdir(cwd, 'fatlib');
  $fp->packlists_to_tree($base, \@packlists);

  my $code=$fp->fatpack_file($ENV{ANSIBLE_MODULE});
  $ENV{ANSIBLE_MODULE}=$ENV{ANSIBLE_MODULE}.'.packed';
  spurt $code, $ENV{ANSIBLE_MODULE};
  chmod 0755, $ENV{ANSIBLE_MODULE};
  print "Wrote $ENV{ANSIBLE_MODULE}\n";
  exit 0;
}

$ENV{ANSIBLE_ARGS} ||="{}";


my $t=Test::AnsibleModule->new;
my $res=$t->exec_module($ENV{ANSIBLE_MODULE},decode_json($ENV{ANSIBLE_ARGS}));

print "Output from $ENV{ANSIBLE_MODULE}: ". dumper($t->last_response);
print "Response code: $res\n";

=encoding utf8

=head1 NAME

test_ansible_module - Test ansible modules on the command line

=head1 SYNOPSIS

  Usage: test_ansible_module -m module_path  -a arguments

    test_ansible_module -m echo -a hello=world

  Options:
    -h,                         Show this help text
    -m, --module_path <path>    Full path to module to run
    -a --args <args>            Arguments to pass to module
    -c, --check                 Run module in check mode
    -p, --pack                  Run fatpacker on module
    --version                   Show version information

=head1 SEE ALSO

L<AnsibleModule>

=cut