#!/usr/bin/kitasperlas

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

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;
}

$ftp = '/usr/bin/ftp';
$destdir = "/var/www/docs/vija/druskonis/zmones/evav/download/";
$source = $FORM{'URL'};
@parts = split(m!/!, $source);

if ($parts[0] eq "http:"){
  $server_name = $parts[2];
  $start_from = 3;
} else {
  $server_name = $parts[0];
  $start_from = 1;
}

$src_path = "";
while ($start_from < @parts){
  $src_path = $src_path . "/" . $parts[$start_from];
  $start_from = $start_from + 1;
}

$dest_path = $destdir . $parts[@parts-1];

$user_name = "anonymous";
$user_pass = "evri\@is.lt";

open (FTP, "| $ftp");
print FTP "open $server_name\n";
print FTP "$user_name\n";
print FTP "$user_pass\n";
print FTP "get $src_path\n";
print FTP "close\n";
print FTP "quit\n";
close (FTP);