#!/bin/sh
#
# clamonacc	Startup script for the Clam On-Access Scanner
#
# chkconfig:	2345 76 24
# description:	Clam AntiVirus Daemon is a TCP/IP or socket protocol \
#		server.
# processname:	clamd
# config:	/etc/clamav/clamd.conf
# pidfile:	/var/run/clamav/clamonacc.pid

### BEGIN INIT INFO
# Provides:          clamonacc
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:
# Default-Stop:
# Short-Description: Start clamobacc at boot time
# Description:       Start Clam On-Access Scanner at boot time
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

# Source clamd configuration.
SourceIfNotEmpty /etc/sysconfig/clamonacc

PIDFILE=/var/run/clamav/clamonacc.pid
LOCKFILE=/var/lock/subsys/clamonacc
RETVAL=0

dbcheck()
{
	if [ ! -s /var/lib/clamav/main.cvd -a ! -s /var/lib/clamav/main.cld -a ! -d /var/lib/clamav/main.inc -a ! -d /var/lib/clamav/daily.inc ]; then
		/usr/bin/freshclam
	fi
}

start()
{
	# create directory if needed (if tmpfs used)
	[ -d "/var/run/clamav" ] || {
	    mkdir "/var/run/clamav"
	    chown root:mail "/var/run/clamav"
	    chmod 775 "/var/run/clamav"
	}

	dbcheck
	start_daemon --expect-user root -- clamonacc --log=/var/log/clamav/clamonacc.log
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --expect-user root clamonacc
	RETVAL=$?
	return $RETVAL
}

restart()
{
	dbcheck
	stop
	sleep 1
	start
}

reload()
{
	dbcheck
	msg_reloading clamonacc
	stop_daemon --expect-user root -HUP -- clamonacc
	RETVAL=$?
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  reload)
	reload
	;;
  restart)
	restart
	;;
  update)
	/usr/bin/freshclam
	;;
  status)
	status --expect-user root -- clamonacc
	RETVAL=$?
	;;
  condstop)
	stop
	;;
  condrestart)
	restart
	;;
  condreload)
	reload
	;;
  *)
	msg_usage "${0##*/} {start|stop|restart|reload|condstop|condrestart|condreload|status|update}"
	RETVAL=1
esac

exit $RETVAL
