#!/usr/bin/perl -w

use URI::Escape;
use XML::Simple;
use Storable;
use strict;
use bytes;

open (CF, "<config");
my %conf; while(<CF>) { /^(\S+)=(\S+)/ and $conf{$1} = $2; }
close CF;

our $USERNAME = $conf{BZ_USERNAME};
our $BZ_PASSWORD_FILE = $conf{BZ_PASSWORD_FILE};
our $SEND_DATA = 1;

our $boilerplate_header = "# [automatically generated by automc: start]\n";
our $boilerplate_footer = "# [automatically generated by automc: end]\n";
our %freqs_file = (
    'DETAILS.new' => $conf{html}.'/DETAILS.new',
);


my $outputs = retrieve($conf{MCTMP}."/outputs.str");


my %freqs_hdr = ();
my %freqs = ();
foreach my $key (sort keys %freqs_file) {
  my $file = $freqs_file{$key};
  open (IN, "<$file") or die "cannot read $file";

  $freqs_hdr{$key}=<IN>;
  %{$freqs{$key}} = ();

  while (<IN>) {
    if (/(?: \(all messages| results used:)/) {
      $freqs_hdr{$key} .= $_;
    }
    elsif (/\s+(\S+)$/) {
      $freqs{$key}{$1} ||= '';
      $freqs{$key}{$1} .= $_;
    }
  }
  close IN;
}

open (IN, "<$BZ_PASSWORD_FILE");
my $password = <IN>; chomp $password;
close IN;

use WWW::Mechanize;
my $mech = WWW::Mechanize->new( autocheck => 1);
# use WWW::Mechanize::Cached;
# my $mech = WWW::Mechanize::Cached->new( autocheck => 1);

main();
exit;

sub main {
  # login
  $mech->get (
  
'http://issues.apache.org/SpamAssassin/query.cgi?Bugzilla_login='.$USERNAME.'&Bugzilla_password='.$password.'&GoAheadAndLogIn=1'

    );

  my @bugs = @{$outputs->{allbugs}};

  foreach my $bug (@bugs) {
    do_bug ($bug);
  }
}

sub do_bug {
  my ($bug) = @_;

  my $comment = "";
  my @trigger_cmts = @{$outputs->{$bug}{trigger_cmts}};
  my @rulenames = @{$outputs->{$bug}{rulenames}};
  my $messages = $outputs->{messages}{$bug};

  $comment .= $boilerplate_header;

  foreach my $cmtnum (@trigger_cmts) {
    $comment .= "# DONEMC $cmtnum: completed request from comment $cmtnum\n";
  }

  use POSIX qw(strftime);
  my $date = strftime("%Y%m%d", localtime);

  foreach my $key (sort keys %freqs_file) {
    my $file = $freqs_file{$key};

    $comment .= "\n";

    # keep the comment in two buffers; one for the top part (freqs),
    # one for the lower (metadata and links)
    my $cmt2 = "above freqs using data from \"$file\" as of ".
                                    (scalar localtime time).":\n";

    foreach my $rule (@rulenames) {
      my $renamedata = $outputs->{rule_renames}{$rule};

      # if (!defined $renamedata) {
      # foreach my $oldname (keys %{$outputs->{rule_renames}}) {
      # if ($outputs->{rule_renames}{$oldname} =~ /rule \Q$rule\E /) {
      # $renamedata = $outputs->{rule_renames}{$oldname};
      # } } }

      $renamedata =~ /^rule (\S+) bug (\S+) cmt (\S+)/;

      my $oldname = $1;
      my $bugnum = $2;
      my $cmtnum = $3;

      $cmt2 .= "\n$rule = $oldname from bug $bugnum comment $cmtnum\n";
      if (defined $freqs{$key}{$rule}) {
        $comment .= $freqs{$key}{$rule};
      } else {
        $comment .= "(could not find freqs for rule '$rule'/'$oldname')\n";
      }
      $cmt2 .= "full freqs: $conf{RULEQA_URL}?rule=$rule&date=$date\n";
    }

    $comment .= "\n".$cmt2;
    $comment .= $freqs_hdr{$key};
  }

  if ($messages) {
    $comment .= "\n".$messages."\n";
  }

  $comment .= $boilerplate_footer;

  if (!$SEND_DATA) {
    print "would post on bug $bug:-------\n$comment\n------------------\n";
    return;
  }

  $mech->get (
'http://issues.apache.org/SpamAssassin/show_bug.cgi?id='.$bug
  );
  my $current = $mech->content();
  $mech->submit_form (
    form_name => "changeform",
    fields => {
        comment => $comment
    }
  );

}

