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

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

# Source clamav configuration.
SourceIfNotEmpty /etc/sysconfig/clamav

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

start()
{
	if [ ! -s /var/lib/clamav/viruses.db ]; then
		/etc/cron.daily/freshclam
	fi
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user mail -- clamd
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user mail clamd
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

reload()
{
	msg_reloading clamd
	stop_daemon --pidfile "$PIDFILE" --expect-user mail -HUP -- clamd
	RETVAL=$?
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  reload)
	reload
	;;
  restart)
	restart
	;;
  status)
	status --pidfile "$PIDFILE" --expect-user mail -- clamd
	RETVAL=$?
	;;
  condstop)
	if [ -e "$LOCKFILE" ]; then
		stop
	fi
	;;
  condrestart)
	if [ -e "$LOCKFILE" ]; then
		restart
	fi
	;;
  condreload)
	if [ -e "$LOCKFILE" ]; then
		reload
	fi
	;;
  *)
	msg_usage "${0##*/} {start|stop|restart|reload|condstop|condrestart|condreload|status}"
	RETVAL=1
esac

exit $RETVAL
