#!/usr/bin/perl
#------------------------------------------------------------------------------
# Copyright (C) 2001 Thomax Kaulmann
# 
# 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, 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.

use CGI;
use DBI;
use OMA;
use XML::Parser;
use Net::HTTP;


local %metaand;
undef(%metaand);

my $USEMYSQL = 0;



                        ###     ###        ##     ##
                         ##      ##               ##
 ## ##   ####   ## ###   ##  ##  ## ##    ###    #####    ####
#######     ##   ### ##  ## ##   ### ##    ##     ##     ##
## # ##  #####   ##      ####    ##  ##    ##     ##      ####
## # ## ##  ##   ##      ## ##   ##  ##    ##     ## ##      ##
##   ##  ### ## ####    ###  ## ###  ##   ####     ###   #####
sub markhits {
	my $line = shift;
	my $kwords = lc(shift);
	my $ready = 0;
	my $last = -1;
	my $passage = "";
	my $i;
	my $n;

	$line =~ tr/(\t|\ |\r|\n)/ /s;

	my @words=split(/\ /,$line);

	for($i=0; $i<=$#words && $ready < 3; $i++) {
		if(lc($words[$i]) =~ /$kwords/ && $i > $last) { 
			for($n=($i>=10)?$i-10:0; $n<$i+20 ;$n++) {
				if(lc($words[$n]) =~ /$kwords/) {
					$passage .= "<b>$words[$n]</b> ";
				} else {
					$passage .= "$words[$n] ";
				}
			}
			chop($passage); 
			$passage .= ".. ";
			$last = $n;
			$ready++;
		}
	}
	$passage = $line if($passage eq "");
	return($passage);
}



                                          ##
                                          ##
## ###   ####   ## ###    ####    ####   #####   ####     #####
 ##  ##     ##   ### ##  ##      ##  ##   ##        ##   ##  ##
 ##  ##  #####   ##       ####   ######   ##     #####   ##  ##
 #####  ##  ##   ##          ##  ##       ## ## ##  ##    #####
 ##      ### ## ####     #####    ####     ###   ### ##      ##
####                                                     #####
sub parsetag {
	local $xmltext = shift;
	local $path = shift;
	local $hit = shift;
	local $numpages = shift;

	local @xmlarr = split(/\n/,$xmltext);
	local $p = XML::Parser->new(Style => 'Stream');
	local $doc = $p->parse($xmltext);

	sub StartDocument {}
	sub EndTag {
		local $exp = shift;
		local $type = shift;
		local $tag = $_;
		local $otag = $tag;
		chop($otag);

#		if($type eq "loop") {
#			$xmlarr[$exp->current_line-1] =~ s/$otag\/?>//;
#		}
	}
	sub Text {
	}

	sub StartTag {
		local $exp = shift;
		local $type = shift;
		local %attr = %_;
		local $tag = $_;
		local %hash = %$exp;
		local $otag = $tag;
		chop($otag);


		if($type eq "search-path") {
			if($path ne "") {
				$xmlarr[$exp->current_line-1] =~ s/$otag\/?>/<br\/>$path<br \/>/;
			} else {
				$xmlarr[$exp->current_line-1] =~ s/$otag\/?>//;
			}
		}
		if($type eq "search-field") {
			$xmlarr[$exp->current_line-1] =~ s/$otag[\ ]*\/?>/<input type="text" name="qstring" value="$in{'qstring'}"\/>/;
		}
		if($type eq "search-hit" && $hit ne "") {
			$xmlarr[$exp->current_line-1] =~ s/$otag\/?>/$hit/;
		}
#		if($type eq "loop") {
#			$xmlarr[$exp->current_line-1] =~ s/$otag\/?>//;
#		}
		if($type eq "search-pages" && $numpages > 0) {
			local $s="";
			local $x;
			for($x=0;$x<=$numpages;$x++) {
				if($in{'n'} != $x) {
					$s .= " <a href=\"/oma-bin/usearch?qstring=$in{'qstring'}&n=$x\">". ($x+1) ."</a> ";
				} else {
					$s .= " ". ($x+1) ." ";
				}
			}
			$xmlarr[$exp->current_line-1] =~ s/$otag[\ ]*\/?>/$s/;
		}
        if($type eq "tree-view") {
            my $tree = OMA::painttree();
            $xmlarr[$exp->current_line-1] =~ s/$otag[\ ]*\/?>/$tree/;
        }
		if($type eq "fast-arch") {
			my $form = OMA::fastArch();
			$xmlarr[$exp->current_line-1] =~ s/$otag[\ ]*\/?>/$form/;
		}

	}
	$xmltext="";
	for($i=0;$i<=$#xmlarr;$i++) {
		$xmltext .= $xmlarr[$i];
	}
	return($xmltext);
}


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

