#!/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) 2004, 2005, 2006 Zimbra, Inc.
# All Rights Reserved.
# 
# Contributor(s):
# 
# ***** END LICENSE BLOCK *****
# 

progdir=`dirname $0`

source `dirname $0`/zmshutil || exit 1
zmsetvars \
	zimbra_home \
	zimbra_server_hostname 

platform=`/opt/zimbra/libexec/get_plat_tag.sh`

rewriteconfig() {
    /opt/zimbra/libexec/zmmtaconfig mta > /dev/null 2>&1
}

if [ "x$1" = "xstatus" -o "x$1" = "xstart" ]; then
	${zimbra_home}/postfix-${postfix_version}/sbin/postqueue -p > /dev/null 2>&1
	R=$?
	if [ "x$1" = "xstart" ]; then
		if [ "x$R" = "x0" ]; then
			exit 0
		fi
	else
		if [ x$R != "x0" ]; then
			exit 1
		else
			exit 0
		fi
	fi
fi

if [ "x$1" = "xstart" -o "x$1" = "xreload" ]; then
	if [ x$2 = "x" ]; then
		rewriteconfig
	fi
	${zimbra_home}/postfix-${postfix_version}/sbin/postmap ${zimbra_home}/conf/postfix_header_checks
	#sudo ${zimbra_home}/postfix-${postfix_version}/sbin/postsuper -r ALL
fi

if [ -f /etc/aliases ]; then
  sudo ${zimbra_home}/postfix-${postfix_version}/sbin/postalias /etc/aliases
fi

sudo ${zimbra_home}/postfix-${postfix_version}/sbin/postfix "$@"

