#!/usr/bin/perl -w

use strict;
sub run;


# Mar  6 2007 jm: use the "Cool Tools" Solaris-optimized perl
my $perl = '/usr/local/bin/perl';
# my $perl = $^X;

if (!$perl) {
  die "no perl path found in ARGV!";
}

my $slavename;

$|=1;
my $pwd = `pwd`;
$pwd =~ /slaves\/([-_A-Za-z0-9]+)\//; if ($1) {
  $slavename = $1;
} else {
  die "cannot work out slavename!  $pwd";
}

my %mass_check_args = (

  'mc-fast' =>      '--tail=2000',
  'mc-med' =>       '--tail=12000 --head=10000',
  ## 'mc-slow' =>      '--tail=22000 --head=10000',
  ## 'mc-slower' =>    '--tail=42000 --head=20000',

);

# super-nice please!
#
system ("renice +19 $$");

$ENV{'TMPDIR'} = '/tmpfs';

# cd to masses
#
chdir "masses" or die;

unlink ("ham.log", "spam.log");

# change of plan: mass-check the entire ruleset
#
system ("rm -rf tstrules");
run "mkdir tstrules";

run "cp ../rules/*.* tstrules";
run "cp plugins/*.* tstrules";

# well, ok just those, and anything that's been mailed-in
# 
if (-f 'mailed.cf') {
  run "cp mailed.cf tstrules/70_mailed.cf";
}

run "ls -l tstrules";

# create the user_prefs file for the mass-check.  Settings: turn off Bayes;
# turn off the auto-whitelist.

mkdir "spamassassin";
open PREFS, ">spamassassin/user_prefs";
print PREFS q{

  use_bayes 0
  use_auto_whitelist 0

};
close PREFS or die "cannot create 'spamassassin/user_prefs' file: $! $@";


# notes on this mass-check command:
#
# this is run in a chroot jail, just in case there's hostile rule code in
# there. 
# de-encapsulate 'report_safe' messages from petuniapress.com.
# produce lots of noisy output to stop the buildbot from timing out on
# mass-checks of large corpora.
# store AICache data in /tmpfs/aicache.
# ignore mails older than 6 months (use the nightly runs for those corpora,
# it's too slow to mass-check them here).

run "/local/bbmasstools/masschroot $perl ".
    "mass-check -c=tstrules --cache -j=1 ".
    "--noisy --deencap='petuniapress.com' ".
    "--cachedir=/tmpfs/aicache_bbmass ".
    "--after='6 months ago' ".
    $mass_check_args{$slavename}." ".
    "ham:detect:/export/home/bbmass/uploadedcorpora/*/ham/* ".
    "spam:detect:/export/home/bbmass/uploadedcorpora/*/spam/*";


exit;

# ---------------------------------------------------------------------------

sub run {
  my ($cmd, $ignoreexit) = @_;

  print "[$cmd]\n";
  system ($cmd);

  if (!$ignoreexit) {
    die "command '$cmd' failed with status $?" if (($? >> 8) != 0);
  } else {
    return ($? >> 8);
  }
}

