#! /bin/sh
# Copyright (c) 1996-2004 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version. 
#
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details. 
#
# You should have received a copy of the GNU General Public License 
# along with this program; if not, write to the Free Software 
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Author: Florian La Roche <florian@suse.de>, 1996, 1997
#	  Werner Fink <feedback@suse.de>, 1996, 1999
#	  Rainer Link <feedback@suse.de>, 2001-2003
#	  H+BEDV Datentechnik GmbH <support@antivir.de>, 2004
#	  Marius Tomaschewski <feedback@suse.de>, 2004-2005
#
### BEGIN INIT INFO
# Provides:                    avmailgate
# Required-Start:              $syslog $remote_fs $named
# Required-Stop:               $syslog $remote_fs $named
# X-UnitedLinux-Should-Start:
# X-UnitedLinux-Should-Stop:
# Default-Start:               3 5
# Default-Stop:                0 1 2 6
# Description:                 Start AvMailGate 
### END INIT INFO                        

#
# Note: binary has to match the name /proc/$PID/exe
# really points to, because of start/kill/checkproc.
#
AVGATED_BIN="/usr/sbin/avgated"
AVGATEFWD_BIN="/usr/sbin/avgatefwd"

AVGATED_PID="/var/run/avmailgate/avgated.pid"
AVGATEFWD_PID="/var/run/avmailgate/avgatefwd.pid"

AVMG_STATS_BIN="/usr/lib/AntiVir/gui/bin/avmg_stats"

test -x $AVGATED_BIN   || {
	test "$1" == "stop" && exit 0 || exit 5
}
test -x $AVGATEFWD_BIN || {
	test "$1" == "stop" && exit 0 || exit 5
}
test -r /etc/avmailgate.conf || {
	test "$1" == "stop" && exit 0 || exit 6
}
test -r /etc/avmailgate.acl  || {
	test "$1" == "stop" && exit 0 || exit 6
}


# First reset status of this service
. /etc/rc.status
  
rc_reset        

case "$1" in
      start)
       echo -n "Initializing service AvMailGate"
       startproc -p $AVGATED_PID $AVGATED_BIN
       rc_check 
       startproc -p $AVGATEFWD_PID $AVGATEFWD_BIN
       rc_status -v    
       ;;
      stop)
       echo -n "Shutting down service AvMailGate"
       if [ -e ${AVMG_STATS_BIN} ]; then
        killproc -TERM $AVMG_STATS_BIN
       fi
       killproc -TERM -p $AVGATED_PID $AVGATED_BIN
       rc_check
       killproc -TERM -p $AVGATEFWD_PID $AVGATEFWD_BIN
       rc_status -v
       ;;
     try-restart|condrestart)
       if test "$1" = "condrestart"; then
           echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
       fi
       $0 status
       if test $? = 0; then
           $0 restart
       else
           rc_reset        # Not running is not a failure.
       fi
       rc_status
       ;;
     restart)
       $0 stop
       $0 start
       rc_status
       ;;
     force-reload)
       # SIGHUP is not supported
       $0 try-restart
       rc_status
       ;;
     reload)
       # signaling is not supported so simply do nothing
       rc_failed 3
       rc_status -v
       ;;
     status)
       echo -n "Checking for service AvMailGate: "
       checkproc -p $AVGATED_PID $AVGATED_BIN
       rc_check 
       checkproc -p $AVGATEFWD_PID $AVGATEFWD_BIN
       rc_status -v
       ;;                
     probe)
       test /etc/avmailgate.conf -nt $AVGATEFWD_PID || \
       test /etc/avmailgate.acl  -nt $AVGATEFWD_PID && \
         echo restart
       ;;
     *)
       echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
       exit 1
       ;;
esac

rc_exit

