#!/usr/bin/perl

use DBI();
use Socket;
use WWW::Google::PageRank;
use Getopt::Long;
use Config;
use threads;
use threads::shared;

my $pr = WWW::Google::PageRank->new;
my $dbh = DBI->connect(
	"DBI:mysql:database=seo;host=localhost",
	"seoman",
	"3seA86uD",
	{'RaiseError' => 1}
);

my $values : shared = "";

&GetOptions(
	"in=s" => \$inList
);

sub worker {
	my ($user, $pass, $domain) = @_;

	if($domain =~ /\b(?:\d{1,3}\.){3}\d{1,3}\b/) {
		$temp = inet_aton($temp);
		$temp = gethostbyaddr($temp, AF_INET);
		
		if($temp ne "") {
			$domain = $temp;
		}
	}

	my $rank = $pr->get("http://$domain");

	{
		lock($values);
		$values .= ", " if $values;
		$values .= "('$user', '$pass', '$domain', '$rank')";
	}
}


$Config{useithreads} or die "Recompile Perl with threads to run this program.\n";

open FTPLIST, "<" . $inList;

while() {

	/(.+):(.+)@(.+)/;
	
	my @t = threads->list; my $thr;
	if ($#t < 30) {
		$thr = threads->new(\&worker, $1, $2, $3);
	} else {
		# Loop through all the threads
		foreach (threads->list) {
			# Don't join the main thread
			if ($_->tid && !threads::equal($_, threads->self)) {
				$_->join;
				last;
			}
		}

		{
			lock($values);
			$dbh->do("INSERT INTO ftp_servers $values");
			$values = "";
		}

		$thr = threads->new(\&worker, $1, $2, $3);
	}
}

# Loop through all the threads
foreach (threads->list) {
	# Don't join the main thread
	$_->join if $_->tid
}

$dbh->do("INSERT INTO ftp_servers $values");

close FTPLIST;
$dbh->disconnect();