#! /usr/bin/perl
###############################################################################
#
# "Power" search interface
#
# Written by Behan Webster <behanw@verisim.com>
# Copyright (c) 1996 by Verisim, Inc.  All rights reserved.
#
###############################################################################


require query;


###########################################################################
# The power query form
#
sub powerQueryForm {
	my ($frame, $page, $display, $number, @query) = @_;

	my ($i, $and, $or, $not, $close, $followed);
	my $summary = "selected" if $display eq "s";
	my $jump    = "selected" if $display eq "j";
	my $compact = "selected" if $display eq "c";
	my $defnum  = ($number > 0) ? $number : "all";
	my $deframe = ($frame  == 1)  ? 1 : 0;
	my $target  = "target=_top" if $frame > 1;

    $query[0] =~ s/\"/&quot;/g;

	background($textBack);

print qq{
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
  <td valign=top>
    <a href="$homeUrl" target=_top>
    <img $powerLogo align=left>
	</a>
  </td>
  <td width=100%>
} if $frame == 1;

print qq{
<center><form action="$powerCgi" method=get $target>
  <input type=hidden name=f value=$deframe>
  <input type=hidden name=p value=$page>

  <table border=0 cellspacing=0 cellpadding=0>

    <tr><td align=center>Search for</td>
    <td><input type=text name=q0 size=15 maxlength=100 value=}.$query[0].qq{></td></tr>
};

    for ($i=1; $i<=$page; $i++) {
		$_ = $query[$i*2-1];
		$and      = (/^a?$/) ? "selected" : "";;
		$or       = (/^o$/)  ? "selected" : "";;
		$not      = (/^n$/)  ? "selected" : "";;
		$close    = (/^c$/)  ? "selected" : "";;
		$followed = (/^f$/)  ? "selected" : "";;

    	$query[$i*2] =~ s/\"/&quot;/g;

print qq{
    <tr>
      <td><select name=o$i>
        <option value=a $and> and
        <option value=o $or> or
        <option value=n $not> but not
        <option value=c $close> close to
        <option value=f $followed> followed by
      </select></td>
      <td><input type=text name=q$i size=15 maxlength=100 value=}.$query[$i*2].qq{></td>
    </tr>
};

	}

print qq{
    <tr><td>&nbsp;</td><td align=center>
      <input type=image value="[more terms]" name="more" $moregif>
    </td></tr>

  </table>

  <table border=0 cellspacing=0 cellpadding=10><tr align=center>
    <td><input type=submit value=Submit name=done></td>
    <td><input type=reset  value=Clear></td>
    <td><input type=submit value=Help name=help></td>
  </tr></table>

  <br><nobr>Display results as
  <select name=d>
    <option value=s $summary> summary list
};

print qq{
    <option value=j $jump> jump list
} if $frame > 1;

print qq{
    <option value=c $compact> compact list
  </select></nobr>
  <nobr>Show
  <input type=text name=n value="$defnum" size=5 maxlength=10>
  entries at once.</nobr>

  <center><p>
    <a href="$simpleCgi" target=_top>simple search</a>
	&nbsp;&nbsp;
  	<font color=#555555>power search</font>
	&nbsp;&nbsp;
    <a href="$advancedCgi" target=_top>advanced search</a>
  </center>
</form></center>
};

print qq{
  </td>
</tr>
</table>
} if $frame == 1;

}


###########################################################################
# The Power search logo page
#
sub logoPage {

print qq{
<center>
<a href="$homeUrl" target=_top>
<img $powerLogo></a>
</center>
};

}


###########################################################################
# The power set of frames
#
sub powerFrames {
	my ($page, $display, $number, @query) = @_;

	my $defnum  = ($number > 0) ? $number : "all";
	my $query = join('+', @query);
	$query =~ tr/ /+/;

print qq{
<frameset rows="*,43">
} if @buttons;

print qq{
<frameset cols="25%,*">
  <frame src="$cmd?3" name=logo scrolling=no marginheight=100>
  <frame src="$cmd?2+$page+$display+$defnum+$query" name=query marginheight=20>
</frameset>
};

print qq{
  <frame src="$cmd?4" name=buttons scrolling=no marginwidth=0 marginheight=0>
</frameset>
} if @buttons;

print qq{
<noframes>
};

	#
	# Just in case for some bizarre reason they all of a
	# sudden don't have frames.
	#
	powerQueryForm(1, $page+2, $display, $number, @query);
	print "<hr>\n";
	buttonBar();

print qq{
</noframes>
};

}


