#!/bin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright (c) 1991, 2013, Oracle and/or its affiliates. All rights reserved.

. /lib/svc/share/smf_include.sh

# for debug, uncomment it
#set -x

method=$1
#instance=$2
instance='exim4'
contract=$3

LANG=C
export LANG

#read default file
QUEUERUNNER='combined'
QUEUEINTERVAL='30m'
UPEX4OPTS=''

PIDDIR=/var/run/$instance
SERVER_PID_FILE="${PIDDIR}/exim.pid"
QR_PID_FILE="${PIDDIR}/eximqr.pid"
QUEUE=/var/spool/$instance
DAEMON="/usr/sbin/exim4"

DEFAULT_FILE=/etc/default/$instance
[ -f $DEFAULT_FILE ] && . $DEFAULT_FILE

upex4conf() {
  UPEX4CONF="update-exim4.conf"
  OLDIFS="$IFS"
  IFS=:
  for p in $PATH; do
    if [ -x "$p/$UPEX4CONF" ]; then
      IFS="$OLDIFS"
      $p/$UPEX4CONF $UPEX4OPTS $1
      return 0
    fi
  done
  IFS="$OLDIFS"
}

exim_start()
{

	if [ ! -d ${QUEUE} ]; then
		/usr/bin/mkdir -m 0750 ${QUEUE}
		/usr/bin/chown Debian-exim:Debian-exim ${QUEUE}
	fi
	if [ ! -d /var/mail/:saved ]; then
		/usr/bin/mkdir -m 0775 /var/mail/:saved
		/usr/bin/chown root:mail /var/mail/:saved
	fi

#	val=`/usr/bin/svcprop -p config/queuerunner $SMF_FMRI 2>/dev/null`
#	if [ -n $val ]
#	then
#		QUEUERUNNER=$val
#	fi
#	val=`/usr/bin/svcprop -p config/queueinterval $SMF_FMRI 2>/dev/null`
#	if [ -n $val ]
#	then
#		QUEUEINTERVAL=$val
#	fi
#	if [ -n $QUEUEINTERVAL ]
#	then
#		QUEUEINTERVAL="30m"
#	fi

	case $QUEUERUNNER in
	'combined')
		$DAEMON -bd -q${QFLAGS}${QUEUEINTERVAL} -oP $SERVER_PID_FILE\
			${COMMONOPTIONS} ${QUEUERUNNEROPTIONS}\
			${SMTPLISTENEROPTIONS}
		;;
	'separate')
		$DAEMON -bd -oP $SERVER_PID_FILE\
			${COMMONOPTIONS} ${SMTPLISTENEROPTIONS}
		$DAEMON -oP $QR_PID_FILE -q${QFLAGS}${QUEUEINTERVAL}\
			${COMMONOPTIONS} ${QUEUERUNNEROPTIONS}
		;;
	'queueonly')
		$DAEMON -oP $SERVER_PID_FILE -q${QFLAGS}${QUEUEINTERVAL}\
			${COMMONOPTIONS} ${QUEUERUNNEROPTIONS}
		;;
	'no'|'ppp')
		$DAEMON -oP $SERVER_PID_FILE -bd\
			${COMMONOPTIONS} ${SMTPLISTENEROPTIONS}
		;;
	esac
}


check_and_kill()
{

	PID=`head -1 $1`
	kill -0 $PID > /dev/null 2>&1
	[ $? -eq 0 ] && kill $PID
}


exim_stop()
{

	[ -f $SERVER_PID_FILE ] && check_and_kill $SERVER_PID_FILE
	[ -f $QR_PID_FILE ] && check_and_kill $QR_PID_FILE
	# Need to kill the entire service contract to kill all exim related
	# processes
	smf_kill_contract $contract TERM 1 30
	ret=$?
	[ $ret -eq 1 ] && exit 1

	# Since exim spawns user processes out of .forward files, it is
	# possible that some of these are not responding to TERM.  If the
	# contract did not empty after TERM, move on to KILL.
	if [ $ret -eq 2 ] ; then
		smf_kill_contract $contract KILL 1
	fi
	rm -f $SERVER_PID_FILE $QR_PID_FILE
}


case $method in
'start')
#	upex4conf
	exim_start
	;;

'stop')
	exim_stop
	;;

'refresh')
        [ -f $SERVER_PID_FILE ] && kill -1 `head -1 $SERVER_PID_FILE`
        [ -f $QR_PID_FILE ] && kill -1 `head -1 $QR_PID_FILE`
	;;

*)
	echo "Usage: $0 [start|stop|refresh] <instance> <contract>"
	exit 1
	;;
esac
exit $SMF_EXIT_OK
