#!/bin/bash
#
# ***** BEGIN LICENSE BLOCK *****
# Zimbra Collaboration Suite Server
# Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 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 *****
#

SCRIPTS="zmamavisdctl zmclamdctl zmfreshclamctl"

if [ ! -x "/opt/zimbra/common/sbin/amavisd" ]; then
  exit 0
fi

source /opt/zimbra/.bashrc

case "$1" in
	start)
		if [ x$2 = "x" ]; then
			/opt/zimbra/libexec/configrewrite antivirus
		fi
		for i in $SCRIPTS; do
			/opt/zimbra/bin/$i start norewrite
		done
	;;
	stop)
		for i in $SCRIPTS; do
			if [ $i = "zmclamdctl" ]; then
				/opt/zimbra/bin/$i stop
			fi
			if [ $i = "zmfreshclamctl" ]; then
				/opt/zimbra/bin/$i stop
			fi
		done
	;;
	reload|restart)
		for i in $SCRIPTS; do
			/opt/zimbra/bin/$i $1
		done
	;;
	status)
		STATUS=0
		for i in $SCRIPTS; do
			/opt/zimbra/bin/$i status > /dev/null 2>&1
			R=$?
			if [ $R -ne "0" ]; then
				echo "$i is not running"
				STATUS=$R
			fi
		done
		if [ "$STATUS" -eq "0" ]; then
			echo "antivirus is running"
		fi
		exit $STATUS
	;;
	*)
		echo "$0 start|stop|restart|reload|status"
		exit 1
	;;
esac
