#!/usr/bin/perl

use warnings;
use strict;

use lib( '/home/perl_test' );

use Mail::Audit;
use DBI;
use File::Basename;
use Email::Find;
use dbSmoke;

use constant LOGFILE => '>> /tmp/perl_test.log';
my $mail_dir = '/home/perl_test/mail/';

open LOG, LOGFILE;

print LOG "--------------------------------------------\n";

my $dbh =
   DBI->connect_cached( "DBI:Pg:dbname=cpan;host=chaos", "", "",
                        { RaiseError => 0, AutoCommit => 1 } );

my $mail = Mail::Audit->new( "log" => "/tmp/perl_test.audit" );
my $subject = $mail->subject;
my $from    = $mail->from;
my $date;

my $finder = Email::Find->new( sub{ my( $email, $orig_email ) = @_;
                                    $from=$email->format;
				    return $orig_email;});
$finder->find(\$mail->from);

print LOG "From: $from\n";

$mail->header =~ /Date: (.*)\n/;
if ( defined $1 )
   {
   $date = $1;
   $date =~ s/\(.*\)//g;
   }

print LOG "$date - $subject\n";

if ( $subject =~ /^CPAN Upload: (.*)$/ )
   {
   my $module = $1;

   if ( defined $date )
      {
      $dbh->do( qq/INSERT INTO modules( module, time, email )
                   VALUES ( '$module', '$date', 'true' )/ );
      print LOG "CPAN: inserted with date\n";
      }
   else
      {
      $dbh->do( qq/INSERT INTO modules( module, email )
                   VALUES ( '$module', 'true' )/ );
      print LOG "CPAN: inserted without date\n";
      }
   }
elsif ( $subject =~ /^(PASS|FAIL|UNKNOWN|NA) (\S+) (\S+) (\S+)$/ )
   {
   my ( $grade, $module, $arch, $osver ) = ( $1, $2, $3, $4 );
   my $grade_index = get_grade_index( $grade, $dbh );
   my $module_index = get_module_index( $module, $dbh );
   my $arch_index   = get_arch_index( $arch,     $dbh );
   my $osv_index    = get_osv_index( $osver,     $dbh );
   my $tester_index = get_tester_index( $from, $dbh );

   print LOG "$grade => $grade_index\n";
   print LOG "$grade $from => $tester_index\n";
   print LOG "$grade $module => $module_index\n";
   print LOG "$grade $arch => $arch_index\n";
   print LOG "$grade $osver => $osv_index\n";

   if ( defined $date )
      {
      $dbh->do( qq/INSERT INTO tested( tester_index, module_index,
                                       archname_index, osver_index,
				       grade_index, time )
	           VALUES( $tester_index, $module_index, $arch_index,
		           $osv_index, $grade_index, '$date' )/
      );
      print LOG "$grade: inserted with date\n";
      }
   else
      {
      $dbh->do( qq/INSERT INTO
          tested( module_index, archnames_index, osver_index, grade_index )
	  VALUES( $module_index, $arch_index, $osv_index, $grade_index )/
      );
      print LOG "$grade: inserted without date\n";
      }
   }

$dbh->disconnect;

$mail->accept( $mail_dir . 'PASS' )    if $subject =~ /^PASS/;
$mail->accept( $mail_dir . 'FAIL' )    if $subject =~ /^FAIL/;
$mail->accept( $mail_dir . 'UNKNOWN' ) if $subject =~ /^UNKNOWN/;
$mail->accept( $mail_dir . 'NA' ) if $subject =~ /^NA/;
$mail->accept( $mail_dir . 'CPAN' )    if $subject =~ /^CPAN/;
$mail->accept;
