#!/usr/local/bin/perl
#Print the uplaoded file as ASCII text
#noexec

require '/home/scsfac/downeyt/scripts/parse-lib.pl';
#%query=&get_query;

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

$content_type=$ENV{'CONTENT_TYPE'};
$boundary=$content_type;
($boundary) = ($content_type =~ /^multipart\/form-data; boundary=-*(\d*)/);
$len= length $boundary;

while (<>)
{

    chop;
    chop;
    ($line) = ($_ =~ /-*(\d*)-*/);  #isolate the digits
    if ($line eq $boundary)
    {
	$line = <>;
	chop $line;
	chop $line;
	($name,$filename)=($line =~ /^Content-Disposition: form-data; name=\"(.*?)\"; filename=\".*\\(.*?)\"/);
	if ($name eq "file")
	{
	    $uniquename = $filename;
	    $count=1;
	    while (-e $uniquename)
	    {
		$uniquename = $filename . "." . $count++;
	    }
	    print "$uniquename\n";
	    open UNIQUE, ">$uniquename";
	    while (<>)
	    {
		$line = $_;
		chop $line;
		chop $line;
		if (/^Content-Type/)
		{
		    <>;
		    next;
		}
		($line) = ($line =~ /-*(\d*)-*/);
		if ($line ne $boundary)
		{
		    print $_;
		}
		else
		{
		    #found end of file, so get out
		    last;
		}
	    }
	}
    }
}












