#!/sbin/runscript

extra_started_commands="reload"
extra_commands="logfix"

command=/usr/bin/freshclam
pidfile=/var/run/clamav/freshclam.pid
CONF=/etc/clamav/freshclam.conf

depend() {
	need net
	after firewall
}

start() {
	ebegin "Starting freshclam"
	start-stop-daemon --start --quiet \
		--nicelevel ${FRESHCLAM_NICELEVEL:-0} \
		--exec $command \
		-- \
		--daemon \
		--pid=$pidfile
	eend $?
}

stop() {
	ebegin "Stopping freshclam"
	start-stop-daemon --stop --quiet --pidfile $pidfile
	eend $?
}

reload() {
	ebegin "Reloading freshclam"
	start-stop-daemon --stop --signal HUP \
		--exec $command --pidfile $pidfile
	eend $?
}


logfix() {
	# fix freshclam log permissions
	# (might be clobbered by logrotate or something)
	logfile=$(awk '$1 == "UpdateLogFile" { print $2 }' $CONF)
	local freshclam_user=$(awk '$1 == "DatabaseOwner" { print $2 }' $CONF)
	if [ -n "${logfile}" -a -n "${clamav_user}" ]; then
		if [ ! -f "${logfile}" ]; then
			touch ${logfile}
		fi	
		chown ${freshclam_user} ${logfile}
		chmod 640 ${logfile}
	fi
}