#!/usr/bin/perl 
#
# debian-mirrors    measure ping time to all mirrors in README.mirrors.txt
#
# downloads README.mirrors.txt from ftp.debian.org (but not if a local file
# is pointed to), runs fping on all Debian mirrors, sorts the result by ping 
# time and output the 'n' (default is 20) fastest mirrors.
#
# Written by Dirk Eddelbuettel <edd@debian.org> and released under the GPL
# $Id: debian-mirrors.pl,v 1.7 1999/02/22 00:07:55 edd Exp $

## Yes, Net::FTP is much cooler, but we don't want to depend on libnet-perl
## 'ftp.pl' is provided by the mirror package
unshift( @INC, "/usr/lib/mirror" );
require 'ftp.pl';

#use strict;			# doesn't work with ftp.pl
use English;
use File::Basename;
use vars qw($opt_h $opt_f $opt_v);
use Getopt::Std;
use IPC::Open2;

my $tempfile = "/tmp/".$file."-".$$;
my $readfrom = $tempfile;
my $max = 20;
$PROGRAM_NAME =~ s|.*/||;	# strip everything before last slash

getopts('hf:n:v') or die("Try `$PROGRAM_NAME -h` for help screen.\n");

if ($opt_h or $#ARGV != -1) {
    print "Usage:\n  $PROGRAM_NAME [options]\n";
    print "Options:\n";
    print "  -f file\tpoint to local version README.mirrors.txt\n";
    print "  -n max\tmaximum number of mirrors to show\n";
    print "  -v\t\tverbose operation\n";
    print "  -h\t\tshow this help\n";
    exit 0;
}

$max = $opt_n if ($opt_n);
print "Max is set to $max\n" if $opt_v;

if ( ! $opt_f ) {
    my $hostname = "ftp.debian.org";
    my $account = "ftp";
    my $password = "user\@debian.org";
    my $directory = "/pub/debian";
    my $filename = "README.mirrors.txt";
    print "Retrieving ", $filename, " for analysis.\n";
    ftp::debug(1) if $opt_v;	# for debugging output
    ftp::open($hostname,21,0,1) or die "No ftp connection\n";   
    ftp::login($account, $password) or die "Couldn't login\n";
    ftp::cwd($directory) or die "Cannot cd to $directory\n";
    ftp::get($filename, $tempfile, 0) or die "Could not get $file\n";
    ftp::quit();
} else {
    die "File $opt_f does not exist.\n" unless -f $opt_f;
    $readfrom = $opt_f;
}

print "Measuring ping times to all Debian mirror sites. " if $opt_v;
print "Please be patient.\n" if $opt_v;
    
open(DATA, $readfrom) or die "Cannot open $filename\n";

open2("PINGOUT", "PINGIN", "fping -ae") or die "Cannot start fping(1)\n";

my ($mirror,$time,$state);
$state = 0;
while (<DATA>) {		# parse the file for ftp sites 
  next if (m/^($| |-)/);	# skip empty lines and such

  ## the following is really crude code, and my only excuse is that the 
  ## README.mirrors.txt isn't quite as normalised as I'd like it to be ...

  if (m/Primary ISO Mirror Sites/) {
    $state = 1;
  } elsif (m/Secondary FTP and HTTP mirrors of the Debian archive/) {
    $state = 2;
  } elsif ($state ne 0) {
    if ($state eq 1) {
      ($mirror) = ($ARG =~ m|.* - *(\S*).*(Yes\|No).*(Yes\|No)|);
    } elsif ($state eq 2 and m/(d|D)ebian/) {
      ($mirror) = ($ARG =~ m|(^\S*\.\S*)\s*\S|);
    }
    print "Pinging $mirror \n" if $opt_v;
    print PINGIN "$mirror\n"; 
  }
}
close(DATA);
close(PINGIN);

open2("SORTOUT", "SORTIN", "sort -n") or die "Cannot start sort(1)\n";

while (<PINGOUT>) {
    ($mirror,$time) = ($ARG =~ m|^(\S*)\s*\((\d*) ms(ec)?\)|);
    print "fping reports $time for $mirror\n" if $opt_v;
    print SORTIN "$time $mirror\n";
}
close(SORTIN);
close(PINGOUT);

my $i=1;
while (<SORTOUT>)   {
    print "$_"; 
    last if ($i++ == $max);
}
close(SORTOUT);



