#!/usr/local/bin/perl
#PERL-CGI to parse sentences inputted from HTML forms "DIPETT on the Web" using
#dipw
#Last modified: October 30, 1997.
#Author: Kong Wei Yew

$dip = "/local/apache/cgi-bin/dipett/dipw1";
#$empty = "/home/kaml1/usr5/tanka/share/public_html/cgi-bin/empty.txt";
$refer = $ENV{'HTTP_REFERER'};
$refer =~ s:[^/]*$::;
$refer = "http://www.site.uottawa.ca/" unless $refer;

### to change all "%__" strings into legible characters and putting them into
### associative arrays
foreach (split('&', $ENV{'QUERY_STRING'})){
	s/\+/ /g;
	($name, $value) = split('=', $_, 2);
	$name =~ s/%(..)/sprintf("%c",hex($1))/ge;
	$value =~ s/%(..)/sprintf("%c",hex($1))/ge;
	$in{$name} .= "," if defined($in{$name});
	$in{$name} .= $value;
}

### check to go to error screen when no sentence input
if ($in{'s'} eq ""){
	$load = 'error0fr.html';
}

### check to go to error screen when bad time allowance input
elsif ($in{'t'} =~ /\D/){
	$load = 'error1fr.html';
}

else {
	$inputsentence = $in{'s'};
	### to get rid of " and to replace ` with '
	$in{'s'} =~ s/\"//ge;
	$in{'s'} =~ s/\`/\'/ge;

	### to prepare sentence for dip executable
	$in{'s'} =~ s/(\W)/\\$1/g;

	### to add a period if missing
	if ($in{'s'} =~ /\w$/){
		$in{'s'} .= ".";
	}

	### to add " to beginning and end of sentence(s)
	$tempstring = "\"" . $in{'s'} . "\"";
	$in{'s'} = $tempstring;

	### time allowance defaults and maxima
	if ($in{'t'} eq ""){
		$in{'t'} = 30;
	}
	if ($in{'t'} > 180){
		$in{'t'} = 180;
	}
	if ($in{'t'} eq "0"){
		$in{'t'} = 30;
	}
	$options = "-t$in{'t'}";
	if ($in{'p'} eq "y"){
		$options .= " -pr";
	}

#	open(INFO, $empty); ###open for comparison
#	@empty = <INFO>;
#	close(INFO);

	###parse sentence using dip, then collect dip output
	open(DIP, qq[echo $options "$in{'s'}" | $dip |]);
	while (<DIP>){@dip = <DIP>;}
	close(DIP);
	if (@dip eq ""){
		$load = 'error2fr.html'; ###when dip has no output
	} ###error2fr.html may be obsolete and never used, but just in case
	else {
		$load = 'done'; ###when everything is ok
	}
}

if ($load ne 'done'){
###to show error screen(s)
print <<EOF;
Content-type: text/html\n\r\n\r
<html>
<meta http-equiv="refresh" content="0; url=$refer$load">
<head><title>Parsing...</title></head>
</html>
EOF
}

else{
### to print final legible output for user to see
print <<EOF;
Content-type: text/html\n\r\n\r
<html>
<head><title>Parsing results</title></head>
<body background="${refer}DIPETT.gif">

<h2 align="center">Parsing results</h2>

<p>
Input sentence(s): $inputsentence
<br>
Time limit: $in{'t'} second(s)
</p>
<!--<p>Escaped Input Sentence(s):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@sentence</p>-->

<p>
Parse time is in milliseconds. You will not see a parse tree for a
sentence if DIPETT cannot parse it. If that's the case, please return to
<a href="${refer}parserfr.html">the parser screen</a>, modify the structure
of the sentence, and try again.
</p>
<pre>@dip</pre><br>
<p align="center">Thank you for using DIPETT on the Web (:-)!</p>
<p align="center"><a href="${refer}parserfr.html">[Back to the parser screen]</a></p>

</body>
</html>
EOF
}

###end
