#!/bin/sh
# ident @(#)maild	1.4	01/09/03 

#
# IScan Mail deliver daemon startup
#
# chkconfig: 345 99 99
# description: Startup script for the InterScan mail delivery 
MAILD=/etc/iscan/isdelvd
PIDFILE=/etc/iscan/pids/isdelvd.pids
PLOCKFILE=/etc/iscan/isdelv.boot.lock

case "$1" in
'start')

        IS_EXIST=`ps -ef | grep "/etc/iscan/isdelvd" | grep -v grep `
	if test "$IS_EXIST"
	then
		exit 1 
	fi
	rm -f $PLOCKFILE > /dev/null

               unset JP_FUNC   # clear
               /etc/iscan/getconf scan-configuration jp_func > /dev/null 2>&1
               if [ $? = 0 ] ; then
                        if [ `/etc/iscan/getconf scan-configuration jp_func` = "no" ] ; then
                                JP_FUNC="no"
                                export JP_FUNC
                        fi
                fi
		cd /etc/iscan
		$MAILD > /dev/null 2>&1
	;;

'stop')


        for i in `ps -ef | grep "/etc/iscan/isdelvd" | grep -v grep |  awk '{ print $2 }'`
        do
          if test "$i"
          then
                  kill -TERM $i
          fi
        done 

        COUNT=0
        while ps -ef | grep "/etc/iscan/isdelvd" | grep -v grep > /dev/null 2>&1 ; do
		sleep 1
		COUNT=`expr $COUNT + 1`
		if test "$COUNT" = "5"
		then
			for i in `ps -ef | grep "/etc/iscan/isdelvd" | grep -v grep |  awk '{ print $2 }'`
			do
				#echo "FORCE TO KILL $i"
				kill -KILL $i
			done
			break
		fi
	done  


	rm -f $PIDFILE   > /dev/null	
	rm -f $PLOCKFILE > /dev/null
	;;

*)
	echo "usage: $0 start|stop"
	exit 1
	;;
esac
