#!/usr/bin/perl

my $PROGNAME = substr($0,0,rindex($0,'.'));

# Constants
my $mHost = 'ucs4.us';
my $mPort = '80';

use LWP;
#use strict;
use XML::Simple;
use XML::Writer;
use XML::LibXML;
use XML::Parser;
use Data::Dumper;
use IO::Socket;

# Subroutines
sub doPostXml;
sub doDebugXml;

# Global vars
our $mUri = "http://" . $mHost . "/ucs/engine/ucs-engine.cgi";
our $mBrowser = LWP::UserAgent->new;

##########################################################################################

my $lXmlReq =	'<?xml version="1.0" encoding="UTF-8"?>' .
		'<!-- ucs-layout.xml -->' .
		'<layout mode="AUTO" src="API">' .
		'   <fabint model="FI6296" qty="2">' .
		'     <expmod model="E16UP" qty="0" />' .
		'     <power qty="2" />' .
		'   </fabint>' .
		'   <chassis model="UCS5108" cfg="1" qty="4">' .
		'     <blade model="B200" qty="4" />' .
		'     <blade model="B420" qty="2" />' .
		'     <iomod model="IOM2204" qty="2" />' .
		'     <power qty="4" />' .
		'   </chassis>' .
		'   <chassis model="UCS5108" cfg="2" qty="2">' .
		'     <blade model="B230" qty="6" />' .
		'     <blade model="B440" qty="1" />' .
		'     <iomod model="IOM2204" qty="2" />' .
		'     <power qty="3" />' .
		'   </chassis>' .
		'   <rackmount>' .
		'     <server model="C220" qty="16" />' .
		'     <server model="C240" qty="16" />' .
		'     <server model="C260" qty="8" />' .
		'     <server model="C460" qty="8" />' .
		'   </rackmount>' .
		'   <params extra="0" face="FRONT" panels="YES" size="MEDIUM" />' .
		'</layout>';

my ($lXmlRsp, $lLocation, $lMessage, $lSuccess) = doPostXml($mUri, $lXmlReq);

print 'UCS Rack Layout: ' . $lLocation . "\n";

#doDebugXml ("LAYOUT", $lXmlReq, $lXmlRsp, $lMessage, $lSuccess);

if ($lSuccess)
{
    eval {
	# lXmlRsp should now contain a JPEG image of the racklayout

	open FILE, ">xmlrsp.xml" or die $!;
	print FILE $lXmlRsp;
	close FILE;
    };

    $lSuccess = undef if ($@);
}


##########################################################################################

#
# Send the XML request to the Web Server
#
sub doPostXml()
{
    my ($mUri, $postData) = @_;
    if (!$mBrowser) {
        $mBrowser = LWP::UserAgent->new();
    }
    my $request = HTTP::Request->new(POST => $mUri);
#   $request->content_type("application/x-www-form-urlencoded");
    $request->content_type("text/xml");
    $request->content($postData);
    # print("\nRequest: \n" . $request->as_string() . "\n");
    my $resp = $mBrowser->request($request);    # HTTP::Response object
    # print("\nResponse: \n" . $resp->content . "\n");
    # print("\nLocation: \n" . $resp->header("Location") . "\n");
    return ($resp->content, $resp->header("location"), $resp->status_line, $resp->is_success, $resp) if wantarray;
    return unless $resp->is_success;
    return $resp->content;
}

##########################################################################################

#
# print debug
#
sub doDebugXml()
{
    my ($mTopic, $lReq, $lRsp, $lMsg, $lSuc) = @_;

#    print "\n\n-- " . $mTopic . " --\n\n";
    print "\n";
    print "request:\n" . $lReq . "\n\n";
    print "content:\n" . $lRsp . "\n\n";
    print "message:\n" . $lMsg . "\n\n";
    print "success:\n" . $lSuc . "\n\n";

    return;
}

##########################################################################################
