#!/usr/bin/perl

use strict;
use Getopt::Long;
use LWP::UserAgent;

my $version = "1.0.0";
my $port = 5988;
my $host = "localhost";
my $protocol = "http";

# Usage description
sub usage {
   print "Usage: wbemcat [OPTION]... [FILE]\n";
   print "Send FILE containing CIM-XML data to CIMOM and display returned data.\n";
   print "If no input FILE specified then read the data from stdin.\n";
   print "\nOptions:\n";
   print "  -t, --protocol=PROTOCOL\tProtocol with which to connect. Default=$protocol\n";
   print "  -h, --host=HOSTNAME\tName of host running the CIMOM. Default=$host\n";
   print "  -p, --port=PORT\tPort that the CIMOM is listening on. Default=$port\n";
   print "  -?, --help\t\tDisplay this help and exit\n";
   exit;
}

# Process command line options, if any
GetOptions("host|h=s" => \$host,
           "port|p=i" => \$port,
           "protocol|t=s" => \$protocol,
           "help|?" => sub{usage}) || usage;
my $file = @ARGV[0];

# Read all the XML data from the input file
my @xml;
if ($file) {
  open(XMLFILE,"<$file") || die "Cannot open $file: $!";
  @xml = (<XMLFILE>);
  close(XMLFILE) || warn "Cannot close $file: $!";
} else {
  # If no input file specified then read XML data from stdin
  @xml = (<STDIN>);
}

my $ua = LWP::UserAgent->new;
$ua->agent("wbemcat $version");

my $req = HTTP::Request->new(POST => "$protocol://$host:$port/cimom");
$req->content_type("application/xml");
$req->header("CIMProtocolVersion" => "1.0",
             "CIMOperation" => "MethodCall");
$req->content(join("", @xml));

my $res = $ua->request($req);
if($res->is_success) {
  print $res->content, "\n";
} else {
  print $res->status_line, "\n";
}

