#!/usr/bin/perl

use strict;

use Mysql;
use XML::DOM;
use Compress::Zlib;

print "Content-type: text/plain\n\n";

#----------------------------------------
# Parse incoming data
#----------------------------------------
my $buffer;
my %params;
my $cmd = "";

if($ENV{REQUEST_METHOD} eq 'POST')
{
    read(STDIN, $buffer, $ENV{CONTENT_LENGTH});
    $buffer = Compress::Zlib::memGunzip($buffer) if ($ENV{HTTP_X_SATIS_ENCODING} || '') =~ /gzip/; 
    $buffer .= "&";
    $buffer .= $ENV{QUERY_STRING};
} 
else
{
    $buffer = $ENV{QUERY_STRING};
}
my @pairs = split(/&/, $buffer);
my $pair;

foreach $pair(@pairs)
{
    (my $key, my $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%0D%0A/"%0A"/eg;
    $value =~ s/%([A-Fa-f0-9]{2})/pack("C",hex($1))/eg;
    
    if (defined $params{$key})
    {
        $params{$key} .= "\t$value";
    } 
    else
    {
        $params{$key} = $value;
    }
}

#--- Gereral Parameters Definition
my $opt_user="satis";
my $opt_password="satis";
my $opt_base="satis";
my $socket="/tmp/mysql.sock"; 
# Определяем используемый сокет
$ENV{"3306"}="$socket";

my $conf="/usr/local/controller/scripts/scripts.conf";
open(CONF,$conf) or die "Can't open config file!";
my $confparam="";
my $confvalue;

while(<CONF>){
    chomp();
#    print $_ . "\n";
    ($confparam,$confvalue)=split(/=/);
    if(defined($confvalue)){
        if($confparam eq "dbuser") {
	    $opt_user=$confvalue;
	}
        elsif($confparam eq "dbpass") {
	    $opt_password=$confvalue;
	}
        elsif($confparam eq "db") {
	    $opt_base=$confvalue;
	}
	else {}
    }
    else {
#	print "WARNING: split didn't find '=' in line.\n";
    }
}
close(CONF);

#--- Mysql connection 
my $dbh = Mysql->connect("", "$opt_base", "$opt_user", "$opt_password") ||
        die "Can't connect\n";
#--- End of section



my $strtoparse = $params{"xml"};
open(FILE, "> ARMInput.xml");
print FILE $strtoparse;
close(FILE);
my $parser = new XML::DOM::Parser;
my $indoc = $parser->parse($strtoparse);

my $outdoc = new XML::DOM::Document;
my $data = $outdoc->createElement("xml");
$outdoc->appendChild($data);

my $objects = $indoc->getElementsByTagName("object");
my $i;

for ( $i = 0; $i < $objects->getLength(); $i++)
{
  my $object = $objects->item($i);
  my $id = $object->getAttribute("id");

  my $query;
  $query = "SELECT Name, Value FROM SystemConf WHERE NodeID=" . $id;
  my $sth = Query $dbh $query or die "$Mysql::db_errstr";

  $object = $outdoc->createElement("object");
  $data->appendChild($object);
$object->setAttribute("id", $id);
  my $line;
  if ( $line = $sth->fetchrow_arrayref )
  {
    my $param = $outdoc->createElement("param");
    $object->appendChild($param);

    my $name = $$line[0] if $$line[0] ne "";
    $param->setAttribute("name", $name);

    my $value = $$line[1] if $$line[1] ne "";
    my $cdata=$outdoc->createCDATASection("cdata");
    $cdata->setData("$value");
    $param->appendChild($cdata);
  }

  undef $sth;
}

$outdoc->printToFile("ARMOutput.xml");
my $final = $outdoc->toString();
#$final =~  s/([^\w()'*~!.-])/sprintf '%%%02X', ord $1/eg;

print $final;

$outdoc->dispose;
$indoc->dispose;

undef $dbh;
