#!/usr/bin/kitasperlas

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
 ($name, $value) = split(/=/, $pair);
 $value =~ tr/+/ /;
 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
 $value =~ s/~!/ ~!/g;
 $FORM{$name} = $value;
}

$taip = 0;
$data = $FORM{'num'};
$kur = "/var/www/docs/vija/druskonis/archyvai";

opendir(ARCHDIR, $kur) || die ("Unable to open directory");
while ($dir = readdir(ARCHDIR)) {
  $taip = $dir =~ /$data/; 
  last if ($taip > 0);
}
closedir(ARCHDIR);

if ($taip > 0) {
print "Location: http://www.druskonis.lt/archyvai/$dir/\n\n";
exit;
}

print "Content-type:text/html\n\n";
print "Numeris neegzistuoja."
