#!/opt/perl/bin/perl
use strict;
use warnings;
use File::Copy;
my $sites;
my $ok = 2*60*60*24;
my $time = time;
open(my $state, 'mirmon.state') or die qq{Cannot open mirmon.state: $!};
while (my $line = <$state>) {
  chomp $line;
  my @a = split ' ', $line;
  my $diff = $time - $a[1];
  next unless ($ok - $diff > 0);
  $sites->{$a[0]}++;
}
close $state;
open(my $list, 'mirmon.list') or die qq{Cannot open mirmon.list: $!};
while (my $line = <$list>) {
  next if $line =~ /^#/;
  chomp $line;
  my @a = split ' ', $line;
  next if $a[0] =~ /partial/i;
  if (defined $sites->{$a[1]}) {
    $sites->{$a[1]} = $a[0];
  }
}
close $list;
$sites->{'ftp://ftp.dante.de/tex-archive/'} = 'de';
$sites->{'ftp://ftp.tex.ac.uk/tex-archive/'} = 'gb';
$sites->{'ftp://tug.ctan.org/tex-archive/'} = 'us';
open(my $txt, '>ctan.txt') or die qq{Cannot open ctan.txt: $!};
foreach my $key(keys %$sites) {
  (my $site = $key) =~ s/\/$//;
  my $cc = $sites->{$key};
  next unless $cc =~ /^[a-z]+$/;
  next if ($site =~ m{(http|ftp)://tug.ctan.org});
  print $txt "$site $cc\n";
}
close $txt;
if (-e '/home/randy/bin/ctan.txt') {
  copy('/home/randy/bin/ctan.txt', '/usr/local/share/GeoIP/ctan.txt')
    or warn "Cannot cp ctan.txt to /usr/local/share/GeoIP: $!";
}

