#!/bin/sh
# Check that networking is up.
# Source function library.
# chkconfig: 2345 80 30
# description: TrendMicro InterScan Mail Script

DAEMON=/etc/iscan/sendmail
ORIG_SMTP=/etc/iscan/sendmail.rc
SMTPPKG=/etc/iscan/pkg/ISSMTPON
GETCONF="/etc/iscan/getconf"
rc_done=""
rc_failed=""

if [ ! -f $SMTPPKG ] ; then
	exit 0
fi

#
# RL to detect if the target linux is SUSE
#
if [ -f /etc/SuSE-release ]; then
	DIS_TYPE=SuSE
fi

SMTPON=`cat $SMTPPKG`

. /etc/iscan/isfunctions	# isdaemon/iskillproc
if [ $DIS_TYPE = "SuSE" ] ; then
     dummy=1
else
    . /etc/rc.d/init.d/functions
fi

# Source networking configuration.
if [ $DIS_TYPE != "SuSE" ] ; then
    . /etc/sysconfig/network
fi

return=$rc_done


# Source sendmail configureation.
if [ $DIS_TYPE = "SuSE" ] ; then
	if [ -f /etc/rc.config.d/sendmail.rc.config ] ; then
        . /etc/rc.config.d/sendmail.rc.config
	fi
	if test -z "$SENDMAIL_ARGS" ; then
		SENDMAIL_ARGS="-bd -q30m -om"
		DAEMON=yes
		QUEUE=30m
	else
    	    DAEMON=yes
    	    QUEUE=30m
	fi
else
	if [ -f /etc/sysconfig/sendmail ] ; then
		. /etc/sysconfig/sendmail
	else
		DAEMON=yes
		QUEUE=1h
	fi
fi


ExecOrgSendmail() {
        /usr/bin/newaliases > /dev/null 2>&1
        for i in virtusertable access domaintable mailertable ; do
            if [ -f /etc/mail/$i ] ; then
                makemap hash /etc/mail/$i < /etc/mail/$i
            fi
        done
        isdaemon /usr/sbin/sendmail $([ "$DAEMON" = yes ] && echo -bd) \
                                  $([ -n "$QUEUE" ] && echo -q$QUEUE) \
                                  $([ "$ORG_PORT" -ne 0 ] && echo -o Oport=$ORG_PORT)
        RETVAL=$?
}

# Check that networking is up.
if [ $DIS_TYPE != "SuSE" ] ; then
	[ ${NETWORKING} = "no" ] && exit 0
fi


# See how we were called.
case "$1" in
  start)
	# Start daemons.
	echo -n "Starting sendmail: "
	if [ $SMTPON = "1" ] ; then
	  cd /etc/iscan
	  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
	  if [ `$GETCONF smtp local_mode` = "yes" ] && [ `$GETCONF smtp command_mode` = "no" ] ; then
		cd /etc/iscan
		DAEMON=yes
		ORG_PORT=`$GETCONF smtp local_daemon_port`
		isdaemon /etc/iscan/sendmail || return=$rc_failed
		echo -e "$return"
	  else
		# command mode
		isdaemon /etc/iscan/sendmail || return=$rc_failed
		DAEMON=no
		ORG_PORT=0
		echo -e "$return"
	  fi
	else
	  if [ -f $ORIG_SMTP ] ; then
	  	$ORIG_SMTP start
	  fi
	fi
	;;
  stop)
	# Stop daemons.
	if [ $SMTPON = "1" ] ; then
		echo -n "Shutting down sendmail: "
		# kill only all "sendmail" (iscan/sendmail, /usr/sbin/sendmail)
		iskillproc iscan/sendmail || return=$rc_failed
		#iskillproc iscan/sendmail || return=$rc_failed
		#iskillproc sbin/sendmail || return=$rc_failed
		echo -e "$return"
	else
		if [ -f $ORIG_SMTP ] ; then 
			$ORIG_SMTP stop
		fi
	fi
	;;
  restart)
	$0 stop
	$0 start
	;;
  status)
	if [ $DIS_TYPE = "SuSE" ] ; then
		checkproc /etc/iscan/sendmail && echo ok || echo No process
		if [ -f $ORIG_SMTP ] ; then 
			$ORIG_SMTP status
		fi
	else
		status sendmail
	fi


	;;
  *)
	echo "Usage: sendmail {start|stop|restart|status}"
	exit 1
esac

test "$return" = "$rc_done" || exit 1
exit 0

