#!/usr/bin/perl

#use Net::SCP::Expect;
# perl -MCPAN -e 'install "Net::SCP::Expect"'
require "functions.pl";


while(1){

	system("perl /root/zoozle/new-bots/emule-newsbot/EmuleNewsCrawler.pl");
	system("perl /root/zoozle/new-bots/torrent-newsbot/TorrentNewsCrawler.pl");	# torrent.newsbot.pl
	system("perl /root/zoozle/new-bots-fr/TorrentCrawlerFR.pl");				# french content

	# newsarchive cachen
	# system("perl /root/zoozle/caching/NewsarchiveCachePage.pl");

	# &test_working();
	&CACHE();
	# &ftp_upload();	
	# &swarm_files();

	# sitemaps für heute erstellen
	system("perl /root/zoozle/sitemaps/SitemapToday.pl");

	# sitemaps aus den suchkeywords der user bilden 
	system("perl /root/zoozle/sitemaps/SitemapKeywordsToday.pl");
		
	system("date");	
	sleep 30200;

};


sub CACHE(){

	my $timeout	= "300";
	my $ua		= "Mozilla/5.0 (Windows; U; Windows NT 5.0; de-DE; rv:1.6) Gecko/20040206 Firefox/1.0.1";

	my %CACHING = (
		"/srv/server/wwwroot/index.html" => "http://www.zoozle.net/index.php",
	#	"/server/en.wwwroot/index.html" => "http://en.zoozle.net/index.php",
	#	"/srv/server/de.wwwroot/index.html" => "http://de.zoozle.net/index.php",

	#	"/server/apache2/htdocs/de/index.html" => "http://85.214.90.176/de/index_de.php",
	#	"/server/apache2/htdocs/org/index.html" => "http://85.214.90.176/org/index_org.php",
	#	"/server/apache2/htdocs/devu/index.html" => "http://85.214.90.176/devu/index_devu.php"
	);

	while( my ( $path, $url ) = each(%CACHING) ){
		my $page = &http_get($url, $timeout, $ua, );
		print "Generate Cache for $url\n";
		
		if ( length($page) >= 1000 ) {
		
			unlink $page;
			open(WH,">$path");
				print WH $page;
			close WH;

		}; # if ( length($page) >= 1000 ) {

	}; # while( my ( $path, $url ) = each(%CACHING) ){

	return 1;

}; # sub CACHE(){}


sub test_working() {

	# torrent check

	my $hashref = {};
	my $final	= {};

	$hashref->{'torrent.to'} = "de";
	$hashref->{'speedtorrent.to'} = "de";
	$hashref->{'bitreactor.to'} = "de";
	$hashref->{'bitbull.to'} = "de";
	$hashref->{'deluxebits.to'} = "de";
	$hashref->{'torrents-and-more.to'} = "de";

	$hashref->{'thepiratebay.org'} = "en";
	$hashref->{'demonoid.com'} = "en";
	$hashref->{'isohunt.com'} = "en";
	$hashref->{'metrotorrents.info'} = "en";
	$hashref->{'torrentbox.com'} = "en";
	$hashref->{'meganova.org'} = "en";
	$hashref->{'btjunkie.org'} = "en";
	$hashref->{'torrentreactor.to'} = "en";
	$hashref->{'mininova.org'} = "en";
	$hashref->{'snarf-it.org'} = "en";
	$hashref->{'seedler.org'} = "en";
	$hashref->{'mybittorrent.com'} = "en";
	
	open(RH, "<torrent_news.txt") or warn $!;

	while(<RH>){
		while ( my ($key, $value) = each(%{$hashref}) ) {
			#print "$key => $value\n";
			if ( $_ =~ /$key/i ) {
				$final->{$key}++;
			};
		};
	};
	close RH;

	my $result ;

	$now = localtime();
	chomp($now);
	$now =~ s/:/-/ig;
	$subject = " zoozle Bots Report for $now";

	open(WH,">status.txt");

	print WH "$subject\n\n";

	while ( my ($key, $value) = each(%{$final}) ) {	
		if ( $value >= 1 ) {
			$result .= "SUCCESS: Bittorrent Bot for $key is working correctly -> got $value results!\n";
			print WH "SUCCESS: Bittorrent  Bot for $key is working correctly -> got $value results!\n";
		} else {
			$result .= "ERROR: Bittorrent Bot for $key is not working\n";
			print WH "ERROR: Bittorrent Bot for $key is not working\n";
		};
	};
	close WH;

	# emule check 
	my $hashref = {};
	my $final	= {};

	$hashref->{'tvunderground.org.ru'} = "en";
	$hashref->{'saugstube-server.to'} = "de";
	$hashref->{'page.titanesel.ws'} = "de";
	$hashref->{'eselfilme.com'} = "de";
	$hashref->{'goldesel.to'} = "de";
	$hashref->{'fucktheinter.net'} = "en";

	open(RH, "<emule_news.txt") or warn $!;

	while(<RH>){
		while ( my ($key, $value) = each(%{$hashref}) ) {
			#print "$key => $value\n";
			if ( $_ =~ /$key/i ) {
				$final->{$key}++;
			};
		};
	};
	close RH;

	my $result ;

	open(WH,"+>>status.txt");

	while ( my ($key, $value) = each(%{$final}) ) {	
		if ( $value >= 1 ) {
			$result .= "SUCCESS: eMule Bot for $key is working correctly -> got $value results!\n";
			print WH "SUCCESS: eMule Bot for $key is working correctly -> got $value results!\n";
		} else {
			$result .= "ERROR: eMule Bot for $key is not working\n";
			print WH "ERROR: eMule Bot for $key is not working\n";
		};
	};
	close WH;

	system("/bin/cp status.txt /root/web/");
	system("cat status.txt | mail thecerial\@gmail.com");
	system("postqueue -f");
	system("/usr/sbin/postfix flush");

}; # sub


























