#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
use Net::HTTP;
use HTTP::Request::Common qw(POST);
use Mysql;
use POSIX ":sys_wait_h";

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

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

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

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 $lockID 	 = $params{"lockid"} || print "no lockid\n";

my $opt_user 	 = "satis";
my $opt_password = "satis";
my $database 	 = "satis";

my $dbh = Mysql->connect("", "$database", "$opt_user", "$opt_password") ||
          die "Can't connect\n";

my $lockName = "locking$lockID";
my $query = "DELETE FROM SystemConf WHERE Name='$lockName'";
Query $dbh $query or die "$Mysql::db_errstr";

undef $dbh;

printf "[$lockID]";