#!/usr/bin/env perl
package # prevent cluttering main
    App::Yath;
use strict;
use warnings;

BEGIN {
    no warnings 'once';

    require File::Spec;
    my $script = File::Spec->rel2abs(__FILE__);

    $App::Yath::SCRIPT = $script;
    $ENV{YATH_SCRIPT} = $script;
}

our $VERSION = '0.001007';

use App::Yath::Util qw/load_command find_pfile/;
use List::Util qw/sum/;
use Time::HiRes qw/time/;

use Test2::Util qw/pkg_to_file/;

use Test2::Util::Times qw/render_bench/;

BEGIN {
    if (@ARGV && $ARGV[0] =~ m/^-*h(elp)?$/i) {
        shift @ARGV;
        *cmd_name = sub() { 'help' };
    }
    elsif (@ARGV && -f $ARGV[0] && $ARGV[0] =~ m/\.jsonl(\.bz2|\.gz)$/) {
        print "\n** First argument is a log file, defaulting to 'replay' command **\n\n";
        *cmd_name = sub() { 'replay' };
    }
    elsif (!@ARGV || -d $ARGV[0] || -f $ARGV[0] || substr($ARGV[0], 0, 1) eq '-') {
        if (find_pfile) {
            print "\n** Persistent runner detected, defaulting to 'run' command **\n\n";
            *cmd_name = sub() { 'run' };
        }
        else {
            print "\n** Defaulting to 'test' command **\n\n";
            *cmd_name = sub() { 'test' };
        }
    }
    else {
        my $cmd_name = shift @ARGV;
        *cmd_name = sub() { $cmd_name };
    }

    return unless cmd_name() eq 'spawn';

    ########################################
    # Spawn command follows  \/
    ########################################

    my ($class, $dir, %args) = @ARGV;

    if ($args{setsid}) {
        require POSIX;
        POSIX::setsid();
    }

    my $pid = $$;

    END {
        local ($?, $!, $@);
        if ($args{pfile} && -f $args{pfile} && $pid == $$) {
            print "Deleting $args{pfile}\n";
            unlink($args{pfile}) or warn "Could not delete $args{pfile}: $!\n";
        }
    }

    my $file = pkg_to_file($class);
    require $file;
    my $spawn = $class->new(dir => $dir);

    my $test = $spawn->start;

    unless ($test) {
        my $complete = File::Spec->catfile($dir, 'complete');
        open(my $fh, '>', $complete) or die "Could not open '$complete'";
        print $fh '1';
        close($fh);
        exit 0;
    }

    require App::Yath::Filter;
    App::Yath::Filter->import($test);
}

my $cmd_class = load_command(cmd_name());

my $cmd = $cmd_class->new(args => \@ARGV);

my $start = time;
my $exit  = $cmd->run;

if ($cmd->show_bench) {
    my $end = time;
    my $bench = render_bench($start, $end, times);
    print $bench, "\n\n";
}

exit $exit;

__END__

=pod

=encoding UTF-8

=head1 NAME

yath - Primary Command Line Interface (CLI) for Test2::Harness

=head1 DESCRIPTION

=head1 SOURCE

The source code repository for Test2-Harness can be found at
F<http://github.com/Test-More/Test2-Harness/>.

=head1 MAINTAINERS

=over 4

=item Chad Granum E<lt>exodist@cpan.orgE<gt>

=back

=head1 AUTHORS

=over 4

=item Chad Granum E<lt>exodist@cpan.orgE<gt>

=back

=head1 COPYRIGHT

Copyright 2017 Chad Granum E<lt>exodist7@gmail.comE<gt>.

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

See F<http://dev.perl.org/licenses/>

=cut
