#!/bin/sh
#
# Pre/post-installation setup of ravcore

PATH=/bin:/usr/bin:/sbin:/usr/sbin
PREFIX="/usr/local"
ETC_PATH="${PREFIX}/etc/rav"
CONFIG_FILE="${PREFIX}/etc/rav/rup.conf"
SAMPLE_CONFIG_FILE="${PREFIX}/share/examples/rav/etc/rup.conf"
_USER="ravms"
_GROUP="ravms"

# Function: set up RAV AntiVirus user account.
#
do_accts()
{
	if /usr/sbin/pw showgroup ${_GROUP} -q 1>/dev/null 2>/dev/null; then
		echo "-> Using group '${_GROUP}' to execute RAV."
	else
		echo "-> Creating '${_GROUP}' group (used to run RAV)..."
		/usr/sbin/pw addgroup ${_GROUP} -h - 1>/dev/null 2>/dev/null
	fi

	if /usr/sbin/pw showuser ${_USER} -q 1>/dev/null 2>/dev/null; then
		echo "-> Using account '${_USER}' to send warning mails."
	else
		echo "-> Creating '${_USER}' user (used to send warning mails)..."
		/usr/sbin/pw adduser ${_USER} -h - -d /nonexistent -s /nonexistent \
		-c "RAV AntiVirus for Mail Servers (FreeBSD)" \
		-g ${_GROUP} 1>/dev/null 2>/dev/null
	fi
}

# Function: configuration file for ravupdate
#
do_config()
{
	echo ""
	if [ -f ${CONFIG_FILE} ]; then
		echo "| The existing ravupdate configuration file,"
		echo "| ${CONFIG_FILE},"
		echo "| has NOT been changed."
		echo "| A new configuration sample can be found in the"
		echo "| ${SAMPLE_CONFIG_FILE} file."
	else
		# Install configuration file.
		mkdir -p ${ETC_PATH}
		cp ${SAMPLE_CONFIG_FILE} ${CONFIG_FILE}
		chown ${_USER}:${_GROUP} ${CONFIG_FILE}
		echo "| You can customize the updating process from"
		echo "| $CONFIG_FILE file."
	fi
}

# verify proper execution
#
if [ $# -ne 2 ]; then
	echo "usage: $0 distname { PRE-INSTALL | POST-INSTALL }" >&2
	exit 1
fi

# Verify/process the command
#
case $2 in
	PRE-INSTALL)
	do_accts
	;;
	POST-INSTALL)
	do_config
	;;
	*)
	echo "Usage: `basename $0` distname <PRE-INSTALL | POST-INSTALL>" >&2
	exit 1
	;;
esac

exit 0

