#!/bin/bash
### Copyright 1999-2023. Plesk International GmbH. All rights reserved.

set -e

MAIL_HANDLERS_CONTROL=/opt/psa/admin/bin/mail_handlers_control
PLESKRC=/opt/psa/admin/sbin/pleskrc
HANDLER_PRIORITY=20
HANDLER_EXECUTABLE="/opt/plesk/sophosav/bin/sophos-handler"
SOPHOS_SERVICE="plesk-sophos-av.service"
SOPHOS_SERVICE_NAME="${SOPHOS_SERVICE%%.service}"
SOPHOS_UPDATER_TIMER_NAME="plesk-sophos-av-updater.timer"
SOPHOS_UPDATER_ENV_FILE="/opt/plesk/sophosav/savdi/plesk-sophos-av-updater.env"

service_start() {
	$PLESKRC $SOPHOS_SERVICE_NAME start
}

service_stop() {
	$PLESKRC $SOPHOS_SERVICE_NAME stop || :
}

service_restart() {
	$PLESKRC $SOPHOS_SERVICE_NAME restart || :
}

service_status() {
	if $PLESKRC $SOPHOS_SERVICE_NAME status > /dev/null 2>&1; then
		echo "is running"
	else
		echo "is stopped"
	fi
}

updates_on () {
	systemctl enable "$SOPHOS_UPDATER_TIMER_NAME"
	systemctl start  "$SOPHOS_UPDATER_TIMER_NAME"
}

updates_off() {
	systemctl disable "$SOPHOS_UPDATER_TIMER_NAME"
	systemctl stop    "$SOPHOS_UPDATER_TIMER_NAME"
}

set_updates_url() {
	sed -n -e '/^SOPHOS_UPDATER_BASE_URL=/!p' -e '$aSOPHOS_UPDATER_BASE_URL='$1 $SOPHOS_UPDATER_ENV_FILE > $SOPHOS_UPDATER_ENV_FILE.temp
	[ $? == 0 ] && mv $SOPHOS_UPDATER_ENV_FILE.temp $SOPHOS_UPDATER_ENV_FILE
}

service_off() {
	service_stop
	${MAIL_HANDLERS_CONTROL} --remove --name=sophos-snd  --type=sender    --queue=before-remote
	${MAIL_HANDLERS_CONTROL} --remove --name=sophos-rcpt --type=recipient --queue=before-queue
	updates_off
	/opt/psa/admin/sbin/register_service --disable $SOPHOS_SERVICE_NAME
}

service_on() {
	updates_off
	[ -n "$1" ] && set_updates_url "$1"
	updates_on
	/opt/psa/admin/sbin/register_service --enable $SOPHOS_SERVICE_NAME
	service_start
}

mailname_add() {
	local ht=$1
	local mailname=$2
	case $ht in
		incoming|outgoing|both) ;;
		*) echo "Bad direction $ht"; usage;;
	esac

	[ -n "$mailname" ] || usage

	mailname_remove $mailname

	if [ "$ht" != "incoming" ]; then
		${MAIL_HANDLERS_CONTROL} --add --name=sophos-snd  --type=sender    --mailname=$mailname --priority=${HANDLER_PRIORITY} \
			--queue=before-remote --executable=${HANDLER_EXECUTABLE} --enabled --dont-preserve-on-restore
	fi

	if [ "$ht" != "outgoing" ]; then
		${MAIL_HANDLERS_CONTROL} --add --name=sophos-rcpt --type=recipient --mailname=$mailname --priority=${HANDLER_PRIORITY} \
			--queue=before-queue  --executable=${HANDLER_EXECUTABLE} --enabled --dont-preserve-on-restore
	fi
}

mailname_remove() {
	mailname=$1
	${MAIL_HANDLERS_CONTROL} --remove --name=sophos-snd  --type=sender    --mailname=${mailname} --queue=before-remote
	${MAIL_HANDLERS_CONTROL} --remove --name=sophos-rcpt --type=recipient --mailname=${mailname} --queue=before-queue
}

usage() {
	echo "Usage:
$0 command, where command is one of:
	on [<url>]			-- Turn Sophos service on with an optional updates URL
	off 				-- Turn Sophos service off
	start				-- Start Sophos service
	stop				-- Stop Sophos service
	restart				-- Restart Sophos service
	status				-- Show status of Sophos service
	add-updates			-- Add periodic task for Sophos updates
	del-updates 		-- Remove periodic task for Sophos updates
	set-updates-url [<url>]			-- Set updates URL; no URL parameter unsets it
	add [incoming|outgoing|both] mailname	-- Enable anti-virus check for the mailname
	remove mailname		-- Disable anti-virus checks for the mailname
	usage
	help				-- Display this help
"
exit 2
}

exec 2>&1 # w/a Plesk util_exec function which gets only stdout, and looses stderr.

cmd=$1

[ -n "$cmd" ] || usage

cmd=${cmd#--}

case $cmd in
	on)			service_on $2 ;;
	off)		service_off ;;
	add)		mailname_add $2 $3 ;;
	add-updates)	updates_on ;;
	del-updates)	updates_off ;;
	set-updates-url)	set_updates_url $2 ;;
	remove)		mailname_remove $2 ;;
	start)		service_start ;;
	restart)	service_restart ;;
	stop)		service_stop ;;
	status)		service_status ;;
	help|usage) 	usage;;
	*)		echo "Bad command $cmd"; usage;;
esac
