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

print "content-type: text/plain\n";
print "content-length: 4\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;
    }
}

##############
# Parameters #
##############
my $SAID = $params{"said"} || print "No said\n";
my $BID = $params{"bid"} || print "No bid\n";
my $N = $params{"n"} || print "No n\n";

print "[OK]\n";

my $pid = fork();
if($pid == 0)
{
    system("/usr/local/controller/spectrum/WRmonitoring/scanBand.pl $SAID $BID $N");
}