#! /bin/sh
#
# This is /etc/rc.d/init.d file for Courier-IMAP
#
# chkconfig: 2345 80 30
# description: Courier-IMAP - IMAP server
#
# Following lines are inserted for compatibility with SuSE service registration system
#
### BEGIN INIT INFO
# Provides:          courier-authdaemon courier-imap courier-imap-ssl courier-pop3 courier-pop3-ssl
# Required-Start:    $syslog $remote_fs
# X-UnitedLinux-Should-Start:
# Required-Stop:     $syslog $remote_fs
# X-UnitedLinux-Should-Stop: courier-imap
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Courier-IMAP server
# Description:       Start the Courier-IMAP server,
#                    which is an IMAP and POP3 server for Maildir mailboxes.
### END INIT INFO

prefix=/
exec_prefix=/
sysconf=/etc/courier-imap
COURIERIMAP_LIBEXECDIR=/usr/lib/courier-imap
COURIERIMAP_DATADIR=/usr/share/courier-imap

# Source networking configuration.
if [ -f /etc/sysconfig/network ]; then
	. /etc/sysconfig/network

	# Check that networking is up.
	[ "${NETWORKING}" = "no" ] && exit 0
fi

# Source function library.
if [ -f /etc/rc.d/init.d/functions ]; then
	. /etc/rc.d/init.d/functions
fi

# this way is for SuSE Linux
test -s /etc/rc.status && . /etc/rc.status && rc_reset

myname=courier-imap
daemon_name=couriertcpd

# this function is designed to emulate RedHat-like service reporting on the SuSE Linux
report_action()
{
	# $1 - text to put
	# $2 - status of operation
	case `type -t action` in
		function)
			action $"$1" $2
			;;
		*)
			case `type -t rc_reset` in
				function)
					# suse linux reporting style
					rc_reset
					echo -n $"$1"
					$2
					rc_status -v
					;;
				*)
					# debian style, probably useful for other systems
					echo -n $"$1"
					$2
					if [ $? -eq 0 ]; then
						echo " done"
					else
						echo " failed"
					fi
					;;
			esac
			;;
	esac
}

# this routine is courier-imap specific override of standard status function
status() {
	local base=${1##*/}
	local dbase=${2##*/}
	local pid

	# Test syntax.
	if [ $# = 0 ] ; then
		echo $"Usage: status {service name} {daemon name}"
		return 1
	fi

	# First try "pidof"
	pid=`pidof -o $$ -o $PPID -o %PPID -x $2 || \
	     pidof -o $$ -o $PPID -o %PPID -x ${dbase}`
	if [ "$pid" != "" ] ; then
	        echo $"${dbase} (pid $pid) is running..."
	        return 0
	fi

	# Next try "/var/run/*.pid" files
	if [ -f /var/run/${base}.pid ] ; then
	        read pid < /var/run/${base}.pid
	        if [ "$pid" != "" ] ; then
	                echo $"${base} dead but pid file exists"
	                return 1
	        fi
	fi
	# See if /var/lock/subsys/${base} exists
	if [ -f /var/lock/subsys/${base} ]; then
		echo $"${base} dead but subsys locked"
		return 2
	fi
	echo $"${base} is stopped"
	return 3
}


start() 
{
#        cd /
	. ${sysconf}/imapd

	echo "Starting Courier-IMAP server:"

	case x$IMAPDSTART in
	x[yY]*)
		# Start daemons.
		report_action "   Starting imapd" "$COURIERIMAP_LIBEXECDIR/imapd.rc start"
		;;
	esac

	. ${sysconf}/imapd-ssl
	case x$IMAPDSSLSTART in
	x[yY]*)
		if test -x $COURIERTLS
		then

		# First time we start this, generate a dummy SSL certificate.

			if test ! -f ${COURIERIMAP_DATADIR}/imapd.pem
			then
				echo -n " generating-SSL-certificate..."
				${COURIERIMAP_DATADIR}/mkimapdcert >/dev/null 2>&1
				echo
			fi
			report_action "   Starting imap-ssl" "$COURIERIMAP_LIBEXECDIR/imapd-ssl.rc start"
		fi
		;;
	esac

	POP3DSTART=""
	POP3DSSLSTART=""

	[ -f ${sysconf}/pop3d ] && . ${sysconf}/pop3d

	case x$POP3DSTART in
	x[yY]*)
		# Start daemons.
		report_action "   Starting pop3" "$COURIERIMAP_LIBEXECDIR/pop3d.rc start"
		;;
	esac

	[  -f ${sysconf}/pop3d-ssl ] &&  . ${sysconf}/pop3d-ssl

	case x$POP3DSSLSTART in
	x[yY]*)
		if test -x $COURIERTLS
		then

		# First time we start this, generate a dummy SSL certificate.

			if test ! -f ${COURIERIMAP_DATADIR}/pop3d.pem
			then
				echo -n " generating-SSL-certificate..."
				${COURIERIMAP_DATADIR}/mkpop3dcert >/dev/null 2>&1
			fi
			report_action "   Starting pop3-ssl" "$COURIERIMAP_LIBEXECDIR/pop3d-ssl.rc start"
		fi
		;;
	esac

	echo ""

	if [ -d "`dirname /var/lock/subsys/$myname`" ]; then
		touch /var/lock/subsys/$myname
	fi
}
	
stop()
{
        echo "Stopping Courier-IMAP server:"
	. ${sysconf}/imapd
	. ${sysconf}/imapd-ssl
	report_action "   Stopping imap" "$COURIERIMAP_LIBEXECDIR/imapd.rc stop"
	if test -x $COURIERTLS
	then
		report_action "   Stopping imap-ssl" "$COURIERIMAP_LIBEXECDIR/imapd-ssl.rc stop"
	fi

	if test -f ${sysconf}/pop3d
	then
		report_action "   Stopping pop3" "$COURIERIMAP_LIBEXECDIR/pop3d.rc stop"

		if test -x $COURIERTLS
		then
			report_action "   Stopping pop3-ssl" "$COURIERIMAP_LIBEXECDIR/pop3d-ssl.rc stop"
		fi
	fi

	echo ""
	rm -f /var/lock/subsys/$myname
}

restart(){
    stop
    start
}

condrestart(){
    [ -e /var/lock/subsys/$myname ] && restart || true
}

#reload(){
#    [ -e /var/lock/subsys/$myname ] && mysqladmin reload
#}
	
# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status $myname $daemon_name
    ;;
  reload)
    restart
#    reload
    ;;
  restart)
    restart
    ;;
  condrestart)
    condrestart
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|reload|condrestart|restart}"
    exit 1
esac

exit $?
