#! /bin/sh
# Copyright (c) 2002 H+BEDV Datentechnik GmbH
# Copyright (c) 2002-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.
#
# Authors: John Ogness, H+BEDV Datentechnik GmbH
#          Rainer Link <feedback@suse.de>, 2002
#          Marius Tomaschewski <feedback@suse.de>, 2004
#
### BEGIN INIT INFO
# Provides:       avguard
# Required-Start: $syslog $remote_fs
# Required-Stop:
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Description:    Start AvGuard (H+BEDV AntiVir) for on-access virus scanning
### END INIT INFO

# The avguard type can be "server" or "workstation"
# and depends on the license file.
AVGUARD_TYPE="server"

# The name (or file) of the avguards dazuko module
# Sould be empty - the preferred way on 2.6 kernel
# is to load the module usind the
#   MODULES_LOADED_ON_BOOT
# variable in /etc/sysconfig/kernel.
DAZUKO_MODULE=""

if test -f /etc/sysconfig/antivir ; then
	. /etc/sysconfig/antivir

	case "$AVGUARD_TYPE"  in
	workstation|server) ;;
	*)
	    echo "AVGuard type not configured"
	    test "$1" = stop && exit 0 || exit 6
	;;
	esac

	case "$DAZUKO_MODULE" in
	dazuko)             ;; # default module name
	"")                 ;; # skip dazuko (un)loading
	*)
	    test -f "$DAZUKO_MODULE" || {
	        echo "Dazuko module '$DAZUKO_MODULE' not existing"
	        test "$1" = stop && exit 0 || exit 6
	    }
	;;
	esac
fi

DEVNAME="dazuko"
DEVFILE="/dev/$DEVNAME"

DAEMONNAME="antivir"
AVDIR="/usr/lib/AntiVir"

DAEMON="$AVDIR/$DAEMONNAME"
test -x $DAEMON || {
    echo "AntiVir binary '$DAEMON' not installed"
    test "$1" == stop && exit 0 || exit 5
}

CONFIG="/etc/avguard.conf"
test -f $CONFIG || {
    echo "AntiVir avguard config '$CONFIG' not existing"
    test "$1" == stop && exit 0 || exit 6
}

#
# we are using this pragmatic way, because
# antivir is broken and creates no pid file.
#
function find_avguard ()
{
	ps xho pid,args | grep -e "${DAEMONNAME}.*--${AVGUARD_TYPE}" | \
	                  grep -v grep | cut -b -6 2>/dev/null
}

function kill_avguard ()
{
	pids=$(find_avguard)
	if test -n "$pids" ; then
	    kill -TERM ${pids} &>/dev/null
	fi
}

# Source status functions:
. /etc/rc.status

# Reset status of this service
rc_reset

# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signaling is not supported) are
# considered a success.

case "$1" in
    start)
	echo "Starting AntiVir on-access scanning facility: avguard"

	echo -n "  loading $DEVNAME kernel module"
	if test -n "$DAZUKO_MODULE" ; then
	    modprobe -qs $DAZUKO_MODULE &>/dev/null
	    rc_status -v -r
	else
	    rc_status -s -r
	fi

	echo -n "  verifying $DEVNAME device"
	# read device info from proc
	DEV=$(grep $DEVNAME /proc/devices 2>/dev/null)
	set -- $DEV
	PROC_MAJOR=$(($1))
	test -n "$DEV" && test 0 -lt $PROC_MAJOR || {
	    rc_status -v
	    rc_exit
	}
	# check device file against proc
	if test -e $DEVFILE ; then
	    test -c $DEVFILE || {
	        rc_status -v
	        rc_exit
	    }
	    FILE_MAJOR=$(printf "%d\n" $(stat --format="0x%t" \
	                                 $DEVFILE 2>/dev/null))
	    if test $PROC_MAJOR -ne $FILE_MAJOR ; then
	        kill_avguard
	        rm -f $DEVFILE && \
	        mknod -m 600 $DEVFILE c $PROC_MAJOR 0 && \
	        chown root:root $DEVFILE || {
	            rc_status -v
	            rc_exit
	        }
	    fi
	else
	    kill_avguard
	    mknod -m 600 $DEVFILE c $PROC_MAJOR 0 && \
	    chown root:root $DEVFILE || {
	        rc_status -v
	        rc_exit
	    }
	fi
	rc_status -v -r

	echo -n "  starting avguard"
	pids=$(find_avguard)
	test -n "$pids" || $DAEMON --$AVGUARD_TYPE &>/dev/null || rc_failed
	rc_status -v
	;;
    stop)
	echo "Shutting down AntiVir on-access scanning facility: avguard"
	echo -n "  shutting down avguard"
	kill_avguard
        rc=$?
	rc_status -v

	echo -n "  removing $DEVNAME kernel module"
        if test -n "$DAZUKO_MODULE" ; then
	    MODULE=$(basename $DAZUKO_MODULE)
	    MODULE=${MODULE%.*o}
        else
            MODULE=""
        fi
	if test -n "$MODULE" ; then
	    if lsmod | grep -qse "^${MODULE}" ; then
	        rmmod -s $MODULE
	    fi
	    rc_status -v
	else
	    rc_status -s -r
	    rc_failed $rc
	fi
	;;
    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
	fi
	rc_status
	;;
    restart)
	$0 stop
	$0 start
	rc_status
	;;
    force-reload)
	$0 try-restart
	rc_status
	;;
    reload)
	rc_failed 3
	rc_status -v
	;;
    status)
	echo -n "Checking for service avguard"
	pids=$(find_avguard)
	test -n "$pids" || rc_failed 3
	rc_status -v
	;;
    probe)
	# not yet supported
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
esac

rc_exit
