#!/usr/bin/perl -w
use strict;
use warnings;

# Author: Stefan Trcek
# Copyright(c) 2004 ABAS Software AG

use CGI qw(:standard -no_xhtml);
#use diagnostics
#$SIG{__DIE__} = \&Carp::confess;
#$SIG{__WARN__} = \&Carp::cluck;

use WWW::Webrobot;
use WWW::Webrobot::SendMail;
use WWW::Webrobot::CGIHelper;


my $USAGE = <<EOF;
USAGE: webrobot [options]
-h              this message
-v              version
All other options are written 'option_name=value_of_option'.
option           description
------------------------------------------------------------------------
cfg              test configuration [mandatory], must end in '.prop'
testplan         overwrite 'testplan' in cfg
<property>       set property
EOF

my ($p1, $other) = @ARGV;
$p1 ||= "";
do {print $USAGE; exit} if $p1 =~ /^-(-)?h(elp)?$/i;
do {print "Webrobot version: $WWW::Webrobot::VERSION\n"; exit} if $p1 =~ /^-(-)?v(ersion)?$/i;

my $cgi = CGI -> new();
my $cfg_name = $cgi->param('cfg') or die "No configuration file defined!";
my $test_plan_name = $cgi->param('testplan');

MAIN: {
    my $cmd_param = WWW::Webrobot::CGIHelper::param2list($cgi, [qw(cfg testplan)]);
    my $webrobot = WWW::Webrobot -> new(\$cfg_name, $cmd_param);
    my $exit = $webrobot -> run(\$test_plan_name);

    # flush output, because attachments for send_mail
    # may be these handles redirected output
    close STDOUT;
    close STDERR;

    # conditionally send mail
    WWW::Webrobot::SendMail::send_mail($webrobot->cfg()->{mail}, $exit);

    exit $exit;
}

1;

=head1 NAME

webrobot - run a testplan

=head1 SYNOPSIS

 webrobot cfg=example/cfg.prop testplan=example/testplan.xml

=head1 DESCRIPTION

This command runs a testplan.
It takes two parameters,
both are mandatory:

=over

=item cfg

This is the configuration file,
see L<WWW::Webrobot::pod::Config>.

=item testplan

This is the testplan to run,
see L<WWW::Webrobot::pod::Testplan>.

=back

B<Change example/testplan.xml to your needs.>
It is just an example and won't run successfully
because it is taken from an intranet environment!

=head1 SEE ALSO

L<webrobot-load>


=cut