sub qterms {
	my @all = split(/\ /,$_[0]);
	my $i;
	my $n;
	my $c=0;
	my $exp;
	my @ret;

	for($i=0;$i<=$#all;$i++) {
		$exp="";
		if($all[$i] =~ /^"/ || $all[$i] =~ /[+,-]"/) {
			$n=$i;
			do {
				if(!($all[$n] =~ /"$/)) {
					$exp .= "$all[$n++] ";
				}
			} while(!($all[$n] =~ /"$/));
			$exp .= $all[$n];
			$i=$n;
			$exp =~ s/\"//g;
		} else {
			$exp = $all[$i];
		}
		$ret[$c++] = $exp;
	}
	return(@ret);
}


          ###           ###               ###
           ##            ##                ##
  #####    ##     ####   #####   ####      ##
 ##  ##    ##    ##  ##  ##  ##     ##     ##
 ##  ##    ##    ##  ##  ##  ##  #####     ##
  #####    ##    ##  ##  ##  ## ##  ##     ##
     ##   ####    ####  ## ###   ### ##   ####
 #####
                   ##      ###
                            ##
         ##  ##   ###    #####    ####    ####
         ##  ##    ##   ##  ##   ##  ##  ##  ##
         ##  ##    ##   ##  ##   ######  ##  ##
          ####     ##   ##  ##   ##      ##  ##
           ##     ####   ### ##   ####    ####
########
sub global_video
{
	local $qstring = shift;
	local $conj = shift;
	local $meta = shift;
	local @terms;
	local $i;
	local %listed;
	local $not = "";
	local $conj2 = "OR";
	local $path;

	@terms = qterms($qstring);
	$qstring="";
	for($i=0;$i<=$#terms;$i++) {
	    if($terms[$i] =~ /^-/) {
			$not = " not ";
			$conj2 = "AND";
			$terms[$i] =~ s/^-//;
		} else {
			$not = "";
			$conj2 = "OR";
		}

		if($USEMYSQL) {
			$qstring .= "
           	(
				lcase(ova_page.dc__title) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ova_page.dc__title__alternative) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ova_page.dc__publisher) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ova_page.dc__description__toc) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ova_page.dc__description__abstract) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ova_page.dc__description__long) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ova_page.dc__type) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ova_page.dc__subject) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ova_page.dc__coverage__spatial) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ova_page.dc__coverage__temporal) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ova_creator.dc__creator) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ova_contributor.dc__contributor) $not regexp lcase('[[:<:]]($terms[$i])+')
			) ";
		} else {
			$qstring .= "
			(
                ova_page.dc__title ilike '%$terms[$i]%' $conj2
                ova_page.dc__title__alternative ilike '%$terms[$i]%' $conj2
                ova_page.dc__publisher ilike '%$terms[$i]%' $conj2
                ova_page.dc__description__toc ilike '%$terms[$i]%' $conj2
                ova_page.dc__description__abstract ilike '%$terms[$i]%' $conj2
                ova_page.dc__description__long ilike '%$terms[$i]%' $conj2
                ova_page.dc__type ilike '%$terms[$i]%' $conj2
                ova_page.dc__subject ilike '%$terms[$i]%' $conj2
                ova_page.dc__coverage__spatial ilike '%$terms[$i]%' $conj2
                ova_page.dc__coverage__temporal ilike '%$terms[$i]%' $conj2
                ova_creator.dc__creator ilike '%$terms[$i]%' $conj2
                ova_contributor.dc__contributor ilike '%$terms[$i]%'
           )";
		}
		$qstring .= " $conj " if($i < $#terms);
	}
	$st=$db->prepare("
		SELECT	item.sid,item.description,node.node,item.type,treemap.node,ova_conn.page
		FROM	item,ova_conn,ova_page,node,treemap,ova_creator,ova_contributor
		WHERE	ova_page.ID=ova_conn.ova_page
		AND		item.sid=ova_conn.item_sid
		AND		ova_page.ID=ova_creator.ID
		AND		ova_page.location=ova_creator.location
		AND		ova_page.ID=ova_contributor.ID
		AND		ova_page.location=ova_contributor.location
		AND		item.node_sid=node.sid
		AND		item.published = 1
		AND		treemap.sid=node.sid
		AND		$meta
		AND		( $qstring )
		ORDER BY	treemap.node,item.description
	");
	$st->execute();

	while(@row=$st->fetchrow()) {
		$path = $row[4];
		$path =~ s/\ /%20/g;
		if(!$listed{$row[0]}) {
			if($row[3] eq "meta") {
				$metaand{$row[0]}++;
#				$results{"$row[4]/$row[0]"} = "<img alt=\"$OMA::MEDIA_TYPE{$row[3]}\" src=\"http://$OMA::OMAHOST/img/$OMA::MEDIA_TYPE{$row[3]}.png\" /> <a class=\"clink\" href=\"http://$OMA::OMAHOST/$OMA::OMABIN/parse?op=showitem&item=$row[0]&page=1\">$row[1]</a><br />\n";
				$results{"$row[4]/$row[0]"} = "<img alt=\"$OMA::MEDIA_TYPE{$row[3]}\" src=\"http://$OMA::OMAHOST/img/$OMA::MEDIA_TYPE{$row[3]}.png\" /> <a class=\"clink\" href=\"http://$OMA::OMAHOST$OMA::BASE$path/$row[0].html\">$row[1]</a><br />\n";
			}
			else {
#				$results{"$row[4]/$row[0]"} = "<img alt=\"$OMA::MEDIA_TYPE{$row[3]}\" src=\"http://$OMA::OMAHOST/img/$OMA::MEDIA_TYPE{$row[3]}.png\" /> <a class=\"clink\" href=http://$OMA::OVAHOST/ova-perl/ova-parse?op=showitem&item=$row[0]&page=$row[5]>$row[1]</a><br />\n";
				$results{"$row[4]/$row[0]"} = "<img alt=\"$OMA::MEDIA_TYPE{$row[3]}\" src=\"http://$OMA::OMAHOST/img/$OMA::MEDIA_TYPE{$row[3]}.png\" /> <a class=\"clink\" href=\"http://$OMA::OMAHOST$OMA::BASE$path/$row[0].html\">$row[1]</a><br />\n";
			}

			$listed{$row[0]} = 1;
		}
	}
	$st->finish();
}



          ###           ###               ###
           ##            ##                ##
  #####    ##     ####   #####   ####      ##
 ##  ##    ##    ##  ##  ##  ##     ##     ##
 ##  ##    ##    ##  ##  ##  ##  #####     ##
  #####    ##    ##  ##  ##  ## ##  ##     ##
     ##   ####    ####  ## ###   ### ##   ####
 #####
                           ###     ##
                            ##
         ####    ##  ##  #####    ###     ####
            ##   ##  ## ##  ##     ##    ##  ##
         #####   ##  ## ##  ##     ##    ##  ##
        ##  ##   ##  ## ##  ##     ##    ##  ##
         ### ##   #####  ### ##   ####    ####
