#! /bin/sh
#
# This is /etc/rc.d/init.d file for Q-Mail
#
# chkconfig: 345 80 30
# description: qmail Mail Transfer Agent
# processname: qmail-send
#
#
### BEGIN INIT INFO
# Provides:       sendmail qmail
# Required-Start: $network $named $syslog $remote_fs $time
# X-UnitedLinux-Should-Start:
# Required-Stop:
# Default-Start:  3 5
# Default-Stop:
# Description:    start the qmail MTA
### END INIT INFO

# 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/init.d/functions ]; then
	. /etc/init.d/functions
fi

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

if ! type status >/dev/null 2>/dev/null; then
    status () {
	/sbin/pidof >/dev/null qmail-send
    }
fi

myname=qmail
qmail_dir=/var/qmail
subsys=/var/lock/subsys/$myname
PATH="$qmail_dir/bin:$PATH" 
export PATH
proccess=qmail-send

# 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
			;;
		*)
			rc_reset
			echo -n $"$1"
			$2
			rc_status -v
			;;
	esac
}

start() 
{
		qmail-start ./Maildir/ splogger qmail &
		sleep 1
		[ "`pidofproc $proccess`" ]
        	ret=$?
        	if [ $ret -eq 0 ]; then
        	    report_action "Starting $myname: " /bin/true
        	else
        	    report_action "Starting $myname: " /bin/false
        	fi
        	[ $ret -eq 0 ] && touch $subsys
        	return $ret
}

check_and_start()
{
	if [ ! -f $subsys ]; then
		start
	fi
}
	
stop()
{
	if [ -f $subsys ]; then
		echo -n $"Stopping $prog: " 
		killproc $proccess
		ret=$?
        	[ $ret -eq 0 ] && rm -f $subsys
        	return $ret
	else
		killall $proccess  > /dev/null 2>&1
		true
	fi
}

restart(){
    stop
    start
}

condrestart(){
    [ -e $subsys ] && restart || true
}

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

exit $?
