#!/usr/bin/perl

#catches output of selenium script ran with TimePerf2

use strict;
use Getopt::Long;

my $message ='Script Ran, but an error occurred.\n';
my $rc = 2;
my $performance_msg = '';
my $script = '';
my $items_ran = '';
GetOptions(
    's|script=s' => \$script,    # REQUIRED
);

if ( ! $script ) {
    print "No script, use --script\n";
    exit(2);
}

my $output = `perl $script 2>&1`;
if ( $output =~ m/(ERROR.+\n)/ ) {
    $message = $1;
    $rc = 2; #end it with an error
} elsif ( $output =~ m/OK/ )  {
    my @lines = split(/\n/, $output);
    foreach my $line (@lines) {
        if ( $line =~ m/OK:/ ) {
            ($message, $rc, $performance_msg, $items_ran) = split(/\|/, $line);
            $message = "$message | $performance_msg\n";
        }
    }
} else {
    $message = "UNKNOWN: $output";
    $rc = 3;
}

print "$message";
exit $rc;