########
sub global_audio
{
	local $qstring = shift;
	local $conj = shift;
	local $meta = shift;
	local @terms;
	local $i;
	local %listed;
	local $not = "";
	local $conj2 = "OR";
	local $path;

#	print "<p></p>\n";
	@terms = qterms($qstring);
	$qstring="";
	for($i=0;$i<=$#terms;$i++) {
	    if($terms[$i] =~ /^-/) {
			$not = " not ";
			$conj2 = "AND";
			$terms[$i] =~ s/^-//;
	    } else {
			$not = "";
			$conj2 = "OR";
		}

		if($USEMYSQL) {
			$qstring .= "
           	(
				lcase(ora_page.dc__title) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ora_page.dc__title__alternative) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ora_page.dc__publisher) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ora_page.dc__description__toc) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ora_page.dc__description__abstract) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ora_page.dc__description__long) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ora_page.dc__type) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ora_page.dc__subject) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ora_page.dc__coverage__spatial) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ora_page.dc__coverage__temporal) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ora_creator.dc__creator) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ora_contributor.dc__contributor) $not regexp lcase('[[:<:]]($terms[$i])+')
			) ";
		} else {
			$qstring .= "
			(
                ora_page.dc__title ilike '%$terms[$i]%' $conj2
                ora_page.dc__title__alternative ilike '%$terms[$i]%' $conj2
                ora_page.dc__publisher ilike '%$terms[$i]%' $conj2
                ora_page.dc__description__toc ilike '%$terms[$i]%' $conj2
                ora_page.dc__description__abstract ilike '%$terms[$i]%' $conj2
                ora_page.dc__description__long ilike '%$terms[$i]%' $conj2
                ora_page.dc__type ilike '%$terms[$i]%' $conj2
                ora_page.dc__subject ilike '%$terms[$i]%' $conj2
                ora_page.dc__coverage__spatial ilike '%$terms[$i]%' $conj2
                ora_page.dc__coverage__temporal ilike '%$terms[$i]%' $conj2
                ora_creator.dc__creator ilike '%$terms[$i]%' $conj2
                ora_contributor.dc__contributor ilike '%$terms[$i]%'
           )";
		}
		$qstring .= " $conj " if($i < $#terms);
	}
	$st=$db->prepare("
				SELECT	item.sid,item.description,node.node,item.type,
						treemap.node,ora_conn.page
				FROM	item,ora_conn,ora_page,node,treemap,ora_creator,ora_contributor
				WHERE	ora_page.ID=ora_conn.ora_page
				AND		item.sid=ora_conn.item_sid
				AND		item.node_sid=node.sid
				AND		ora_page.ID=ora_creator.ID
				AND		ora_page.location=ora_creator.location
				AND		ora_page.ID=ora_contributor.ID
				AND		ora_page.location=ora_contributor.location
				AND		item.published = 1
				AND		treemap.sid=node.sid
				AND		$meta
				AND		( $qstring )
				ORDER BY	item.description
	");

	$st->execute();
	while(@row=$st->fetchrow()) {
		$path = $row[4];
		$path =~ s/\ /%20/g;
		if(!$listed{$row[0]}) {
			if($row[3] eq "meta") {
				$metaand{$row[0]}++;
				$results{"$row[4]/$row[0]"} = "<img alt=\"$OMA::MEDIA_TYPE{$row[3]}\" src=\"http://$OMA::OMAHOST/img/$OMA::MEDIA_TYPE{$row[3]}.png\" /> <a class=\"clink\" href=\"http://$OMA::OMAHOST$OMA::BASE$path/$row[0].html\">$row[1]</a><br />\n";
				$path =~ s/\ /%20/g;
			}
			else {
				$results{"$row[4]/$row[0]"} = "<img alt=\"$OMA::MEDIA_TYPE{$row[3]}\" src=\"http://$OMA::OMAHOST/img/$OMA::MEDIA_TYPE{$row[3]}.png\" /> <a class=\"clink\" href=\"http://$OMA::OMAHOST$OMA::BASE$path/$row[0].html\">$row[1]</a><br />\n";
			}
			$listed{$row[0]} = 1;
		}
	}
	$st->finish();
}


          ###           ###               ###
           ##            ##                ##
  #####    ##     ####   #####   ####      ##
 ##  ##    ##    ##  ##  ##  ##     ##     ##
 ##  ##    ##    ##  ##  ##  ##  #####     ##
  #####    ##    ##  ##  ##  ## ##  ##     ##
     ##   ####    ####  ## ###   ### ##   ####
 #####
                   ##             ##
                                  ##
        ## ###    ###     ####   #####   ##  ## ## ###    ####
         ##  ##    ##    ##  ##   ##     ##  ##  ### ##  ##  ##
         ##  ##    ##    ##       ##     ##  ##  ##      ######
         #####     ##    ##  ##   ## ##  ##  ##  ##      ##
         ##       ####    ####     ###    ##### ####      ####
