#!/usr/local/bin/perl -w

use strict;

use App::Options (
    options => [qw(client_class responder ping minimal verbose)],
    option => {
        client_class => {
            description => "Class which implements the client protocol",
            default => "Business::Travel::OTA::Client::HTTP",
        },
        responder => {
            description => "URL to POST the Request to and get a Response from",
            default => "http://localhost/cgi-bin/otaserver",
        },
        ota_message_dir => {
            description => "Directory for OTA messages to send",
            default => '${prefix}/share/ota/message',
        },
        send => {
            description => "Send an XML request message message before any other messages",
        },
        ping => {
            description => "Send an OTA_Ping message before any other messages",
        },
        minimal => {
            description => "Use minimal number of HTTP Headers",
            default => 0,
        },
        verbose => {
            description => "Print more detail so we can see what's going on (range=0-9)",
            default => 0,
        },
    },
);

my $module = $App::options{client_class} . ".pm";
$module =~ s!::!/!g;
require $module;

use Business::Travel::OTA::Utils qw(read_file);

=head1 NAME

otaclient - Submits OTA messages to an OTA-compliant server

=head1 SYNOPSIS

  otaclient --ping               # sends an OTA_PingRQ message
  otaclient --ping --verbose     
  otaclient --send=OTA_PingRQ
  otaclient OTA_PingRQ
  otaclient OTA_PingRQ.xml
  otaclient /usr/mycompany/prod/share/ota/message/OTA_PingRQ.xml

=head1 DESCRIPTION

Submits OTA messages to an OTA-compliant server and prints out the response messages.

This is useful for exercising (and testing) an OTA-compliant server that you have developed.

=cut

{
    my $client = $App::options{client_class}->new();
    my ($request_xml, $response_xml);
    if ($App::options{ping}) {
        my $request_xml = <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<OTA_PingRQ
    xmlns="http://www.opentravel.org/OTA/2002/08"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.opentravel.org/OTA/2002/08 /usr/rubicon/spadkins/src/OTA/Business-Travel-OTA/schemas/2002A/OTA_PingRQ.xsd"
    TimeStamp="2002-12-03T11:09:47-05:00"
    Target="Production"
    Version="2002A"
    SequenceNmbr="1" >
  <EchoData>Are you there?</EchoData>
</OTA_PingRQ>
EOF
        $request_xml = $client->send($request_xml);
        print $request_xml;
    }

    my $ota_message_dir = $App::options{ota_message_dir} || "$App::options{prefix}/share/ota/message";

    my @files = @ARGV;
    push(@files, split(/,/,$App::options{send})) if ($App::options{send});

    foreach my $request_file (@files) {
        if (! -f $request_file) {
            if (-f "$ota_message_dir/$request_file") {
                $request_file = "$ota_message_dir/$request_file";
            }
            elsif (-f "$ota_message_dir/$request_file.xml") {
                $request_file = "$ota_message_dir/$request_file.xml";
            }
        }
        $request_xml = &read_file($request_file);
        $response_xml = $client->send($request_xml);
        print $response_xml;
    }
}

=head1 ACKNOWLEDGEMENTS

 * Author:  Stephen Adkins <sadkins@therubicongroup.com>
 * Copyright: (c) 2005 Stephen Adkins (for the purpose of making it Free)
 * License: This is free software. It is licensed under the same terms as Perl itself.

=head1 SEE ALSO

=cut

