#!/bin/bash
# 
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1
# 
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 ("License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.zimbra.com/license
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
# 
# The Original Code is: Zimbra Collaboration Suite Server.
# 
# The Initial Developer of the Original Code is Zimbra, Inc.
# Portions created by Zimbra are Copyright (C) 2005, 2006 Zimbra, Inc.
# All Rights Reserved.
# 
# Contributor(s):
# 
# ***** END LICENSE BLOCK *****
# 

BASE=/opt/zimbra
CONFIG=$BASE/conf/logswatchrc

PIDFILE=/opt/zimbra/log/logswatch.pid

if [ -f $PIDFILE ]; then
    PID=`cat $PIDFILE`
fi

source /opt/zimbra/.bashrc

case "$1" in 
	start)
		if [ -f $CONFIG ]; then

      # make sure it's not already running
      if [ x$PID != 'x' ]; then
        kill -0 $PID 2> /dev/null
        if [ $? = 0 ]; then
          exit 1
        fi
      fi
			$BASE/libexec/logswatch --config-file=$BASE/conf/logswatchrc \
			--use-cpan-file-tail \
			--script-dir=/tmp -t /var/log/zimbra.log >/tmp/logswatch.out 2>&1 &
			PID=$!
			chmod 777 /tmp/logswatch.out
			echo $PID > $PIDFILE
			if [ x$PID != 'x' ]; then
				kill -0 $PID
				exit $?
			else
				exit 1
			fi
		else
			exit 1
		fi
	;;
	stop)
		rm -f $PIDFILE
		if [ x$PID != 'x' ]; then
			kill -TERM $PID
		fi
		if [ "x$platform" = "xMACOSX" ]; then
				killall -TERM logswatch > /dev/null 2>&1
		else
				killall -TERM ${zimbra_home}/libexec/logswatch > /dev/null 2>&1
		fi
		exit 0
	;;
	reload)
		if [ x$PID != 'x' ]; then
			kill -HUP $PID
		fi
	;;
	status)
		if [ x$PID != 'x' ]; then
			kill -0 $PID
			exit $?
		else
			exit 1
		fi
	;;
	*)
		echo "$0 start|stop|reload|status"
		exit 1
	;;
esac
