#!/usr/bin/perl -w

use strict;
use esmith::ConfigDB;

open(STDERR, ">&STDOUT") or warn "Failed to redirect stderr to stdout: $!";
my $c = esmith::ConfigDB->open_ro || die "Couldn't open the configuration database";

my $spamd   = $c->get('spamd');
my $spamass = $c->get('spamassassin');

if ( ($spamass->prop('status') || 'disabled') ne 'enabled'){
  exec('/usr/bin/sv', 'd', '.');
}
else{
  my $bayes = $spamass->prop('UseBayes') || '0';
  my $tell  = $spamd->prop('SpamLearning') || 'disabled';
  my @args  = qw(-u spamd --syslog=stderr --ipv4-only);
  if ($bayes =~ m/^1|yes|on|enabled$/ && $tell =~ m/^1|yes|on|enabled$/){
    push @args, '--allow-tell';
  }
  exec('/usr/bin/spamd', @args);
}
