#!/opt/perl-5.004/bin/perl -w
#
# $Id: treesearch.pl,v 1.6 1997/06/22 19:16:34 webadm Exp $
#
#
# DESCRIPTION: treesearch.pl - read input and response with a dynamic
#   html-page for finding Documents in a virtual searchtree
#
# URL: none yet
#
# AUTHOR: Cord Beermann (cord@Wunder-Nett.org)
#
# Thanks to: Ralf Begemann (begemann@cc.fh-lippe.de)
#	     Gerold Meerkoetter (gerold@noc.fh-lippe.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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
# A Perl script is "correct" if it gets the job done before your boss fires
# you.
# ('Programming Perl Second Edition' by Larry Wall, Tom Christiansen
#   & Randal. L. Schwartz)
#
# Bugs and shortcomings
# none yet
#
# Todo
# * write instructions
#
### Configuration ###
#
$WEBROOT="/var/infosystems/www/webroot";
$DBROOT="/fhl/dvz/loesungen";
$URLROOT="http://www.fh-lippe.de";
$FILE_TITLE_DB="$WEBROOT$DBROOT/.file_title.db";
$KW_FILE_DB="$WEBROOT$DBROOT/.kw_file.db";
$FILE_KW_DB="$WEBROOT$DBROOT/.file_kw.db";
#
### End of Configuration ###

# Initialize Variables
use vars qw ! $WEBROOT $DBROOT $FILE_TITLE_DB $KW_FILE_DB $FILE_KW_DB
	%kw_file_db %file_title_db %file_kw_db $s ! ;

# include perl-modules
use CGI::Carp;
use CGI qw(:standard);
use CGI qw(:shortcuts);
use POSIX;
use GDBM_File;
use Set::Scalar;

# initialize the CGI-Module and load the form-input
$query = new CGI;
$query->autoEscape(undef);

# get The current url and the request
$url = $query->url;
$self_url = $query->self_url;
$request = $query->path_info();

# open the Databases
tie(%kw_file_db, GDBM_File, $KW_FILE_DB, O_RDONLY, 0) or die("open
    $KW_FILE_DB: $!\n");
tie(%file_kw_db, GDBM_File, $FILE_KW_DB, O_RDONLY, 0) or die("open
    $FILE_KW_DB: $!\n");
tie(%file_title_db, GDBM_File, $FILE_TITLE_DB, O_RDONLY, 0) or die("open
    $FILE_TITLE_DB: $!\n");

# print HTTP-Header
print $query->header;

# check for input-request
unless ($request) {
    $request = '';
    # load all known keywords in @remainkw
    @remainkw = keys %kw_file_db;
} else {
    # split the requested keywords and load them in @request
    @request = split(/\//, $request);
    # shift empty fields out of @request
    shift @request unless length($request[0]);
    # loop for each requested keyword
    foreach $reqkeyword (@request) {
	# if $filelist isn't defined
	unless (defined($filelist)) {
	    # load all known database-files
	    $filelist = Set::Scalar->new(split(/\s/,
					       $kw_file_db{$reqkeyword}));
	} else {
	    # load all files which are related with the current keyword in $a
	    $a = Set::Scalar->new(split(/\s/, $kw_file_db{$reqkeyword}));
	    # put all filenames in $filelist which are in $filelist AND $a
	    $filelist = Set::Scalar->intersection($filelist, $a);
	}
    }
    # loop on each remaining file
    for $file ($filelist->members) {
	# load remaining files in @documents
	push(@documents, $file);
	# loop for each keyword which is in the remaining files
	foreach $keyword (split(/\s/, $file_kw_db{$file})) {
	    # if keyword isn't already selected
	    unless ($request =~ /$keyword/) {
		# load it in @remainkw
		push(@remainkw, $keyword);
	    }
	}
    }
    # remove double keywords from @remainkw
    @remainkw = grep !$s{$_}++, @remainkw;
}

# print HTML-Head
print $query->start_html(-"title"=>"L&ouml;sungsdatenbank - Virtueller Suchbaum",
			 -"author"=>'gnats@thalassa.cc.fh-lippe.de',
			 -"base"=>'true',
			 -"meta"=>{'keywords'=>"$request"},
			 -"text"=>'#000000',
			 -"BGCOLOR"=>'#FFFFF7');

print "\n";

print img {-src=>'/Images/fhl/fh-picto.030tc3d-2.gif',-alt=>'Logo'};

print("\n", 
      a({href=>'/'}, "Einstiegsseite"), 
      " /\n", 
      a({href=>'/fhl/'}, "FH Lippe"),
      " /\n",
      a({href=>'/fhl/dvz/'}, "DVZ"),
      " /\n",
      a({href=>'/fhl/dvz/loesungen/'}, "L&ouml;sungsdatenbank"),
      "\n",
      hr, 
      "\n",
      h1("L&ouml;sungsdatenbank","-","virtueller","Suchbaum"),
      );

# Line for Debugging-Purposes
# print p($#request, $#remainkw, $#documents);

# if there are selected keywords print this part.
if ($#request > -1) {
    print h2("Ausgew&auml;hlte Stichw&ouml;rter:");
    print p("Die folgenden Stichw&ouml;rter wurden bisher ausgew&auml;hlt.
	    Um die Auswahl f&uuml;r ein Stichwort aufzuheben, w&auml;hlen
	    Sie den Begriff nochmals aus.");
    # make an anchor for deselecting choosen keywords
    foreach $keyword (@request) {
	$request_url = $self_url;
	$request_url =~ s|/$keyword||;
	print(a({href=>"$request_url"}, $keyword) , "\n");
    }
}

# If there are no choosen keywords or if there are more than one remaining
#   keyword and more than one matching document, print this section.
if ($#request < 0 or ($#remainkw > 0 and $#documents > 0)) {
    print h2("verf&uuml;gbare Stichw&ouml;rter:");
    print p("Die folgenden Stichw&ouml;rter sind verf&uuml;gbar. Um die
	    Suche einzugrenzen, w&auml;hlen Sie einen Begriff aus.");
    # sort the keywords alphabetically and loop over them
    foreach $keyword (sort {"\l$a" cmp "\l$b"} (@remainkw)) {
	# print a | for better readability
	print(' | ') if ($first);
	# and anchor it
	print(a({href=>"$self_url/$keyword"}, $keyword) , "\n");
	$first = 1;
    }
}

# if there is one keyword selected print this section
if ($#request > -1) {
    print h2("gefundene Dokumente:");
    print p("Die folgenden Dokumente wurden gefunden. W&auml;hlen Sie ein
	    Dokument aus.\n<br><br><ul>");
    # sort alphabetically and loop for each document
    foreach $file (sort {"\l$a" cmp "\l$b"} (@documents)) {
	# anchor the document
	print(li(a({href=>"$URLROOT$DBROOT/$file"}, $file_title_db{$file})), "\n");
    }
    # print this to help the frustrated user
    print('</ul>',
	  'Wenn keines der aufgef&uuml;hrten Dokumente Ihr Problem
	    beschreibt, k&ouml;nnen Sie Ihr Problem &uuml;ber ein ',
	  a({href=>'http://www.fh-lippe.de/cgi-bin/quick-report-page.pl?OPTION_betreff=' . $self_url},
	    'Formular'), ' an die ',
	  a({href=>"/fhl/dvz/loesungen/misc/benutzerberatung.html"},
	  'Benutzerberatung'), ' weiterleiten.', br, "\n");
}

# print the HTML-Footer.

print hr;
print address('Autor: ' . a({href=>"http://Cord.de/"}, "Cord Beermann"));
print "\n";
print $query->end_html;
