#!/bin/sh
#
# skeleton	example file to build /etc/init.d/ scripts.
#		This file should be used to construct scripts for /etc/init.d.
#
#		Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#		Modified by Davide Libenzi <davidel@xmailserver.org>
#
# Version:	@(#)skeleton  1.8  03-Mar-1998  miquels@cistron.nl
#
# xmail        This shell script takes care of starting and stopping the XMail.
# chkconfig: 2345 90 10
# description:  XMail Server - Modified by PassKorea.NET
# processname: xmail
# pidfile: /var/run/XMail.pid

if [ -f /var/MailRoot/NMAIL_APACHE_GID ]
then
    #export NMAIL_APACHE_GID=`cat /var/MailRoot/NMAIL_APACHE_GID`
    NMAIL_APACHE_GID=`cat /var/MailRoot/NMAIL_APACHE_GID`
    export NMAIL_APACHE_GID
fi
XMAIL_ROOT=/var/MailRoot

# SMTP/POP3 Port - Standard
XMAIL_CMD_LINE="-Mr 24 -Pl -Sl -Ql -Qg -Yl -Fl -Cl -Ll -Qt 120 -Qr 5 -St 300 -Pt 300 -Se 3600 -QT 900 -Qn 16 -Mx 3 -CX 1024 -Pw 10 -SI 0.0.0.0:25 -SI 0.0.0.0:587"

# SMTP/POP3 Port - User defined
#XMAIL_CMD_LINE="-Mr 24 -Pl -Sl -Ql -Qg -Yl -Fl -Cl -Ll -Qt 120 -Qr 5 -St 300 -Pt 300 -Se 3600 -QT 900 -Qn 16 -Mx 3 -CX 1024 -Pw 10 -Fp 79 -Cp 6017 -SI 0.0.0.0:20025 -SI 0.0.0.0:20587 -Pp 20110"

PATH=$XMAIL_ROOT/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=$XMAIL_ROOT/bin/XMail
NAME=XMail
DESC="XMail server"

test -f $DAEMON || exit 0

set -e
ulimit -c 20000
ulimit -u 65536 -n 65536

start_xmail()
{
    if [ -f /var/run/$NAME.pid ]
    then
        XMAIL_PID=`cat /var/run/$NAME.pid`
	set +e
        if [ "`ps -p $XMAIL_PID|grep $XMAIL_PID|grep $NAME`" = "" ]; 
        then
            rm -f /var/run/$NAME.pid
            rm -f $XMAIL_ROOT/.shutdown
        else
            echo "$NAME.[$XMAIL_PID ] already running"
            exit
        fi
	set -e
    fi
    MAIL_ROOT=$XMAIL_ROOT
	export MAIL_ROOT
    MAIL_CMD_LINE=$XMAIL_CMD_LINE
	export MAIL_CMD_LINE
    $DAEMON
    while [ ! -f /var/run/$NAME.pid ]
    do
        echo -n .
	sleep 1
    done
}

stop_xmail()
{
    if [ -f /var/run/$NAME.pid ]
    then
        XMAIL_PID=`cat /var/run/$NAME.pid`
	set +e
        if [ "`ps -p $XMAIL_PID|grep $XMAIL_PID|grep $NAME`" = "" ]; 
        then
	    set -e
            rm -f /var/run/$NAME.pid
            rm -f $XMAIL_ROOT/.shutdown
        else
	    set -e
            echo `date` > $XMAIL_ROOT/.shutdown
            kill -INT `cat /var/run/$NAME.pid`
	    
            COUNTER=0
            while [ -f $XMAIL_ROOT/.shutdown ]
            do
	        if [ $COUNTER -gt 15 ]
		then
		    if [ -f /var/run/$NAME.pid ]
		    then
		        kill -9 `cat /var/run/$NAME.pid`
                        rm -f $XMAIL_ROOT/.shutdown
		    fi
		    break
		fi
                #COUNTER=$(($COUNTER+1))
                COUNTER=`expr $COUNTER + 1`
                echo -n .
                sleep 1
            done
        fi

    fi
}

debug_xmail()
{
    MAIL_ROOT=$XMAIL_ROOT
        export MAIL_ROOT
    MAIL_CMD_LINE=$XMAIL_CMD_LINE
        export MAIL_CMD_LINE
    $DAEMON $MAIL_CMD_LINE &
    while [ ! -f /var/run/$NAME.pid ]
    do
        sleep 1
    done
}


case "$1" in
  start)
      echo -n "Starting $DESC: "
      start_xmail
      echo "$NAME.[" `cat /var/run/$NAME.pid` "]"
	;;
  stop)
      echo -n "Stopping $DESC: "
      stop_xmail
      echo "$NAME."
	;;
  debug)
      XMAIL_CMD_LINE="-Md "$XMAIL_CMD_LINE
      echo "Starting Debug... (Option : "$XMAIL_CMD_LINE")"
      debug_xmail
      echo -n "Starting $DESC: "
      echo "$NAME.[" `cat /var/run/$NAME.pid` "]"
        ;;
  #reload)
	#
	#	If the daemon can reload its config files on the fly
	#	for example by sending it SIGHUP, do it here.
	#
	#	If the daemon responds to changes in its config file
	#	directly anyway, make this a do-nothing entry.
	#
	# echo "Reloading $DESC configuration files."
	# start-stop-daemon --stop --signal 1 --quiet --pidfile \
	#	/var/run/$NAME.pid --exec $DAEMON
  #;;
  restart|force-reload)
	#
	#	If the "reload" option is implemented, move the "force-reload"
	#	option to the "reload" entry above. If not, "force-reload" is
	#	just the same as "restart".
	#
	echo -n "Restarting $DESC: "
	stop_xmail
	sleep 1
	start_xmail
        echo "$NAME.[" `cat /var/run/$NAME.pid` "]"	
	;;
  *)
	N=/etc/init.d/$NAME
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
