#!/usr/bin/perl -Imodules

use CGI;
use strict;
use Encode;
use Request::HTTPRequest;
use HTML::Headers;

my $CGI	= new CGI;
print $CGI->header();


# Parameter entgegennehmen
my $query	= $CGI->param("q");
my $engine	= $CGI->param("s");
my $type	= $CGI->param("a");


# Variabeln vordeklarieren
my $ArrayRefResults;
my $REF;


# wenn keine suchanfrage gestellt wurde
if ( $query eq '' ) {
	
	# Zeige Headerinformationen an und beende dich
	if ( $type eq 1 ) {
		HTML::Headers->GenerateAdvancedHeaders();
		print "</body></html>";
		exit(0);
	} else {
		HTML::Headers->GenerateHeaders();
		print "</body></html>";
		exit(0);
	};

} else {
	
	# Gib Headerinformationen aus und stelle dann die Suchresults da
	if ( $type eq 1 ) {
		HTML::Headers->GenerateAdvancedHeaders( $query );
	} else {
		HTML::Headers->GenerateHeaders( $query );
	};

	if ( $engine eq "google" ) {
		
		$ArrayRefResults = &QueryGoogle( $query );

	} elsif ( $engine eq "msn" ) {
		
		$ArrayRefResults = &QueryMSNSearch( $query );

	}  elsif ( $engine eq "alltheweb" ) {
					
		$ArrayRefResults = &QueryAllTheWeb( $query );

	}  elsif ( $engine eq "ask" ) {
	
		$ArrayRefResults = &QueryAskJeeves( $query );

	}  elsif ( $engine eq "gigablast" ) {
			
		$ArrayRefResults = &QueryGigablast( $query );

	}  elsif ( $engine eq "teoma" ) {
	
		$ArrayRefResults = &QueryTeoma( $query );

	}  elsif ( $engine eq "googlenews" ) {
			
		$ArrayRefResults = &QueryGoogleNews( $query );

	}  elsif ( $engine eq "yahoonews" ) {
	
		$ArrayRefResults = &QueryYahooNews( $query );

	}  elsif ( $engine eq "googleblogs" ) {
	
		$ArrayRefResults = &QueryGoogleBlogs( $query );

	}  elsif ( $engine eq "blogdigger" ) {
	
		$ArrayRefResults = &QueryBlogDigger( $query );

	}  elsif ( $engine eq "googlegroups" ) {
	
		$ArrayRefResults = &QueryGoogleGroups( $query );

	}  elsif ( $engine eq "lycosgroups" ) {
	
		$ArrayRefResults = &QueryLycosGroups( $query );

	# meta suchen
	}  elsif ( $engine eq "metaweb" ) {
		
		$ArrayRefResults = &QueryGoogle( $query );
		$REF = _getOnlyTenResults( $ArrayRefResults, $REF );
		$ArrayRefResults = &QueryMSNSearch( $query );
		$REF = _getOnlyTenResults( $ArrayRefResults, $REF );
		$ArrayRefResults = &QueryAllTheWeb( $query );
		$REF = _getOnlyTenResults( $ArrayRefResults, $REF );
		$ArrayRefResults = &QueryAskJeeves( $query );
		$REF = _getOnlyTenResults( $ArrayRefResults, $REF );

		sub _getOnlyTenResults(){
			
			my $ArrayResultRef	= shift;
			my $ArrayReturnRef	= shift;
			my $rcount = 0; 
			
			foreach my $ArrayRefResults ( @{$ArrayResultRef} ) {
				next if ( $rcount == 10 );
				my $url		= %{$ArrayRefResults}->{'URL'};
				my $title	= %{$ArrayRefResults}->{'TITLE'}; 
				my $engine	= %{$ArrayRefResults}->{'ENGINE'};
				my $desc	= %{$ArrayRefResults}->{'DESCRIPTION'}; 
				push(@{$ArrayReturnRef}, "$url#\t#$title#\t#$desc#\t#$engine\n");
				$rcount++;
			};
			return $ArrayReturnRef;
		};

		# speichere in rückgabe in datei für spätere bearbeitung und beende dich
		open(WH,">/tmp/CLUSTER.txt");
			print WH @{$REF};
		close WH;
		
	}  elsif ( $engine eq "metanews" ) {

	}  elsif ( $engine eq "metablogs" ) {
	
	}  elsif ( $engine eq "metagroups" ) {
	
	} else {
		# sonst führe metaweb suche durch
	};

};


exit(0);






