#!/usr/bin/perl

# Randomly generates names by mixing up the first/last names of Debian
# developers. First argument is the number of names to output.

use strict;
use utf8;
use LWP::UserAgent;

my $count = 1;
$count = $ARGV[0] if $ARGV[0];

my $ua = LWP::UserAgent->new;

my $url = "http://www.us.debian.org/devel/people";

my $localcache = "$ENV{HOME}/.random_name.cache";

unless( -f $localcache ){
  $ua->mirror( $url, $localcache );
}

my @firsts = ();
my @lasts = ();

open NAMES, "<", $localcache or die "cannot read name cache\n";

while ( my $line = <NAMES> ) {
  if ( $line =~ /<dt><strong>\<a name=\"[^\"]+\"\>([^\<]+)\<\/a\>([^\<]+)\<\/strong\>/ism ) {
    my $name = html_ent2char($1.$2);

    my ($last, $first) = $name =~ /^([^,]+),\s+(.+)$/;
    push @firsts, $first;
    push @lasts, $last;
  }
}

close NAMES;

for( my $i = 0; $i < $count; $i++){
  print $firsts[int rand $#firsts], " ", $lasts[int rand $#lasts], "\n";
}

sub html_ent2char{
  my( $txt ) = @_;

  $txt =~ s/\&\#(\d+)\;/chr($1)/eg;

  return $txt;
}
