#!/usr/bin/perl

# Last change: Mon Mar 26 15:14:43 CEST 2001 by <josv@rocklinux.org>

use LWP::UserAgent;
use HTTP::Request;
use Net::FTP;

$pkgfile="packages";
$pkgfile=$ARGV[0] if $#ARGV>=0;
print "Reading from package file $pkgfile\n";

@order=qw/nl uk se fi at de edu com org ca/;
$hits=500;

$|=1;

$ua = new LWP::UserAgent;
$ua->agent("getpkg/0.1 " . $ua->agent);

open(PKG,"<$pkgfile") or die;
@pkgs=<PKG>;
close(PKG);

pakketje: foreach $pkg (@pkgs) {
	chomp $pkg;
	print "Locating package: $pkg\n";

	if (-f $pkg) {
		print "  Package exists? (in current directory?)\n";
		print "  Skipping!\n";
		next;
	}

	#$url="http://ftpsearch.lycos.com/cgi-bin/search?form=lycosnet&type=Case+insensitive+multiple+substrings+search&filetype=All+files&query=$pkg&hits=$hits";
	$url="http://download.lycos.com/swbasic/BasicResults.asp?query=$pkg&first=0&recordcount=$hits&component=Other+Download+Sites";

	$req = new HTTP::Request GET => $url;
	print "  Retrieving site list...\n";
	$res = $ua->request($req);

	unless ($res->is_success) {
		warn "Lycos query failed";
		next;
	}

	split "\n", $res->content;
	$n=0;
	undef %loctable;

	foreach (@_) {
		next unless m,(ftp://[^/]+\.([^/]+)/.*/$pkg),;
		$loc=$1;
		$domain=$2;

		unless (defined $loctable{$domain}) {
			$loctable{$domain}=[];
		}

		$a=$loctable{$domain};
		push @$a,$loc;
		$n++;
	}

	print "  Found $n locations\n";

	foreach $dom (@order) {
		print "  Trying sites in the $dom domain...\n";

		$sites=$loctable{$dom};
		next unless defined $sites;

		foreach $loc (@$sites) {
			print "    $loc\n";

			$loc=~m,^ftp://([^/]+)/(.*)$,;
			$host=$1;
			$file=$2;

			$ftp = Net::FTP->new($host, Passive => 1);

			unless (defined $ftp) {
				print "    Connect failed! ($@)\n";
				next;
			} else {
				print "    Connect succeeded!\n";
			}

			if (!$ftp->login("ftp","josv\@osp.nl")) {
				print "    Login failed! ($@)\n";
				next;
			}

			$ftp->binary;

			print "    Retrieving file...\n";
			$start=time();

			if ($ftp->get($file)) {
				$lapsed=time()-$start;
				$size=(stat($pkg))[7]; 
				print "    Success!\n";
				print "    Downloaded $size bytes in $lapsed seconds";
				print " (is ",$size/$lapsed/1024," KB/sec)\n";
				$ftp->quit;
				next pakketje;
			} else {
				print "    Retrieval failed! ($@)\n";
			}
		}
	}
}

print "Done."
