#!/usr/bin/perl
#
# ***** BEGIN LICENSE BLOCK *****
# Zimbra Collaboration Suite Server
# Copyright (C) 2013, 2014, 2015, 2016 Synacor, Inc.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software Foundation,
# version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>.
# ***** END LICENSE BLOCK *****
#
use strict;
use lib '/opt/zimbra/common/lib/perl5';
use Getopt::Long;

if ( ! -d "/opt/zimbra/data/spamassassin/localrules" ) {
  print "ERROR: SpamAssassin does not appear to be installed - exiting\n";
  exit(1);
}

my $id = getpwuid($<);
chomp $id;
if ($id ne "zimbra") {
  print STDERR "Error: must be run as zimbra user\n";
  exit (1);
}

my ($help, %loaded);

my $opts_good = GetOptions(
  'h|help' => \$help,
);

if (!$opts_good) {
  print STDERR "\n";
  usage();
}
if ($help) {
  usage(0);
}

my $zmlocalconfig = "/opt/zimbra/bin/zmlocalconfig";
my $rule_updates_enabled = getLocalConfig("antispam_enable_rule_updates");
if (lc($rule_updates_enabled) =~ /true/) { $rule_updates_enabled= 1; }
if (lc($rule_updates_enabled) =~ /false/) { $rule_updates_enabled= 0; }

if (!$rule_updates_enabled) {
  exit;
}

my $really_allow_plugins = getLocalConfig("antispam_saupdate_reallyallowplugins");
   $really_allow_plugins = $really_allow_plugins =~ /true/ ? "--reallyallowplugins" : "";
my $sa="/opt/zimbra/common/bin/sa-update -v $really_allow_plugins --refreshmirrors >/dev/null 2>&1";

my $restart="/opt/zimbra/bin/zmamavisdctl restart norewrite >/dev/null 2>&1";
my $compile="/opt/zimbra/libexec/zmsacompile >/dev/null 2>&1";

my $restart_enabled = getLocalConfig("antispam_enable_restarts");
my $restart_required;

my $compile_rules=getLocalConfig("antispam_enable_rule_compilation");

if (lc($restart_enabled) =~ /true/) { $restart_enabled = 1; }
if (lc($restart_enabled) =~ /false/) { $restart_enabled = 0; }

if (lc($compile_rules) =~ /true/) { $compile_rules = 1; }
if (lc($compile_rules) =~ /false/) { $compile_rules = 0; }

qx($sa);
my $rc = $?>>8;
if ($rc == 1) {
  exit;
} elsif ($rc == 0) {
  $restart_required=1;
} else {
  print "zmsaupdate: Error code downloading update: $rc\n";
}

if ($restart_required == 0) {
    exit;
}

if ($compile_rules) {
  qx($compile);
}

if ($restart_enabled) {
  qx($restart);
  $rc = $?>>8;
} else {
  exit;
}

if ($rc == 0) {
  exit;
}

print "zmsaupdate: Amavisd restart failed!\n";
exit 1;

sub usage {

  my ($msg) = (@_);

  $msg && print STDERR "\nERROR: $msg\n";
  print STDERR <<USAGE;
  zmsaupdate

  Updates SpamAssassin rules

USAGE
  exit (1);
}

sub getLocalConfig {
  my ($key,$force) = @_;

  return $loaded{lc}{$key}
    if (exists $loaded{lc}{$key} && !$force);

  my $val = qx($zmlocalconfig -x -s -m nokey ${key} 2> /dev/null);
  chomp $val;
  $loaded{lc}{$key} = $val;
  return $val;
}

