#!/usr/bin/perl

require "functions.pl";


while(1){

	my $esel = "/root/zoozle/updates/emule_news.txt";
	my $torr = "/root/zoozle/updates/torrent_news.txt";

	if ( -e $torr && -e $esel) {
		$done = 1;
	};


	InstallToDatabase($esel) if ( -e $esel );
	rename $esel => "/root/zoozle/updates/done/emule_news.txt";

	InstallToDatabase($torr) if ( -e $torr );
	rename $torr => "/root/zoozle/updates/done/torrent_news.txt";

	$now_string = localtime;
	print "Finished Installing at $now_string\n" if ($done == 1);

	$done = 0;
	sleep 3600;

};


sub InstallToDatabase(){

	my $file = shift;

	use DBI();

	$drh = DBI->install_driver("mysql");

	my $DBHOST		= "localhost";
	my $DBNAME		= "zoozle";
	my $DBUSER		= "zoozle";	
	my $DBPASS		= "zoozle23!99";	

	my $dbh = DBI->connect("DBI:mysql:database=$DBNAME;host=$DBHOST", "$DBUSER", "$DBPASS", {'RaiseError' => 0});

	&add_content_toSQLDB($file, $dbh);
	&add_content_toREALSQLDB($file, $dbh);

	$dbh->disconnect;

}; # sub InstallToDatabase(){




sub add_content_toSQLDB() {

	my $filename	= shift;
	my $dbh			= shift;
	my $date		= &getDATE();

	open(RH1,"<$filename") or die "$! - $filename \n";

	foreach (<RH1>) {
		
		my ( $cat, $desc, $link ) = split('#', $_);
		$link =~ s/LINK=//;
		$desc =~ s/DESC=//;
		$cat =~ s/CATE=//;
		
		next if ( $_ !~ /^CATE=/ );
		next if ( $_ =~ /Torrent does not exist/ig );
		next if ( $link =~ /megawerbung/);
		
		$cat	= &deleteSpecialChars($cat);
		$desc	= &deleteSpecialChars($desc);
		
		$desc =~ s/&+(\w)+;/ /i;
		$desc =~ s/&#+(\w)+;/ /i;
		$desc =~ s/&+(\d)+;/ /i;
		$desc =~ s/&#+(\d)+;/ /i;

		$count++;

		chomp($cat, $desc, $link);
				
		$dbh->do( 
			qq {
				INSERT DELAYED INTO `emule_news3` ( `UNID`, `DATE` , `CATG` , `DESC` , `LINK` )
					VALUES (
					'', '$date', '$cat', '$desc', '$link' );
			});

		$dbh->do( 
			qq {
				INSERT DELAYED INTO `emule_news7` ( `DATE` , `CATG` , `DESC` , `LINK` )
					VALUES (
						'$date', '$cat', '$desc', '$link' );
			});

	};# foreach(){}

	print "Done reading $filename with $count Entries\n";
	close RH1;

};

sub add_content_toREALSQLDB() {

	my $filename	= shift;
	my $dbh			= shift;
	my $date		= &getDATE();

	open(RH1,"<$filename") or die "$! - $filename \n";

	foreach (<RH1>) {
		
		my ( $cat, $desc, $link ) = split('#', $_);
		$link =~ s/LINK=//;
		$desc =~ s/DESC=//;
		$cat =~ s/CATE=//;
		
		next if ( $_ !~ /^CATE=/ );
		next if ( $_ =~ /Torrent does not exist/ig );
		next if ( $link =~ /megawerbung/);
		
		$cat	= &deleteSpecialChars($cat);
		$desc	= &deleteSpecialChars($desc);
		$desc =~ s/&+(\w)+;/ /i;
		$desc =~ s/&#+(\w)+;/ /i;
		$desc =~ s/&+(\d)+;/ /i;
		$desc =~ s/&#+(\d)+;/ /i;
		
		$count++;

		chomp($cat, $desc, $link);
				
		$dbh->do( 
			qq {
				INSERT DELAYED  INTO `emule7` (`DATE` , `CATG` , `DESC` , `LINK` )
					VALUES (
						'$date', '$cat', '$desc', '$link' );
			});
		
	};# foreach(){}

	print "Done reading $filename with $count Entries\n";
	close RH1;

};


sub getDATE() {

	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
	$year = $year+1900;
	$mon = $mon + 1;

	if ( length($mon) == 1 ) {
		$mon = "0". $mon;
	};
	if ( length($mday) == 1 ) {
		$mday = "0". $mday;
	};

	my $date = $year ."-". $mon ."-". $mday;

	return $date;

};