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

BASE=/opt/zimbra
SCRIPTS="antispam-mysql.server zmamavisdctl"

source /opt/zimbra/.bashrc

ENABLED=1
/opt/zimbra/bin/zmprov -l gs `/opt/zimbra/bin/zmhostname` zimbraServiceEnabled | grep -qw antispam
if [ $? = 1 ]; then
  ENABLED=0
fi

case "$1" in
	start)
		if [ $ENABLED = 1 ]; then
			for i in $SCRIPTS; do
				/opt/zimbra/bin/$i start $2
			done
		fi
	;;
	stop)
		if [ $ENABLED = 1 ]; then
			for i in $SCRIPTS; do
				/opt/zimbra/bin/$i stop
			done
		fi
	;;
	reload|restart)
		if [ $ENABLED = 1 ]; then
			for i in $SCRIPTS; do
				/opt/zimbra/bin/$i $1
			done
		fi
	;;
	status)
		STATUS=0
		if [ $ENABLED = 1 ]; then
			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
		fi
		exit $STATUS
	;;
	*)
		echo "$0 start|stop|restart|reload|status"
		exit 1
	;;
esac
