
# Run the search and format the results.  First important think to 
# notice.  We only have THREE LINES actually talking to the server.
# The rest is simple perl to format results.  The second is that
# we have searching, sorting for some fields based on distinct,
# usually merged fields, and results based on the standard fields.

sub runquery {
   my $count = 0; my $outbuffer = ""; my $rtn;
   my $thisquery = $_[0];

   if ($sth = $dbh->prepare($thisquery)) {
       $sth->execute();
     while (@results = $sth->fetchrow ) { 
     $count++;
     $resstrippedhw = @results[0];
     $resheadword = @results[1];
     $respageobject = @results[2];
     $respagetag = @results[3];
     $resfilename = @results[4];
     $resarticletype = @results[5];
     $resdocyear = @results[6];
     $resdicoid = @results[7];
     $resstartbyte = @results[8];
     $resbyteoff = @results[9];
     if ($oldresdicoid ne $resdicoid) {
         if ($count > 1) {
             $mvotitlebuf .= ", \n";
         }
         $mvotitlebuf .= @DICOSHORTTITLE{$resdicoid};
         $mvotitlebuf .= "  (<a href=\"#". $resdicoid . "\">Go</a>)";
         $outbuffer .= "<a name=\"" . $resdicoid . "\"></a>\n";
         if ($count > 1) {
              $outbuffer .= "<p><a href=\"#TOP\">Top</a><p>\n";
              }
         $outbuffer .= "<center><h2>" . @DICOTITLE{$resdicoid} . "</h2></center>\n";
         $oldresdicoid = $resdicoid;
         }

     $outbuffer .= "<font size=+2>" . $resheadword . " </font>"; 
     $pathfile = @SYSDIR{$resdicoid} . $resfilename;
     open (DICOTEXT,$pathfile);
     seek (DICOTEXT,$resstartbyte,0);
     read (DICOTEXT,$theentry,$resbyteoff);
     close (DICOTEXT);
     $outbuffer .= "<blockquote>\n";
    
# Some of them have repeated headwords, some not, so they repeat.
# Select the dico and delete them where required.
	$blowheadwords = 0;
    	if ($resdicoid eq "NICOT1606") {
		$theentry =~ s/<f [^>]*>//g;
		$theentry =~ s/<\/f>//g;
         	$blowheadwords = 1;
        	}
	if ($resdicoid eq "LITTRE1872") {
        	$theentry =~ s/<variante>/<p>/gi;
        	$theentry =~ s/<variante num="([^"]*)">/<p> <b>$1\.<\/b> /gi;
        	$theentry =~ s/<rubrique nom="([^"]*)">/<p><b>$1<\/b><br>/gi;
        	$theentry =~ s/<indent>/<br>&nbsp;&nbsp;&nbsp;&nbsp;/gi;
        	$theentry =~ s/<prononciation>/ (<i>/gi;
        	$theentry =~ s/<\/prononciation>/<\/i>) /gi;
        	$theentry =~ s/<cit aut="" ref="([^"]*)">/<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>$1<\/i>: /gi;
        	$theentry =~ s/<cit aut="([^"]*)" ref="([^"]*)">/<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $1, <i>$2<\/i>: /gi;
        	$theentry =~ s/<\/indent>//gi;
        	$theentry =~ s/<\/cit>//gi;
        	$theentry =~ s/<\/rubrique>//gi;
        	$theentry =~ s/<\/variante>//gi;
        	$theentry =~ s/<head>/<b>/gi;
        	$theentry =~ s/<\/head>/<\/b>/gi;
		}

     	if ($resdicoid eq "ACAD1798") {
		$theentry =~ s:<head><hi>[^<]*</hi></head>\n<p>[^\n]*: :g;
        	}

     	if ($resdicoid eq "ACAD1762") {
         	$blowheadwords = 1;
        	}
     	if ($resdicoid eq "ACAD1835") {
		$theentry =~ s/\n([0-9]*)\xb0/\n$1\&deg;/g;
         	$blowheadwords = 1;
        	}

     if ($resdicoid eq "ACAD1694") {
         $blowheadwords = 1;
        }
     if ($resdicoid eq "FERAUD1787") {
         $blowheadwords = 1;
         $theentry =~ s/\_/\&mdash;/g;
        }
     if ($resdicoid eq "ACAD1932") {
	$theentry =~ s:</hi></head>\n<p>: :g;
        $theentry =~ s:<head><hi>::g;
        $theentry =~ s/<DD>//g;
        $theentry =~ s/<BR>\n/\n/g;
        }
     if ($blowheadwords) {
         $theentry =~ s/<head><hi>[^<]*<\/hi><\/head>//g; 
        }
 
     $theentry =~ s/\' */\'/g;
     $theentry =~ s/--/-/g;

     $outbuffer .= $theentry;
     $theentry = "";
     $outbuffer .= "</blockquote>\n";

     $outbuffer .= "\n";
     }

# Finished getting results.  This should probably be a subroutine, since
# I am running a condition here.  Let's leave it for now.

        if ($count > 0) {
        	if ($count > 1) {
                 	$rtn .= "<a name=\"TOP\"></a>\n";
                 	$rtn .=  "<p>Found <b>" . $count ."</b> Entries in ";
                 	$rtn .= $mvotitlebuf;
			}
		$rtn .="<hr/>\n";
		$rtn .= $outbuffer;
                }
	}
return ($rtn);
}

sub editquerystring {
	my $querystring = $_[0];
	$querystring =~ s/\@([a-zA-Z0-9\[])/(\[^a-zA-Z0-9\]|^)$1/g;
	$querystring =~ s/([a-zA-Z0-9\]])\@/$1\([^a-zA-Z0-9\]|\$)/g;
	return ($querystring);
	}

1;


