#! /usr/bin/perl -T
#
# Copyright 2001,2004 by Stefan Hornburg (Racke) <racke@linuxia.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA.

$ENV{'PATH'} = '/bin';

use strict;
use CGI qw/:standard/;

# Swish++ module
use lib qw(/usr/lib/swish++);

eval {
	require WWW;
};

print header() unless param('file');
print start_html(-title=>'Debian Online Help: Search Result',
				 -author=>'dhelp@packages.debian.org',
				 -BGCOLOR=>'white'), "\n";
if (param('file')) {
	print img({src=>'file:/usr/share/doc/dhelp/debian.jpg',alt=>'Debian GNU/Linux'}), "\n";
} else {
	print img({src=>'/doc/dhelp/debian.jpg',alt=>'Debian GNU/Linux'}), "\n";
print p();
}

unless (-x '/usr/bin/search++') {
    print "No search engine installed. Please install swish++.<BR>";
	print end_html();
	exit 0;
}

unless (-f '/var/lib/dhelp/swish++.index') {
    print q{No search database found.<BR>
Please run /etc/cron.weekly/dhelp as superuser to create it.<BR>};			
	print end_html();
	exit 0;
}

# Fetch, untaint and check search parameter
my $search = param('search');
$search =~ m/([\w\d\@._-]+)/;
$search = $1;

if ($search !~ /\S/) {
	print "Please specify a search token.<BR>";
	print end_html();
	exit 0;
}

# Pass parameters to Swish++ search program
open (SEARCH, '-|')
	or exec '/usr/bin/search++', '-i', '/var/lib/dhelp/swish++.index', $search;

##
# Header HTML
##
my $chunk = <<END;
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>
END

##
# Read the search results back.
##
my $ignored;
while ( <SEARCH> ) {
	if ( /^\# ignored: / ) {
		##
		# Get the ignored words so we can report them to the user.
		##
		$ignored = $';
		$ignored = s/\s+$//;
		next;
	}
	if ( /^\# results: (\d+)/ && ! $1) {
		$chunk = '';
		if ($ignored) {
			print "No results (Ignored: $ignored).<BR>";
		} else {
		    print "No results.<BR>";
		}
		last;
	}
	print $chunk;
	##
	# Future releases of SWISH++ may emit other comments: ignore ones we
	# don't know about.
	##
	next if /^\#/;

	my( $rank, $file, $size, $title ) = split( / /, $_, 4 );

	my $desc = WWW::extract_description( "$file" );
	WWW::hyperlink( $desc );
	$size = int( $size / 1024 );
	if ( $size ) {
		$size .= 'K';
	} else {
		$size = '&lt;1K';
	}
	if (param('file')) {
		$file = "file:$file";
	} else {
		$file =~ s%^/usr/share/%/%;
	}
	unless ($title =~ /\S/ ) {
		$title = $file;
	}
	$chunk = <<END;
	<TR VALIGN=top><TD ALIGN=right>$rank%&nbsp;&nbsp;</TD>
	<TD><DL><DT><B><A HREF="$file">$title</A></B> ($size)<DD>$desc</DL></TD>
END
}

if ($chunk) {
	print "$chunk</TABLE>\n";
}

close (SEARCH);

print end_html(), "\n";

__END__