###########################################################################
# Put together the query from an array of operands and operators
#
sub makeQuery {
	my ($query, @query);
	($_, @query) = @_;

	#
	# Put the first operand in the query
	#
	$query = (/\s/) ? qq{"$_"} : qq{$_};

	while ($query[1]) {
		#
		# add the operator to the query
		# NOTE: The default operator is "and"
		#
		$_ = $query[0];
		$query .= ' AND'  if /^a$/;	# And
		$query .= ' OR'   if /^o$/;	# Or
		$query .= ' NOT'  if /^n$/;	# Not
		$query .= ' NEAR' if /^c$/;	# Close to
		$query .= ' 1'    if /^f$/;	# Followed by

		#
		# add the operand to the query
		#
		$_ = $query[1];
		$query .= (/\s/) ? qq{ "$_"} : qq{ $_};

		#
		# Turn off any previous proximity operators
		#
		$query .= ' 0' if $query[3] !~ /^$/ &
		                  $query[0] =~ /^[cf]$/ &
		                  $query[2] !~ /^[cf]$/;

		#
		# we're finished with the first operator/operand pair
		#
		shift @query; shift @query;
	}

	#
	# Return the completed query
	#
	return $query;
}

###########################################################################
# Parse the command arguments
#
if ($#ARGV<0) {
	if ($ENV{'QUERY_STRING'}) {
		#
		# Read key pairs from the get
		#
		splitPairs($ENV{'QUERY_STRING'});
		$frames  = $FORM{'f'};
		$page    = $FORM{'p'};
		$display = $FORM{'d'};
		$number  = $FORM{'n'};
		$done    = $FORM{'done'};

		#
		# Build the query
		#
		if ($#query < 0) {
			my $i;

			push (@query, $FORM{'q0'});
			for ($i=1; $FORM{"q$i"}; $i++) {
				push (@query, $FORM{"o$i"} );
				push (@query, $FORM{"q$i"} );
			}
		}

	}
} else {
	#
	# Read the argument list from the command line
	#
	($frames, $page, $display, $number, @query) = @ARGV;
}

$query = unarmorURL($query);

#
# The power logo page
#
if ($frames == 3) {
	title("Logo Page");
	background($background);
	logoPage();
	exit;
}

#
# The button bar
#
if ($frames == 4) {
	title("Button Bar");
	background($textBack);
	buttonBar();
	exit;
}

#
# Print help file if it's been requested
#
if ($FORM{'help'}) {
    filePage($helpfile);
    exit;
}

#
# Set to defaults if these are undefined
#
$display = "s"  if !$display;
$number  = -1   if $number =~ /(all|ALL|\*)/;
$number  = 15  if !$number | $number =~ /^$/ | $number !~ /[0-9]/;

#
# An actual query!  How exciting!
#
if ($done) {
	#
	# Put together the query
	#
	$query    = makeQuery(@query);
	$_ = $query;
	tr/ /+/;

	#
	# Query the index and print the results
	#
	$page=1;
	$frames = 0 if $frames != 1;
	resultsPage($frames, 1, $query, $_, $advancedCgi, "advancedQueryForm");

#
# See if more lines were requested
#
} else {
	#
	# Print html file header
	#
	title("Query");

	#
	# Print the overall frameset/noframes page
	#
	if (!$frames) {
		powerFrames($page, $display, $number, @query);

	#
	# Just print the query form by default if no options were specified
	#
	} else {
		powerQueryForm($frames, $page+2, $display, $number, @query);
	}

	#
	# Finish it off
	#
	end();
}
