#!/usr/bin/perl
use Net::FTP;
use WWW::Mechanize;
use Template;
use Data::Dumper;

use strict;
use warnings;
#unbuffer stdout
$|=1;
my $rel;
open (FH,"</usr/src/sys/conf/newvers.sh") || die "brotch:$@";
while (<FH>) {
	if ($_ =~ /osr="(\d)\.(\d)"/) {$rel=$1.$2; last;}
}
close FH;
print "Release=$rel..\n";
#grep osr= /usr/src/sys/conf/newvers.sh
my %urls;
my %last;
my $lastp = "zoom-1.1.2";
my $lastp2 = "aamath-0.3p0";
my @plats = ('i386', 'amd64', 'sparc64','alpha','macppc','hppa', 'sparc');
my $src = "http://www.openbsd.org/ftp.html";
my $mech = WWW::Mechanize->new;
print "Getting $src..\n";
$mech->get($src);
print "Extracting links..\n";
my @ftp = $mech->find_all_links(url_regex=>qr/ftp:\/\/.*/);
print "Gathering data..\n";
foreach (@ftp)
{
	my $url = $_->url;
	$url =~ s#ftp://##;
	my ($host, $cwd) = split m#/#,$url, 2;
	#print "Host=$host, cwd=$cwd\n";
	print ".";
	$urls{$host}=$_->url."snapshots/";
	my $cnx = Net::FTP->new($host);
	if (!$cnx)
	{
		print "\nCannot connect to $host : $@\n";
		$last{$host}{i386}{base} = -1;
		next
	}
	if (! $cnx->login('anonymous','anonymous@example.com'))
	{
		print "\nCannot login to $host : ", $cnx->message, "\n";
		$last{$host}{i386}{base} = -2;
		next
	}

	foreach (@plats)
	{
		my $parch = ($_ eq 'macppc' ? 'powerpc' : $_);
		$last{$host}{$_}{base} = $cnx->mdtm($cwd."/snapshots/$_/base$rel.tgz");
		$last{$host}{$_}{xenocara} = $cnx->mdtm($cwd."/snapshots/$_/xbase$rel.tgz");
		$last{$host}{$_}{packages} = $cnx->mdtm($cwd."/snapshots/packages/$parch/$lastp.tgz");
		$last{$host}{$_}{packages} = $cnx->mdtm($cwd."/snapshots/packages/$parch/$lastp2.tgz") if (!defined $last{$host}{$_}{packages});
	}
	$last{$host}{i386}{base} = 0 if (!defined $last{$host}{i386}{base});
	$cnx->quit;
}

# magic sort
print "\nSorting mirrors by freshness..\n";
my @sorted = sort {$last{$b}{i386}{base} <=> $last{$a}{i386}{base}} keys %last;

my $vars = {
	urls => \%urls,
	sorted => \@sorted,
	archs => \@plats,
	what => ["base","xenocara","packages"],
	data => \%last
};

my $template = Template->new();

$template->process("up2date.tt", $vars, "up2date.html") || die "Template process failed: ", $template->error(), "\n";
