#!/usr/local/bin/perl -s

# $Source: /afs/gza.com/development/mail-server/RCS/count-votes.pl,v $
# $Id: count-votes.pl,v 1.2 1993/11/05 14:16:23 jik Exp $

if (0) {
}
#elsif ($rpfad) {
#    $vote_regexp = "rec\\.pictures\\.fine-art\\.d (yes|no)";
#}
#elsif ($ccr) {
#    $vote_regexp = "comp\\.compression\\.research (yes|no)";
#}
#elsif ($sip) {
#    $vote_regexp = "sci\\.image\\.processing (yes|no)";
#}
#elsif ($rfs) {
#    $vote_regexp = "rec\\.food\\.sourdough (yes|no)";
#}
#elsif ($rfh) {
#    $vote_regexp = "rec\\.food\\.historic (yes|no)";
#}
#elsif ($smn) {
#    $vote_regexp = "sci\\.med\\.nutrition (yes|no)";
#}
#elsif ($smo) {
#    $vote_regexp = "sci\\.med\\.occupational (yes|no)";
#}
#elsif ($scua) {
#    $vote_regexp = "soc\\.culture\\.usa\\.appalachian (yes|no)";
#}
#elsif ($rms) {
#    $vote_regexp = "rec\\.models\\.scale (yes|no)";
#}
#elsif ($rmcg) {
#    $vote_regexp = "rec\\.music\\.classical\\.guitar (yes|no|abstain)";
#}
#elsif ($tpm) {
#    $vote_regexp = "talk\\.politics\\.medicine (yes|no|abstain)";
#}
#elsif ($svwa) {
#    $vote_regexp = "sci\\.virtual-worlds\\.apps (yes|no|abstain)";
#}
#elsif ($a1) {
#    $vote_regexp = "comp\\.answers (yes|no|abstain)";
#}
#elsif ($a2) {
#    $vote_regexp = "misc\\.answers (yes|no|abstain)";
#}
#elsif ($a3) {
#    $vote_regexp = "rec\\.answers (yes|no|abstain)";
#}
#elsif ($a4) {
#    $vote_regexp = "sci\\.answers (yes|no|abstain)";
#}
#elsif ($a5) {
#    $vote_regexp = "soc\\.answers (yes|no|abstain)";
#}
#elsif ($a6) {
#    $vote_regexp = "talk\\.answers (yes|no|abstain)";
#}
#elsif ($rgm) {
#    $vote_regexp = "rec\\.games\\.miniatures (yes|no|abstain)";
#}
#else {
#    $vote_regexp = "comp\\.os\\.msdos\\.mail-news (yes|no|abstain)";
#}
#else {
#    $vote_regexp = "soc\\.religion\\.taize (yes|no|abstain)";
#}
else {
    $vote_regexp = "talk\\.politics\\.crypto (yes|no|abstain)";
}

die "Usage: $0 [ -tally ] [ -verbose ] [ -syslog ]\n" if ((@ARGV > 0) || !$vote_regexp);

%votes = ();
%votecount = ();
@votecount = ();
$votes = 0;

if ($syslog) {
    $prefix = ": good vote ";
}
else {
    $prefix = "";
}

while (<>) {
# The " *" at the end of the line is necessary because the losing
# DECstation /etc/syslog turns newlines at the end of logged messages into
# spaces, instead of deleting them.  If this ever gets fixed, it can be
# removed, although it certainly doesn't do any harm.
    if (/$prefix(.*) \/\/ $vote_regexp *$/io) {
	$address = $1;
	$thevote = $2;
	$thevote =~ tr/A-Z/a-z/;
	if ($thevote eq "abstain") {
	    if (defined($votes{$address})) {
		delete $votes{$address};
		$votes--;
	    }
	}
	else {
	    $votes++ if (! defined($votes{$address}));
	    $votes{$address} = $thevote;
	}
    }
    else {
	if ($verbose) {
	    chop;
	    warn "Bad voting line \"$_\".\n";
	}
    }
}

print "Voters ($votes):\n\n" if (! $tally);

foreach $name (&address_sort(keys(%votes))) {
    print "$name\n" if (! $tally);
    $votecount{$votes{$name}}++;
}

foreach $vote (keys(%votecount)) {
    $str = sprintf("%3d %s", $votecount{$vote}, $vote);
    push(@votecount, $str);
}

if (! $tally) {
    print "\nVotes:\n\n";

    foreach $vote (reverse sort(@votecount)) {
	$vote =~ s/^ *([^ ]+) ([^ ]+)$/$2 $1/;
	print "$vote\n";
    }
}
else {
    foreach $vote (reverse sort keys(%votecount)) {
	print "\"$vote\" votes ($votecount{$vote}):\n\n";
	foreach $name (&address_sort(keys(%votes))) {
	    $name =~ s/.*\n//;
	    print "$name\n" if ($votes{$name} eq $vote);
	}
	print "\n";
    }
}

sub address_sort {
    local(@sorted_votes);
    local($name, $address, $i);
    foreach $name (@_) {
	$address = &address_of($name);
	$address =~ tr/A-Z/a-z/;
	push(@sorted_votes, sprintf("%s\n%s", $address, $name));
    }
    @sorted_votes = sort @sorted_votes;
    foreach $i (0..$#sorted_votes) {
	$sorted_votes[$i] =~ s/.*\n//;
    }
    @sorted_votes;
}

sub address_of {
    local($_) = @_;
    
    if (/^.*\<(.*@.*)\>$/) {
	&rotate($1);
    }
    elsif (/^(.*[^ \t])[ \t]*\(.*\)$/) {
	&rotate($1);
    }
    else {
	$_;
    }
}

sub rotate {
    local($_) = $_[0];

    while (s/(.*!|^)([A-Za-z_]+)!([A-Za-z_]+)/$1$3%$2/) {}

    $_;
}
