#!/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;
    }
}

my $id = $params{"id"};

#--- 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


if ( $id > 0 )
{
  my $query;
  $query = "SELECT data FROM storedData WHERE id=$id";
  my $sth = Query $dbh $query or die "$Mysql::db_errstr";

  my $line;
  if ( $line = $sth->fetchrow_arrayref )
  {
    #probaly will not work under FreeBSD: NEED TO CHECK
    utf8::decode($$line[0]) unless utf8::is_utf8($$line[0]);
    print $$line[0] if $$line[0] ne "";
  }
}
else
{
  my $outdoc = new XML::DOM::Document;
  my $data = $outdoc->createElement("containers");
  $outdoc->appendChild($data);

  my $query;
  $query = "SELECT id, name FROM storedData WHERE type='MnemoSVG' ORDER BY name";
  my $sth = Query $dbh $query or die "$Mysql::db_errstr";

  my $line;
  while ( $line = $sth->fetchrow_arrayref )
  {
    my $mnemoid   = $$line[0] if $$line[0] ne "";
    my $mnemoname = $$line[1] if $$line[1] ne "";

    my $mnemotag = $outdoc->createElement("container");
    $data->appendChild($mnemotag);
    $mnemotag->setAttribute("id", $mnemoid);
    $mnemotag->setAttribute("name", $mnemoname);
#    if ( $mnemoname ne "" )
#    {
#      my $cdata=$outdoc->createCDATASection("cdata");
#      $cdata->setData("$mnemoname");
#      $mnemotag->appendChild($cdata);
#    }
  }

  undef $sth;

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

  print $final;

  $outdoc->dispose;
}

undef $dbh;
