#! /usr/bin/perl -w
# $Id: sigpue,v 1.18 2007/09/27 04:38:42 homer Exp $

use strict;
use Text::Wrap;

my %wordlists;

sub populatewordlist() {
    while (<>) {
        next if /^(?:\s*#)|(?:\\\\$)/;          # skip comment lines
        if (my ($key, $val) = /^([a-z0-9-]+)\s+(.*)$/) {
            push @{$wordlists{$key}}, $val;
        }
    }
}

sub main() {
    my $sentence = "%s ça pue, il faut utiliser %s à la place.\n";
    populatewordlist;

    my (@theme, @list, @nwords, @words, @ran);
    for my $n (0 .. 1) {
        $ran[$n] = int rand keys %wordlists;
        $theme[$n] = (keys %wordlists)[$ran[$n]];
        $list[$n] = $wordlists{$theme[$n]};
        $nwords[$n] = @{$list[$n]};
        $words[$n] = $list[$n]->[int rand $nwords[$n]];
        delete $wordlists{$theme[$n]};
    }

    print `cat ~/.signature`, "\n";

    $words[0] =~ s/\[(.*?)\|(.*?)\]/$1/g;
    $words[1] =~ s/\[(.*?)\|(.*?)\]/$2/g;
    print wrap('', '', sprintf($sentence, ucfirst $words[0], $words[1]));
}

main;