sub test() {
	foreach my $HashRef ( @{$ArrayRefResults} ) {

		my $url		= %{$HashRef}->{'URL'};
		my $title	= %{$HashRef}->{'TITLE'}; 
		my $desc	= %{$HashRef}->{'DESCRIPTION'}; 
		my $engine	= %{$HashRef}->{'ENGINE'};

		print <<RESULTS;
			<div class="result">
				<span class="desc"><a href="$url" title="$title"> $title </a> </span> <p></p>
				<span class="descdet green"> Summary </span> 
					<span id="spacer"></span> 
					<span id="spacer"></span> 
					<span id="spacer"></span> 
						$desc
					 <br>
				<span style="color: #999999;"> Source: $engine </span>
				</div>
RESULTS

	};	# foreach my $HashRef ( @{$ArrayRefResults} ) {}
};


###################
################### Discussion Suchmaschinene
###################


sub QueryLycosGroups(){

	my $query = shift;

	use Suchmaschinen::Discussions::Lycos;

	my $ArrayRef = Suchmaschinen::Discussions::Lycos->_packQuery( $query );
	my $QueryUrl = @{$ArrayRef}[0];

	my %HttpRequestConfig = (
			'URL'			=> $QueryUrl,
			'TIMEOUT'		=> '30',
			'USERAGENT'		=> 'QueryServer 1.0',
			'MAXSIZE'		=> '500000',
			'COOKIEFILE'	=> 'cookies.txt',
			'AUTHUSER'		=> '',
			'AUTHPASS'		=> '',
			'PROXY'			=> '',
		);

	my $content		= Request::HTTPRequest->_get( \%HttpRequestConfig );
	my $ArrayRefResults = Suchmaschinen::Discussions::Lycos->GET_RESULTS( \$content );

	return $ArrayRefResults;

};


sub QueryGoogleGroups(){

	my $query = shift;

	use Suchmaschinen::Discussions::Google;

	my $ArrayRef = Suchmaschinen::Discussions::Google->_packQuery( $query );
	my $QueryUrl = @{$ArrayRef}[0];

	my %HttpRequestConfig = (
			'URL'			=> $QueryUrl,
			'TIMEOUT'		=> '30',
			'USERAGENT'		=> 'QueryServer 1.0',
			'MAXSIZE'		=> '500000',
			'COOKIEFILE'	=> 'cookies.txt',
			'AUTHUSER'		=> '',
			'AUTHPASS'		=> '',
			'PROXY'			=> '',
		);

	my $content		= Request::HTTPRequest->_get( \%HttpRequestConfig );
	my $ArrayRefResults = Suchmaschinen::Discussions::Google->GET_RESULTS( \$content );

	return $ArrayRefResults;

};


###################
################### Blog Suchmaschinene
###################

sub QueryGoogleBlogs(){

	my $query = shift;

	use Suchmaschinen::Blogs::Google;

	my $ArrayRef = Suchmaschinen::Blogs::Google->_packQuery( $query );
	my $QueryUrl = @{$ArrayRef}[0];

	my %HttpRequestConfig = (
			'URL'			=> $QueryUrl,
			'TIMEOUT'		=> '30',
			'USERAGENT'		=> 'QueryServer 1.0',
			'MAXSIZE'		=> '500000',
			'COOKIEFILE'	=> 'cookies.txt',
			'AUTHUSER'		=> '',
			'AUTHPASS'		=> '',
			'PROXY'			=> '',
		);

	my $content		= Request::HTTPRequest->_get( \%HttpRequestConfig );
	my $ArrayRefResults = Suchmaschinen::Blogs::Google->GET_RESULTS( \$content );

	return $ArrayRefResults;

};


sub QueryBlogDigger(){

	my $query = shift;

	use Suchmaschinen::Blogs::BlogDigger;

	my $ArrayRef = Suchmaschinen::Blogs::BlogDigger->_packQuery( $query );
	my $QueryUrl = @{$ArrayRef}[0];

	my %HttpRequestConfig = (
			'URL'			=> $QueryUrl,
			'TIMEOUT'		=> '30',
			'USERAGENT'		=> 'QueryServer 1.0',
			'MAXSIZE'		=> '500000',
			'COOKIEFILE'	=> 'cookies.txt',
			'AUTHUSER'		=> '',
			'AUTHPASS'		=> '',
			'PROXY'			=> '',
		);

	my $content		= Request::HTTPRequest->_get( \%HttpRequestConfig );
	my $ArrayRefResults = Suchmaschinen::Blogs::BlogDigger->GET_RESULTS( \$content );

	return $ArrayRefResults;

};