############
sub global_picture
{
	local $qstring = shift;
	local $conj = shift;
	local $meta = shift;
	local @terms;
	local $i;
	local %listed;
	local $not = "";
	local $conj2 = "OR";
	local $path;

#	print "<p></p>\n";
	@terms = qterms($qstring);
	$qstring="";
	for($i=0;$i<=$#terms;$i++) {
	    if($terms[$i] =~ /^-/) {
			$not = " not ";
			$conj2 = "AND";
			$terms[$i] =~ s/^-//;
		} else {
			$not = "";
			$conj2 = "OR";
		}

		if($USEMYSQL) {
			$qstring .= "
           	(
				lcase(opa_page.dc__title) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(opa_page.dc__title__alternative) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(opa_page.dc__publisher) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(opa_page.dc__description__toc) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(opa_page.dc__description__abstract) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(opa_page.dc__description__long) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(opa_page.dc__type) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(opa_page.dc__subject) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(opa_page.dc__coverage__spatial) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(opa_page.dc__coverage__temporal) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(opa_creator.dc__creator) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(opa_contributor.dc__contributor) $not regexp lcase('[[:<:]]($terms[$i])+')
			) ";
		} else {
			$qstring .= "
			(
                opa_page.dc__title ilike '%$terms[$i]%' $conj2
                opa_page.dc__title__alternative ilike '%$terms[$i]%' $conj2
                opa_page.dc__publisher ilike '%$terms[$i]%' $conj2
                opa_page.dc__description__toc ilike '%$terms[$i]%' $conj2
                opa_page.dc__description__abstract ilike '%$terms[$i]%' $conj2
                opa_page.dc__description__long ilike '%$terms[$i]%' $conj2
                opa_page.dc__type ilike '%$terms[$i]%' $conj2
                opa_page.dc__subject ilike '%$terms[$i]%' $conj2
                opa_page.dc__coverage__spatial ilike '%$terms[$i]%' $conj2
                opa_page.dc__coverage__temporal ilike '%$terms[$i]%' $conj2
                opa_creator.dc__creator ilike '%$terms[$i]%' $conj2
                opa_contributor.dc__contributor ilike '%$terms[$i]%'
			) ";
		}
		$qstring .= " $conj " if($i < $#terms);
	}
	$st=$db->prepare("
		SELECT	item.sid,item.description,node.node,item.type,
				treemap.node,opa_conn.opa_page
		FROM	item,opa_conn,opa_page,node,treemap,opa_creator,opa_contributor
		WHERE	opa_page.ID=opa_conn.opa_page
		AND		item.sid=opa_conn.item_sid
		AND		item.node_sid=node.sid
		AND		opa_page.ID=opa_creator.ID
		AND		opa_page.location=opa_creator.location
		AND		opa_page.ID=opa_contributor.ID
		AND		opa_page.location=opa_contributor.location
		AND		item.published = 1
		AND		treemap.sid=node.sid
		AND		$meta
		AND		( $qstring )
		ORDER BY	item.description
	");
	$st->execute();
	while(@row=$st->fetchrow()) {
		$path = $row[4];
		$path =~ s/\ /%20/g;
		if(!$listed{$row[0]}) {
			if($row[3] eq "meta") {
				$metaand{$row[0]}++;
				$results{"$row[4]/$row[0]"} = "<img alt=\"$OMA::MEDIA_TYPE{$row[3]}\" src=\"http://$OMA::OMAHOST/img/$OMA::MEDIA_TYPE{$row[3]}.png\" /> <a class=\"clink\" href=\"http://$OMA::OMAHOST$OMA::BASE$path/$row[0].html\">$row[1]</a><br />\n";
			} else {
				$results{"$row[4]/$row[0]"} = "<img alt=\"$OMA::MEDIA_TYPE{$row[3]}\" src=\"http://$OMA::OMAHOST/img/$OMA::MEDIA_TYPE{$row[3]}.png\" /> <a class=\"clink\" href=\"http://$OMA::OMAHOST$OMA::BASE$path/$row[0].html\">$row[1]</a><br />\n";
			}
			$listed{$row[0]} = 1;
		}
	}
	$st->finish();
}


          ###           ###               ###
           ##            ##                ##
  #####    ##     ####   #####   ####      ##
 ##  ##    ##    ##  ##  ##  ##     ##     ##
 ##  ##    ##    ##  ##  ##  ##  #####     ##
  #####    ##    ##  ##  ##  ## ##  ##     ##
     ##   ####    ####  ## ###   ### ##   ####
 #####
          ##                      ##
          ##                      ##
         #####    ####  ##   ##  #####
          ##     ##  ##  ## ##    ##
          ##     ######   ###     ##
          ## ##  ##      ## ##    ## ##
           ###    ####  ##   ##    ###
########
sub global_text
{
	local $qstring = shift;
	local $conj = shift;
	local $meta = shift;
	local @terms;
	local $i;
	local %listed;
	local $not = "";
	local $conj2 = "OR";
	local $path;

#	print "<p></p>\n";
	@terms = qterms($qstring);
	$qstring="";
	for($i=0;$i<=$#terms;$i++) {
		if($terms[$i] =~ /^-/) {
			$not = " not ";
			$conj2 = "AND";
			$terms[$i] =~ s/^-//;
		} else {
			$not = "";
			$conj2 = "OR";
		}

		if($USEMYSQL) {
		$qstring .= "
           	(
				lcase(ota_page.dc__title) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ota_page.dc__title__alternative) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ota_page.dc__publisher) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ota_page.dc__description__toc) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ota_page.dc__description__abstract) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ota_page.dc__description__long) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ota_page.dc__type) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ota_page.dc__subject) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ota_page.dc__coverage__spatial) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ota_page.dc__coverage__temporal) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ota_creator.dc__creator) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(ota_contributor.dc__contributor) $not regexp lcase('[[:<:]]($terms[$i])+')
			) ";
		} else {
		$qstring .= "
			(
                ota_page.dc__title ilike '%$terms[$i]%' $conj2
                ota_page.dc__title__alternative ilike '%$terms[$i]%' $conj2
                ota_page.dc__publisher ilike '%$terms[$i]%' $conj2
                ota_page.dc__description__toc ilike '%$terms[$i]%' $conj2
                ota_page.dc__description__abstract ilike '%$terms[$i]%' $conj2
                ota_page.dc__description__long ilike '%$terms[$i]%' $conj2
                ota_page.dc__type ilike '%$terms[$i]%' $conj2
                ota_page.dc__subject ilike '%$terms[$i]%' $conj2
                ota_page.dc__coverage__spatial ilike '%$terms[$i]%' $conj2
                ota_page.dc__coverage__temporal ilike '%$terms[$i]%' $conj2
                ota_creator.dc__creator ilike '%$terms[$i]%' $conj2
                ota_contributor.dc__contributor ilike '%$terms[$i]%'
			) ";
		}
		$qstring .= " $conj " if($i < $#terms);
	}
	$st=$db->prepare("
				SELECT  item.sid,item.description,node.node,item.type,treemap.node,ota_conn.page
				FROM    item,ota_conn,ota_page,node,treemap,ota_creator,ota_contributor
				WHERE   ota_page.ID=ota_conn.ota_page
				AND     item.sid=ota_conn.item_sid
				AND     item.node_sid=node.sid
				AND		ota_page.ID=ota_creator.ID
				AND		ota_page.location=ota_creator.location
				AND		ota_page.ID=ota_contributor.ID
				AND		ota_page.location=ota_contributor.location
				AND		item.published = 1
				AND     treemap.sid=node.sid
				AND		$meta
				AND     ( $qstring )
				ORDER BY  item.description
	");
	$st->execute();

	while(@row=$st->fetchrow()) {
		$path = $row[4];
		$path =~ s/\ /%20/g;
		if(!$listed{$row[0]}) {
			if($row[3] eq "meta") {
				$metaand{$row[0]}++;
				$results{"$row[4]/$row[0]"} = "<img alt=\"$OMA::MEDIA_TYPE{$row[3]}\" src=\"http://$OMA::OMAHOST/img/$OMA::MEDIA_TYPE{$row[3]}.png\" /> <a class=\"clink\" href=\"http://$OMA::OMAHOST$OMA::BASE$path/$row[0].html\">$row[1]</a><br />\n";
			} else {
				$results{"$row[4]/$row[0]"} = "<img alt=\"$OMA::MEDIA_TYPE{$row[3]}\" src=\"http://$OMA::OMAHOST/img/$OMA::MEDIA_TYPE{$row[3]}.png\" /> <a class=\"clink\" href=\"http://$OMA::OMAHOST$OMA::BASE$path/$row[0].html\">$row[1]</a><br />\n";
			}

			$listed{$row[0]} = 1;
		}
	}
	$st->finish();
}



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

