#!/usr/bin/perl -w
use strict;
use XML::DOM;
use DBI;
require "../scripts/log.pl";
require "../scripts/connect.pl";

my $dolog = 0;

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

#----------------------------------------
## Parse incoming data
##
my $buffer;
my %params;

if ($ENV{REQUEST_METHOD} eq 'POST') {
    read(STDIN, $buffer, $ENV{CONTENT_LENGTH});
    $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;
    }
}

my $up = 1;
my $strtopars = $params{"xml"};
if(defined $params{"type"}) {
    if($params{"type"} eq "up") {
	$up = 1;
    }
    else {$up = 0;}	
}

print "[OK]\n" if(!$up);

my $dbh = satisMysqlConnect();

my $ready;
my $value;
my $id;
my $parid;
my $name;

my $query;

my $parser = new XML::DOM::Parser;
my $doc = $parser->parse($strtopars);

my $rows = $doc->getElementsByTagName("row");
my $rows_number = $rows->getLength;

for ( my $j = 0; $j < $rows_number; $j++ ) {
    $value = $name = $id = "";
    my $row = $rows->item($j);
    my $columns = $row->getElementsByTagName("col");
    my $columns_number = $columns->getLength;
    for ( my $k = 0; $k < $columns_number; $k++ ) {
      my $column = $columns->item($k);
      my $xmlname = $column->getAttribute("name");
      my $xmlvalue = "0";
	  if(defined $column->getChildNodes->item(0)) {
		  $xmlvalue = $column->getChildNodes->item(0)->getNodeValue;
	  }
      if($xmlname eq "Name") {$name = $xmlvalue;}
      elsif($xmlname eq "Value") {$value = $xmlvalue;}
      elsif($xmlname eq "NodeID") {$id = $xmlvalue;}
      else {}
    }
	if( $dolog ) {
		dolog("update.log","$name, $value, $id",">");
	}
    if($id ne "" and  $name ne "") {
        $query = "UPDATE SystemConf SET Value=\'$value\' WHERE NodeID=$id AND Name=\'$name\';\n";
		$dbh->do($query);
		$ready .= $query;
    }
	if( $dolog ) {
		dolog("update.log","$query",">>");
	}
}

	if( $dolog ) {
		dolog("update.log","ID = $id",">>");
	}

if($id ne "") {
    $query = "SELECT ParentID FROM Nodes WHERE NodeID=$id;\n";
    if( $dolog ) {
        dolog("update.log","$query",">>");
    }
    my $sth = $dbh->prepare($query);
    $sth->execute();
    my $line;
    while ($line = $sth->fetchrow_arrayref) {
        $parid = $$line[0];
    }
    $sth->finish();

    $query = "SELECT SuperObject FROM Nodes WHERE NodeID=$parid;\n";
    if( $dolog ) {
        dolog("update.log","$query",">>");
    }
    my $sth = $dbh->prepare($query);
    $sth->execute();
    my $line;
    while ($line = $sth->fetchrow_arrayref) {
        $parid = $$line[0] > 0 ? $$line[0] : $parid;
    }
    $sth->finish();
}
$dbh->disconnect();

system("../scripts/polstop.sh $parid HUP > /dev/null &");
if( $dolog ) {
	dolog("update.log","Device ID: $parid",">>");
	dolog("update.log",$params{"xml"},">>");
}

$doc->dispose();
