#!/usr/bin/perl -T
# $Id: find,v 1.24 2004/09/02 15:26:42 brondsem Exp $

# find   --	Find files
#
#	Arne Georg Gleditsch <argggh@ifi.uio.no>
#	Per Kristian Gjermshus <pergj@ifi.uio.no>
#
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.

######################################################################

$CVSID = '$Id: find,v 1.24 2004/09/02 15:26:42 brondsem Exp $ ';

use strict;
use lib '.'; # for Local.pm
use lib do { $0 =~ m{(.*)/} ? "$1/lib" : "lib" }; # if LXR modules are in ./lib

use LXR::Common qw(:html);
use LXR::Config;

sub varinputs {
	my $templ = shift;
	my $ret   = '';

	foreach ($config->allvariables) {
		if ($config->variable($_) ne $config->vardefault($_)) {
			$ret .= expandtemplate(
				$templ,
				(
					variable => sub { $_ },
					value    => sub { $config->variable($_) },
				)
			);
		}
	}
	return $ret;
}

sub printresults {
	my $templ   = shift;
	my @results = @_;
	my $ret     = '';

	foreach (@results) {
		$ret .= expandtemplate($templ, (fileref => sub { fileref("$_", "find-file", "/$_") }));
	}
	return $ret;
}

sub dofind {
	my ($searchtext, $FILELISTING, $advanced, $casesensitive) = @_;
	my @ret;

	if ($searchtext ne "") {
		my $sourceroot = $config->sourceroot . '/' . $release . '/';
		while (my $file = <$FILELISTING>) {
			chomp $file;
			$file =~ s/^$sourceroot//;
			if ($advanced) {
				if ($casesensitive) {
					if ($file =~ /$searchtext/) {
						push @ret, $file;
					}
				} elsif ($file =~ /$searchtext/i) {
					push @ret, $file;
				}
			} else {
				if ($casesensitive) {
					if (index($file, $searchtext) != -1) {
						push @ret, $file;
					}
				} elsif (index(lc($file), lc($searchtext)) != -1) {
					push @ret, $file;
				}
			}
		}
	}
	return @ret;
}

sub find {
	my $templ;

	if ($config->htmlfind) {
		unless (open(TEMPL, $config->htmlfind)) {
			warning("Template " . $config->htmlfind . " does not exist.");
		} else {
			local ($/) = undef;
			$templ = <TEMPL>;
			close(TEMPL);
		}
	} else {
		die "'htmlfind' template not configured";
	}

	my $searchtext    = $HTTP->{'param'}->{'string'};
	my $advanced      = $HTTP->{'param'}->{'advanced'};
	my $casesensitive = $HTTP->{'param'}->{'casesensitive'};

	my $FILELISTING;
	if ($config->swishdir and $config->swishbin) {
		unless ($FILELISTING = new IO::File($config->swishdir . "/$release.filenames")) {
			&warning(
				"Version '$release' has not been indexed and is unavailable for searching<br>Could not open "
				  . $config->swishdir
				  . "/$release.filenames.");
			return;
		}
	} elsif ($config->glimpsedir and $config->glimpsebin) {
		unless ($FILELISTING =
			new IO::File($config->glimpsedir . "/" . $release . "/.glimpse_filenames"))
		{
			&warning(
				"Version '$release' has not been indexed and is unavailable for searching<br>Could not open "
				  . $config->glimpsedir
				  . "/$release/.glimpse_filenames.");
			return;
		}
	} else {
		warning(
			"Freetext search engine required for file search, and no freetext search engine is configured"
		);
		return;
	}

	my @results = dofind($searchtext, $FILELISTING, $advanced, $casesensitive);
	close($FILELISTING);

	print expandtemplate(
		$templ,
		(
			variables          => sub { varinputs(@_) },
			advancedchecked    => sub { return $advanced ? "checked" : "" },
			searchtext         => sub { return $searchtext },
			searchtext_escaped => sub { $_ = $searchtext; s/\"/&quot;/g; return $_; },
			casesensitivechecked => sub { return $casesensitive ? "checked" : "" },
			results     => sub { printresults(@_, @results) },
			resultcount => sub { return scalar @results },
		)
	);
}

httpinit;

&makeheader('find');
&find;
&makefooter('find');

httpclean;