sub global_newasset
{
    local $qstring = shift;
    local $conj = shift;
    local $meta = shift;
    local @terms;
    local $i;
    local %listed;
    local $not = "";
    local $conj2 = "OR";
    local $path;

#   print "<p></p>\n";
    @terms = qterms($qstring);
    $qstring="";
    for($i=0;$i<=$#terms;$i++) {
        if($terms[$i] =~ /^-/) {
            $not = " not ";
            $conj2 = "AND";
            $terms[$i] =~ s/^-//;
        } else {
            $not = "";
            $conj2 = "OR";
        }

        if($USEMYSQL) {  
            $qstring .= "
           	(
				lcase(newasset_page.title) $not regexp lcase('[[:<:]]($terms[$i])+')
			) ";
        } else {
            $qstring .= "
           	(
				newasset_page.title $not like '%$terms[$i]%'
			) ";
        }
        $qstring .= " $conj " if($i < $#terms);  
    }
    $st=$db->prepare("
                SELECT  item.sid,item.description,node.node,item.type,
                        treemap.node,newasset_conn.page
                FROM    item,newasset_conn,newasset_page,node,treemap
                WHERE   newasset_page.ID=newasset_conn.newasset_page
                AND     item.sid=newasset_conn.item_sid
                AND     item.node_sid=node.sid
                AND     item.published = 1
                AND     treemap.sid=node.sid
                AND     $meta
                AND     ( $qstring )
                ORDER BY    item.description
    ");

    $st->execute();
    while(@row=$st->fetchrow()) {
        $path = $row[4];
        $path =~ s/\ /%20/g;
        if(!$listed{$row[0]}) {
            if($row[3] eq "meta") {
                $metaand{$row[0]}++;
                $results{"$row[4]/$row[0]"} = "<img alt=\"$OMA::MEDIA_TYPE{$row[3]}\" src=\"http://$OMA::OMAHOST/img/$OMA::MEDIA_TYPE{$row[3]}.png\" /> <a class=\"clink\" href=\"http://$OMA::OMAHOST$OMA::BASE$path/$row[0].html\">$row[1]</a><br />\n";
                $path =~ s/\ /%20/g;
            }
            else {
                $results{"$row[4]/$row[0]"} = "<img alt=\"$OMA::MEDIA_TYPE{$row[3]}\" src=\"http://$OMA::OMAHOST/img/$OMA::MEDIA_TYPE{$row[3]}.png\" /> <a class=\"clink\" href=\"http://$OMA::OMAHOST$OMA::BASE$path/$row[0].html\">$row[1]</a><br />\n";
            }
            $listed{$row[0]} = 1;
        }
    }
    $st->finish();
}


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

sub global_reboot
{
    local $qstring = shift;
    local $conj = shift;
    local $meta = shift;
    local @terms;
    local $i;
    local %listed;
    local $not = "";
    local $conj2 = "OR";
    local $path;

#   print "<p></p>\n";
    @terms = qterms($qstring);
    $qstring="";
    for($i=0;$i<=$#terms;$i++) {
        if($terms[$i] =~ /^-/) {
            $not = " not ";
            $conj2 = "AND";
            $terms[$i] =~ s/^-//;
        } else {
            $not = "";
            $conj2 = "OR";
        }

        if($USEMYSQL) {  
            $qstring .= "
           	(
				lcase(reboot_page.dc__title) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(reboot_page.dc__title__alternative) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(reboot_page.dc__publisher) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(reboot_page.dc__description__toc) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(reboot_page.dc__description__abstract) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(reboot_page.dc__description__long) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(reboot_page.dc__type) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(reboot_page.dc__subject) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(reboot_page.dc__coverage__spatial) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(reboot_page.dc__coverage__temporal) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(reboot_creator.dc__creator) $not regexp lcase('[[:<:]]($terms[$i])+') $conj2
				lcase(reboot_contributor.dc__contributor) $not regexp lcase('[[:<:]]($terms[$i])+')
			) ";
        } else {
            $qstring .= "
           	(
                reboot_page.dc__title ilike '%$terms[$i]%' $conj2
                reboot_page.dc__title__alternative ilike '%$terms[$i]%' $conj2
                reboot_page.dc__publisher ilike '%$terms[$i]%' $conj2
                reboot_page.dc__identifier ilike '%$terms[$i]%' $conj2
                reboot_page.dc__description__toc ilike '%$terms[$i]%' $conj2
                reboot_page.dc__description__abstract ilike '%$terms[$i]%' $conj2
                reboot_page.dc__description__long ilike '%$terms[$i]%' $conj2
                reboot_page.dc__type ilike '%$terms[$i]%' $conj2
                reboot_page.dc__subject ilike '%$terms[$i]%' $conj2
                reboot_page.dc__coverage__spatial ilike '%$terms[$i]%' $conj2
                reboot_page.dc__coverage__temporal ilike '%$terms[$i]%' $conj2
                reboot_creator.dc__creator ilike '%$terms[$i]%' $conj2
                reboot_contributor.dc__contributor ilike '%$terms[$i]%'

			) ";
        }
        $qstring .= " $conj " if($i < $#terms);  
    }
    $st=$db->prepare("
                SELECT  item.sid,item.description,node.node,item.type,
                        treemap.node,reboot_conn.page,
						reboot_page.dc__description__abstract, reboot_page.dc__description__long
                FROM    item,reboot_conn,reboot_page,node,treemap,reboot_creator,reboot_contributor
                WHERE   reboot_page.ID=reboot_conn.reboot_page
				AND		reboot_page.location=reboot_conn.location
                AND		reboot_page.ID=reboot_creator.ID
				AND		reboot_page.location=reboot_creator.location
				AND		reboot_page.ID=reboot_contributor.ID
				AND		reboot_page.location=reboot_contributor.location
                AND     item.sid=reboot_conn.item_sid
                AND     item.node_sid=node.sid
                AND     item.published = 1
                AND     treemap.sid=node.sid
                AND     $meta
                AND     ( $qstring )
                ORDER BY    reboot_page.dc__title, item.description
    ");

    $st->execute();
    while(@row=$st->fetchrow()) {
        $path = $row[4];
        $path =~ s/\ /%20/g;
		my $ks = $in{'qstring'};
		$ks =~ s/\ /|/g;
#		$row[6] = substr($row[6],0,197) ."...<br /><a class=\"clink\" href=\"http://$OMA::OMAHOST$OMA::BASE$path/$row[0].html\">more</a>";

		$row[6] = markhits("$row[6] $row[7]", "($ks)");

		$row[6] .= "...<br /><a class=\"clink\" href=\"http://$OMA::OMAHOST$OMA::BASE$path/$row[0].html\">more</a>";

        if(!$listed{$row[0]}) {
            if($row[3] eq "meta") {
				my $t = $row[1];
				$t =~ s/\//-/g;
                $metaand{$row[0]}++;
                $results{"$t/$row[4]/$row[0]"} = "<img alt=\"$OMA::MEDIA_TYPE{$row[3]}\" src=\"http://$OMA::OMAHOST/img/$OMA::MEDIA_TYPE{$row[3]}.png\" /> <a class=\"clink\" href=\"http://$OMA::OMAHOST$OMA::BASE$path/$row[0].html\">$row[1]</a><br />$row[6]<br />\n";
                $path =~ s/\ /%20/g;
            }
            else {
				my $t = $row[1];
				$t =~ s/\//-/g;
                $results{"$t/$row[4]/$row[0]"} = "<img alt=\"$OMA::MEDIA_TYPE{$row[3]}\" src=\"http://$OMA::OMAHOST/img/$OMA::MEDIA_TYPE{$row[3]}.png\" /> <a class=\"clink\" href=\"http://$OMA::OMAHOST$OMA::BASE$path/$row[0].html\">$row[1]</a><br />$row[6]\n";
            }
            $listed{$row[0]} = 1;
        }
    }
    $st->finish();
}


sub splitpath {
	my $path = shift;
	my @dirs = split(/\//,$path);
	my $cum;
	my $i;

	$path = "<b class=\"thenavdiv\">";
	$cum="http://$OMA::OMAHOST$OMA::BASE";
	for($i=0;$i<=$#dirs;$i++) {
		$cum .= "$dirs[$i]/";
		$cum =~ s/ /%20/g;
		$path .= "<a class=\"navlinks\" href=\"$cum\">$dirs[$i]-&gt;</a>";
	}
	$path .= "</b>";
	return($path);
}


                   ##

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

local $check1, $check2;
local $results;
local $last;
local $head, $body, $bottom, $bodytmpl;
local $n=0;
local $numpages;
undef %results;

CGI::ReadParse();
$in{'conj'} = "AND";

$db = OMA::opendb() if(!$db);

$head = OMA::loadtempl(1,"search_head");
$head =~ s/OLDVALUE/$in{'qstring'}/;
$bodytmpl = OMA::loadtempl(1,"search_body");
#print "<!-- $head -->";
$bottom = OMA::loadtempl(1,"search_bottom");

$check1 = $check2 = "";
$check1 = " checked " if($in{'conj'} eq "OR");
$check2 = " checked " if($in{'conj'} eq "AND");


if($in{'qstring'}) {
	global_video($in{'qstring'},$in{'conj'},"1=1") if($in{'qstring'});
#	global_audio($in{'qstring'},$in{'conj'},"1=1") if($in{'qstring'});
	global_picture($in{'qstring'},$in{'conj'},"1=1") if($in{'qstring'});
	global_text($in{'qstring'},$in{'conj'},"1=1") if($in{'qstring'});
#	global_newasset($in{'qstring'},$in{'conj'},"1=1") if($in{'qstring'});
	global_reboot($in{'qstring'},$in{'conj'},"1=1") if($in{'qstring'});
}


local $last="";
$body="";
$numpages = (keys %results)/10;
ITER: {
	foreach $k (sort(keys(%results))) {
		if($n > $in{'n'}*10+10) {
			last ITER;
		}
		if($n >= $in{'n'}*10) {
			@patha=split(/\//,$k);
			$path="";
			for($i=1;$i<=$#patha-1;$i++) {
				$path .= "$patha[$i]/";
			}
			$path = splitpath($path);
			if($path ne $last) {
				$body .= parsetag($bodytmpl,$path,$results{$k});
			}
			else {
				$body .= parsetag($bodytmpl,"",$results{$k});
			}
			$last = $path;
		}
		$n++;
	}
}

#print $head;
#print $body;
#print $bottom;
if($head =~ /<!--#exec cgi="\/perl\/rembanner" -->/) {
	my $t="";
			
	my $s = Net::HTTP->new(Host => "news.reboot.fm") || die $@;
	$s->write_request(GET => "/rolux/head.php?logo=left&logolink=/", 'User-Agent' => "Mozilla/5.0");
	my($code, $mess, %h) = $s->read_response_headers;

	while (1) {
		my $buf;
		my $n = $s->read_entity_body($buf, 1024);
		die "read failed: $!" unless defined $n;
		last unless $n;
		$t .= $buf;
	}
	$head =~ s/<!--#exec cgi="\/perl\/rembanner" -->/$t/;
}

#$body =~ s/&/&amp;/g;
#$body =~ s/&amp;gt;/&gt;/g;
#print parsetag($head . $body . $bottom,"","",$numpages);
$str =  parsetag($head . "BOOOOODY" . $bottom,"","",$numpages);
$str =~ s/BOOOOODY/$body/g;

print $str;
