#!/bin/sh /etc/rc.common
# Copyright (C) 2015 OpenWrt.org

START=90
STOP=10

USE_PROCD=1
PROG=/usr/sbin/clamav-milter
CLAMAV_MILTER_CONFIGFILE="/tmp/clamav/clamav-milter.conf"

validate_clamav_milter_section() {
	uci_load_validate clamav-milter clamav_milter "$1" "$2" \
		'clamav_milter_config_file:string' \
		'Foreground:string' \
		'PidFile:string' \
		'User:string' \
		'MilterSocketGroup:string' \
		'ReadTimeout:uinteger' \
		'OnClean:string' \
		'OnInfected:string' \
		'OnFail:string' \
		'AddHeader:string' \
		'LogVerbose:string' \
		'LogTime:string' \
		'LogSyslog:string' \
		'Debug:string' \
		'LogFacility:string' \
		'LogInfected:string' \
		'LogClean:string' \
		'MaxFileSize:string' \
		'SupportMultipleRecipients:string' \
		'RejectMsg:string' \
		'TemporaryDirectory:string' \
		'MilterSocket:string' \
		'MilterSocketMode:uinteger' \
		'ClamdSocket:string' \
		'FixStaleSocket:string'
}

start_clamav_milter_instance() {
	[ "$2" = 0 ] || {
		echo "validation failed"
		return 1
	}

	mkdir -p /etc/clamav/
	mkdir -p /var/run/clamav/
	chmod a+rw /var/run/clamav

	mkdir -p "$(dirname $CLAMAV_MILTER_CONFIGFILE)"
	ln -sf "$clamav_milter_config_file" "$CLAMAV_MILTER_CONFIGFILE"

	{
	[ -n "$Foreground" ]				&& echo "Foreground " "$Foreground"
	[ -n "$PidFile" ]					&& echo "PidFile " "$PidFile"
	[ -n "$User" ]						&& echo "User " "$User"
	[ -n "$MilterSocketGroup" ]			&& echo "MilterSocketGroup " "$MilterSocketGroup"
	[ -n "$ReadTimeout" ]				&& echo "ReadTimeout " "$ReadTimeout"
	[ -n "$OnClean" ]					&& echo "OnClean " "$OnClean"
	[ -n "$OnInfected" ]				&& echo "OnInfected " "$OnInfected"
	[ -n "$OnFail" ]					&& echo "OnFail " "$OnFail"
	[ -n "$AddHeader" ]					&& echo "AddHeader " "$AddHeader"
	[ -n "$LogVerbose" ]				&& echo "LogVerbose " "$LogVerbose"
	[ -n "$LogTime" ]					&& echo "LogTime " "$LogTime"
	[ -n "$LogSyslog" ]					&& echo "LogSyslog " "$LogSyslog"
	[ -n "$Debug" ]						&& echo "Debug " "$Debug"
	[ -n "$LogFacility" ]				&& echo "LogFacility " "$LogFacility"
	[ -n "$LogInfected" ]				&& echo "LogInfected " "$LogInfected"
	[ -n "$LogClean" ]					&& echo "LogClean " "$LogClean"
	[ -n "$MaxFileSize" ]				&& echo "MaxFileSize " "$MaxFileSize"
	[ -n "$SupportMultipleRecipients" ]	&& echo "SupportMultipleRecipients " "$SupportMultipleRecipients"
	[ -n "$RejectMsg" ]					&& echo "RejectMsg " "$RejectMsg"
	[ -n "$TemporaryDirectory" ]		&& echo "TemporaryDirectory " "$TemporaryDirectory"
	[ -n "$MilterSocket" ]				&& echo "MilterSocket " "$MilterSocket"
	[ -n "$MilterSocketMode" ]			&& echo "MilterSocketMode " "$MilterSocketMode"
	[ -n "$ClamdSocket" ]				&& echo "ClamdSocket " "$ClamdSocket"
	[ -n "$FixStaleSocket" ]			&& echo "FixStaleSocket " "$FixStaleSocket"
	} > "$CLAMAV_MILTER_CONFIGFILE"

	procd_open_instance
	procd_set_param command $PROG -c $CLAMAV_MILTER_CONFIGFILE
	procd_set_param file $CLAMAV_MILTER_CONFIGFILE
	procd_close_instance
}

start_service()
{
	validate_clamav_milter_section clamav_milter start_clamav_milter_instance
}

stop_service()
{
	service_stop $PROG
}

service_triggers()
{
	procd_add_reload_trigger "clamav-milter"
	procd_add_validation validate_clamav_milter_section
}