###################
################### News Suchmaschinene
###################


sub QueryYahooNews(){

	my $query = shift;

	use Suchmaschinen::News::Yahoo;

	my $ArrayRef = Suchmaschinen::News::Yahoo->_packQuery( $query );
	my $QueryUrl = @{$ArrayRef}[0];

	my %HttpRequestConfig = (
			'URL'			=> $QueryUrl,
			'TIMEOUT'		=> '30',
			'USERAGENT'		=> 'QueryServer 1.0',
			'MAXSIZE'		=> '500000',
			'COOKIEFILE'	=> 'cookies.txt',
			'AUTHUSER'		=> '',
			'AUTHPASS'		=> '',
			'PROXY'			=> '',
		);

	my $content		= Request::HTTPRequest->_get( \%HttpRequestConfig );
	my $ArrayRefResults = Suchmaschinen::News::Yahoo->GET_RESULTS( \$content );

	return $ArrayRefResults;

};


sub QueryGoogleNews(){

	my $query = shift;

	use Suchmaschinen::News::Google;

	my $ArrayRef = Suchmaschinen::News::Google->_packQuery( $query );
	my $QueryUrl = @{$ArrayRef}[0];

	my %HttpRequestConfig = (
			'URL'			=> $QueryUrl,
			'TIMEOUT'		=> '30',
			'USERAGENT'		=> 'QueryServer 1.0',
			'MAXSIZE'		=> '500000',
			'COOKIEFILE'	=> 'cookies.txt',
			'AUTHUSER'		=> '',
			'AUTHPASS'		=> '',
			'PROXY'			=> '',
		);

	my $content		= Request::HTTPRequest->_get( \%HttpRequestConfig );
	my $ArrayRefResults = Suchmaschinen::News::Google->GET_RESULTS( \$content );

	return $ArrayRefResults;

};


###################
################### Normale Suchmaschinene
###################


sub QueryTeoma(){

	my $query = shift;

	use Suchmaschinen::Web::Teoma;

	my $ArrayRef = Suchmaschinen::Web::Teoma->_packQuery( $query );
	my $QueryUrl = @{$ArrayRef}[0];

	my %HttpRequestConfig = (
			'URL'			=> $QueryUrl,
			'TIMEOUT'		=> '30',
			'USERAGENT'		=> 'QueryServer 1.0',
			'MAXSIZE'		=> '500000',
			'COOKIEFILE'	=> 'cookies.txt',
			'AUTHUSER'		=> '',
			'AUTHPASS'		=> '',
			'PROXY'			=> '',
		);

	my $content		= Request::HTTPRequest->_get( \%HttpRequestConfig );
	my $ArrayRefResults = Suchmaschinen::Web::Teoma->GET_RESULTS( \$content );

	return $ArrayRefResults;

};


sub QueryGigablast(){

	my $query = shift;

	use Suchmaschinen::Web::Gigablast;

	my $ArrayRef = Suchmaschinen::Web::Gigablast->_packQuery( $query );
	my $QueryUrl = @{$ArrayRef}[0];

	my %HttpRequestConfig = (
			'URL'			=> $QueryUrl,
			'TIMEOUT'		=> '30',
			'USERAGENT'		=> 'QueryServer 1.0',
			'MAXSIZE'		=> '500000',
			'COOKIEFILE'	=> 'cookies.txt',
			'AUTHUSER'		=> '',
			'AUTHPASS'		=> '',
			'PROXY'			=> '',
		);

	my $content		= Request::HTTPRequest->_get( \%HttpRequestConfig );
	my $ArrayRefResults = Suchmaschinen::Web::Gigablast->GET_RESULTS( \$content );

	return $ArrayRefResults;

};


sub QueryAskJeeves(){

	my $query = shift;

	use Suchmaschinen::Web::AskJeeves;

	my $ArrayRef = Suchmaschinen::Web::AskJeeves->_packQuery( $query );
	my $QueryUrl = @{$ArrayRef}[0];

	my %HttpRequestConfig = (
			'URL'			=> $QueryUrl,
			'TIMEOUT'		=> '30',
			'USERAGENT'		=> 'QueryServer 1.0',
			'MAXSIZE'		=> '500000',
			'COOKIEFILE'	=> 'cookies.txt',
			'AUTHUSER'		=> '',
			'AUTHPASS'		=> '',
			'PROXY'			=> '',
		);

	my $content		= Request::HTTPRequest->_get( \%HttpRequestConfig );
	my $ArrayRefResults = Suchmaschinen::Web::AskJeeves->GET_RESULTS( \$content );

	return $ArrayRefResults;

};


