#!/usr/bin/perl

# Copyright 2001-2004 The Apache Software Foundation
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

#
# Nag people and mailing lists upon particular build events,
# typically failures.
#
# @author Sam Ruby, Jon Stevens
# @see naglist for an example
#
# Note:
#   Regep must be the last line of a group
#   At most only one email will be generated to each project/email pair

($sec,$min,$hour,$mday,$mon,$year) = localtime;
$date=sprintf("%d-%2.2d-%2.2d", $year+1900, $mon+1, $mday);
$url = "/home/bodewig/dev/gump/log";
$home = "/home/bodewig/dev/gump/log";

$sentto = {};

open (LIST, $ARGV[0]) or die "unable to open naglist $ARGV[0]";

foreach (<LIST>) {
  next if /^#/;         # ignore comments
  next if /^\s*$/;      # ignore blank lines

  # parse simple data
  $from    = $1 if /From:\s*(.*?)\s*$/;
  $mailto  = $1 if /To:\s*(.*?)\s*$/;
  $subject = $1 if /Subject:\s*(.*?)\s*$/;

  # Project
  if (/Project:\s*(.*?)\s*$/) {
    $project = $1;
    $sentto = {};

    # read the entire file
    open (FILE, "$home/$project.html");
    $_ = join('',<FILE>);
    close (FILE);

    # extract just the stuff from inside the XMP tag
    ($pageData)=m! .* <XMP> \s* (.*) \s* </XMP> !xs;
    if (not $pageData) {
      # if none found, go for the html, and remove the tags
      ($pageData)=m! .* <p> \s* (.*) \s* </p> !xs;
      $pageData =~ s/<.*?>//g;
    }
  }

  # Regex
  if (/Regex:\s*(.*?)\s*$/) {

    # only nag once per project/mailto pair
    next if $sentto->{$mailto};

    if (eval "\$pageData =~ $1") {

      $sentto->{$mailto}=1;

      open ( EMAIL, "| /usr/sbin/sendmail -i -t");

      print EMAIL "From: $from\n";
      print EMAIL "To: $mailto\n";
      print EMAIL "Subject: $subject\n\n";

      print EMAIL "----------------------------------------------------\n";
      print EMAIL "This email is autogenerated from the output from:\n";
      print EMAIL "<$url/$project.html>\n";
      print EMAIL "----------------------------------------------------\n\n";

      if ($pageData) {
        if (length($pageData)>50000) { # Apache's ezmlm limit is 100000
          print EMAIL "Build results exceed maximum length.\n";
          print EMAIL "Please see URL above for details.\n";
          print EMAIL "Last 50 lines of build output follows.\n\n";

          @lines = split("\n",$pageData);
          $pageData = join("\n", splice(@lines, $#lines-50));
        }

        print EMAIL "$pageData";
      } else {
        print EMAIL "Build results missing. Please see URL above for details.";
      }

      close (EMAIL);
    }
  }
}

close (LIST);
