#!/usr/local/bin/perl
#Script frm the book for couting lines and words, without using CGI.pm. Must be called from HTML page.
#File: countwords.pl
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;

#foreach (keys %query)
#{
#    print "$_=$query{$_}\n";
#}

while (<>)
{

    chop;
    chop;
    ($line) = ($_ =~ /-*(\d*)-*/);
    while ($line eq $boundary)
    {
	$line = <>;
	chop $line;
	chop $line;
	($name)=($line =~ /^Content-Disposition: form-data; name=\"(.*?)\"(.*)/);
	if ($name eq "file")
	{
	    while (<>)
	    {
		$line = $_;
		chop $line;
		chop $line;
		($line) = ($line =~ /-*(\d*)-*/);
		if ($line ne $boundary)
		{
		    $line_count++;
		    $word_count += @words=split(/\s+/);
		    $char_count += length($_);
		}
		else
		{
		    last;
		}
	    }	
	} elsif ($name eq "count") {
	    while (<>)
	    {
		$line = $_;
		chop $line;
		chop $line;
		($line) = ($line =~ /-*(\d*)-*/);
		if ($line ne $boundary)
		{
		    chop;
		    chop;
		    $count{$_}++ if ($_);			
		}
		else
		{
		    last;
		}
	    }	
	}
    }
}

print "lines: $line_count\n" if $count{'line'};
print "words: $word_count\n" if $count{'word'};
print "chars: $char_count\n" if $count{'char'};
print "no totals specified\n" unless (scalar %count);