sub QueryAllTheWeb(){

	my $query = shift;

	use Suchmaschinen::Web::AllTheWeb;

	my $ArrayRef = Suchmaschinen::Web::AllTheWeb->_packQuery( $query );
	my $QueryUrl = @{$ArrayRef}[0];
	
	my %HttpRequestConfig = (
			'URL'			=> $QueryUrl,
			'TIMEOUT'		=> '30',
			'USERAGENT'		=> 'QueryServer 1.0',
			'MAXSIZE'		=> '500000',
			'COOKIEFILE'	=> 'cookies.txt',
			'AUTHUSER'		=> '',
			'AUTHPASS'		=> '',
			'PROXY'			=> '',
		);

	my $content		= Request::HTTPRequest->_get( \%HttpRequestConfig );
	my $ArrayRefResults = Suchmaschinen::Web::AllTheWeb->GET_RESULTS( \$content );

	return $ArrayRefResults;

};


sub QueryMSNSearch(){

	my $query = shift;

	use Suchmaschinen::Web::MSNSearch;

	my $ArrayRef = Suchmaschinen::Web::MSNSearch->_packQuery( $query );
	my $QueryUrl = @{$ArrayRef}[0];
	
	my %HttpRequestConfig = (
			'URL'			=> $QueryUrl,
			'TIMEOUT'		=> '30',
			'USERAGENT'		=> 'QueryServer 1.0',
			'MAXSIZE'		=> '500000',
			'COOKIEFILE'	=> 'cookies.txt',
			'AUTHUSER'		=> '',
			'AUTHPASS'		=> '',
			'PROXY'			=> '',
		);

	my $content		= Request::HTTPRequest->_get( \%HttpRequestConfig );
	my $ArrayRefResults = Suchmaschinen::Web::MSNSearch->GET_RESULTS( \$content );

	return $ArrayRefResults;

};


sub QueryGoogle(){

	my $query = shift;

	use Suchmaschinen::Web::Google;

	my $ArrayRef = Suchmaschinen::Web::Google->_packQuery( $query );
	my $QueryUrl = @{$ArrayRef}[0];

	my %HttpRequestConfig = (
			'URL'			=> $QueryUrl,
			'TIMEOUT'		=> '30',
			'USERAGENT'		=> 'QueryServer 1.0',
			'MAXSIZE'		=> '500000',
			'COOKIEFILE'	=> 'cookies.txt',
			'AUTHUSER'		=> '',
			'AUTHPASS'		=> '',
			'PROXY'			=> '',
		);

	my $content	= Request::HTTPRequest->_get( \%HttpRequestConfig );
	my $ArrayRefResults = Suchmaschinen::Web::Google->GET_RESULTS( \$content );
	return $ArrayRefResults;

};


#############
############# Additional Subroutines
#############

sub _Engine2EngineOut(){

	my $engine = shift;

	if ( $engine eq "google" ) {
		return "Google.de";
	} elsif ( $engine eq "msn" ) {
		return "Msn.de";
	}  elsif ( $engine eq "alltheweb" ) {
		return "AllTheWeb.de";
	}  elsif ( $engine eq "ask" ) {
		return "AskJeeves.de";
	}  elsif ( $engine eq "gigablast" ) {
		return "Gigablast.de";
	}elsif ( $engine eq "teoma" ) {
		return "Theoma.com";
	}  elsif ( $engine eq "googlenews" ) {
		return "Google.de News";
	}  elsif ( $engine eq "yahoonews" ) {
		return "Yahoo.com News";
	}  elsif ( $engine eq "googleblogs" ) {
		return "Google.de Blogs";
	}  elsif ( $engine eq "blogdigger" ) {
		return "BlogDigger.com";
	}  elsif ( $engine eq "googlegroups" ) {
		return "Google.de Groups";
	}  elsif ( $engine eq "lycosgroups" ) {
		return "Lycos.com Groups";
	# meta suchen
	}  elsif ( $engine eq "metaweb" ) {
		return "MetaSuche Web";
	}  elsif ( $engine eq "metanews" ) {
		return "MetaSuche News";
	}  elsif ( $engine eq "metablogs" ) {
		return "MetaSuche Blogs";
	}  elsif ( $engine eq "metagroups" ) {
		return "MetaSuche Groups";
	} else {
		return "MetaSuche";
	};

};


1;