#! /usr/bin/perl

use Catacomb qw(:random);
use Getopt::Long qw(:config gnu_getopt);

$help = 0;
$dict = undef;
GetOptions("dict=s" => \$dict, "help" => \$help) or exit 1;

DICT: {
  last DICT if defined $dict;
  $dict = $ENV{"DICT"}; last DICT if defined $dict;
  $dict = "/usr/share/dict/words"; last DICT if -r $dict;
  -r $dict or $dict = "/usr/dict/words"; last DICT if -r $dict;
  die "no appropriate word list";
}
open DICT, "$dict" or die "can't open $dict: $!";

sub filter { 1; }
$filter = shift();
if (defined $filter) {
  eval "sub filter { $filter };";
  die $@ if $@;
}

$rng = Catacomb::Rand::Counter->new("blowfish", $random->fill(32));

$word = undef;
$i = 1;
WORD: while (<DICT>) {
  chomp();
  next WORD unless filter;
  $rng->range($i) == 0 and $word = $_;
  $i++;
}
close DICT;
print "$word\n";