####################################
######## too old & obsolate functions
####################################



sub ftp_upload(){

	use Net::FTP;
	use LWP::Simple;

	my $StoreURLAs	= "/root/zoozle/bots/index.html";
	my $GetFromURL	= "http://www.zoozle.net/zoozle.org/index_org.php";
	my $doc			= get($GetFromURL);

	open(WH,">$StoreURLAs");
	print WH $doc;
	close WH;

	my $FTP = Net::FTP->new("zoozle.org", Debug => 1) or warn "zoozle.org: $@";

	$FTP->login("u36432067","dvyGd8bn") or warn "Cannot login ", $FTP->message;
	$FTP->cwd("/") or warn "Cannot change working directory ", $FTP->message;
	$FTP->put($StoreURLAs, "index.html"); 
	$FTP->quit;

	unlink $StoreURLAs;

	my $StoreURLAs	= "/root/zoozle/bots/index.html";
	my $GetFromURL	= "http://www.zoozle.net/p2pworld.org/p2pworld.php";
	my $doc			= get($GetFromURL);

	open(WH,">$StoreURLAs");
	print WH $doc;
	close WH;

	my $FTP = Net::FTP->new("p2pworld.org", Debug => 1) or warn "p2pworld.org: $@";

	$FTP->login("v130366","a6z7dnwn") or warn "Cannot login ", $FTP->message;
	$FTP->cwd("/") or warn "Cannot change working directory ", $FTP->message;
	$FTP->put($StoreURLAs, "index.html"); 
	$FTP->quit;

	my $StoreURLAs	= "/root/zoozle/bots/index.html";
	my $GetFromURL	= "http://www.zoozle.net/bittorrent-search-engines.com/index_bts.php";
	my $doc			= get($GetFromURL);

	open(WH,">$StoreURLAs");
	print WH $doc;
	close WH;

	my $FTP = Net::FTP->new("bittorrent-search-engines.com", Debug => 1) or die "zoozle.org: $@";

	$FTP->login("u41545054","qCacFNsE") or die "Cannot login ", $FTP->message;
	$FTP->cwd("/") or die "Cannot change working directory ", $FTP->message;
	$FTP->put($StoreURLAs, "index.html"); 
	$FTP->quit;

	print "Done - FTP Upload\n";

}; # sub ftp_upload(){



sub swarm_files(){

	my %ConfigHash =(
		"1"	=> my $CfG1 = { 
				"HOST"	=> "85.214.66.15",	# fishwarez
				"USER"	=> "root",
				"PASS"	=> "######################!1962",
				"PATH"	=> "/root/zoozle/updates/"
				},
		#	"2"	=> my $CfG2 = { 
		#			"HOST"	=> "HOSTIP",
		#			"USER"	=> "USERNAME",
		#			"PASS"	=> "PASSWORD",
		#			"PATH"	=> "/root/.mutella"	 # wo soll es gespeichert werden
		#			}, 
	);

	# von A nach B kopieren
	while ( my ($key, $value) = each(%ConfigHash) ) {

		my $RemoteUserName	= $value->{ 'USER' };
		my $RemotePassword	= $value->{ 'PASS' };
		my $RemoteHostname	= $value->{ 'HOST' };
		my $RemoteStorePath	= $value->{ 'PATH' };

		# print "SCP: $RemoteUserName $RemotePassword $RemoteHostname $RemoteStorePath\n";

		my $SCP = Net::SCP::Expect->new(
			host		=> "$RemoteHostname",
			user		=> "$RemoteUserName",
			password	=> "$RemotePassword" );

		$SCP->auto_yes(1);
		
		eval {
			$SCP->scp("/root/zoozle/bots/emule_news.txt","$RemoteUserName\@$RemoteHostname:$RemoteStorePath");
			$SCP->scp("/root/zoozle/bots/torrent_news.txt","$RemoteUserName\@$RemoteHostname:$RemoteStorePath");
			undef $SCP;
		};

	}; # while (my ($key, $value) = each(%ConfigHash) ) {}
	
	$now_string = localtime;
	print "$now_string: I swarmed the files to slaves\n";

}; # sub swarm_files(){
