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

PATH=/bin:/usr/bin:/sbin:/usr/sbin
PREFIX="/usr/local"
BIN_PATH="${PREFIX}"
ETC_PATH="${PREFIX}/etc/rav"
DATA_PATH="/var/rav"

CONFIG_FILE="${ETC_PATH}/ravmd.conf"
KEY_FILE="${DATA_PATH}/ravmd.key"
CONTROL_FILE="${PREFIX}/etc/rc.d/ravmail.sh"

SAMPLE_CONFIG_FILE="${PREFIX}/share/examples/rav/etc/ravmd.conf"
SAMPLE_KEY_FILE="${DATA_PATH}/ravmd.key.new"
SAMPLE_CONTROL_FILE="${PREFIX}/share/examples/rav/ravmail.sh"
SAMPLE_CONFIG_DIR="${PREFIX}/share/examples/rav/etc"

RUN_DIR="${DATA_PATH}/run"
TMP_DIR="${DATA_PATH}/tmp"
QUART_DIR="${DATA_PATH}/quarantine"
LOG_DIR="${DATA_PATH}/log"
BULK_DIR="${DATA_PATH}/bulk"

DAEMON_FILE="${BIN_PATH}/bin/ravmd"

_USER="ravms"
_GROUP="ravms"

# Function: set up the ravmd working directories
#
do_dirs()
{
	echo "-> Creating working directories ..."
	echo " ${RUN_DIR}"
	[ -d ${RUN_DIR} ] || install -d -o ${_USER} -g ${_GROUP} -m 0755 ${RUN_DIR}
	echo " ${TMP_DIR}"
	[ -d ${TMP_DIR} ] || install -d -o ${_USER} -g ${_GROUP} -m 0750 ${TMP_DIR}
	echo " ${QUART_DIR}"
	[ -d ${QUART_DIR} ] || install -d -o ${_USER} -g ${_GROUP} -m750 ${QUART_DIR}
	echo " ${LOG_DIR}"
	[ -d ${LOG_DIR} ] || install -d -o ${_USER} -g ${_GROUP} -m755 ${LOG_DIR}
	echo " ${BULK_DIR}"
	[ -d ${BULK_DIR} ] || install -d -o ${_USER} -g ${_GROUP} -m750 ${BULK_DIR}
}

# Function: install ravmd's configuration file from the samples
#
do_configs()
{
	echo ""
	if [ -f ${CONFIG_FILE} ]; then
		echo "| The existing $1 configuration file,"
		echo "| ${CONFIG_FILE},"
		echo "| has NOT been changed."
		echo "| A new configuration sample can be found in the"
		echo "| ${SAMPLE_CONFIG_DIR} directory, and in the"
		echo "| ${SAMPLE_CONFIG_FILE} file."
	else
		# Install configuration file.
		cp -R ${SAMPLE_CONFIG_DIR}/* ${ETC_PATH}/ >/dev/null
		chown -R ${_USER}:${_GROUP} ${ETC_PATH} >/dev/null
		echo "| You can configure $1 from"
		echo "| $CONFIG_FILE file."
		echo "| Please view this file and change the configuration to meet your needs."
	fi

	#trying to add the domains scanned by RAV
	count=`cat ${ETC_PATH}/domains | grep -c -i "^domain"`
	if [ $count -eq 0 ]; then
		machine=`hostname`
		test=`echo $machine | cut -f2 -d"."`
			if [ $test"x" = "x" ]; then
			machine=`hostname``domainname`
		fi
		domain=`echo $machine | cut -f2- -d"."`
		echo "domain = $machine, $domain" >> ${ETC_PATH}/domains
	fi
}

# Function: install ravmd's control file from the samples
#
do_activate()
{
	echo ""
	if [ -f $CONTROL_FILE ]; then
		echo "| The existing $1 control file,"
		echo "| $CONTROL_FILE,"
		echo "| has NOT been changed."
		echo "| A new control script sample can be found in"
		echo "| $SAMPLE_CONTROL_FILE."
	else
		# Install ravmd's control script.
		install -c -o root -g wheel -m 0755 ${SAMPLE_CONTROL_FILE} ${CONTROL_FILE}
		echo "| You can start/stop $1 using"
		echo "| $CONTROL_FILE script."
	fi

	#initialize the evaluation key
	if [ ! -f ${KEY_FILE} ]; then
		mv -f ${SAMPLE_KEY_FILE} ${KEY_FILE}
	fi 
}

# Function: tell the user what s/he needs to do to use the port just installed
#
do_notice()
{
	echo ""
	echo "| The RAV mail scanning daemon starts with:"
	echo "|	$DAEMON_FILE"
	echo "| or"
	echo "|	$CONTROL_FILE start"
	echo "-> Starting ravmd ..."
	${DAEMON_FILE}
	sleep 1
	cp -f ${KEY_FILE} ${SAMPLE_KEY_FILE}
}

# 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)
	;;
	POST-INSTALL)
	do_dirs
	do_configs $1
	do_activate $1
	do_notice
	echo ""
	;;
	*)
	echo "Usage: `basename $0` distname <PRE-INSTALL|POST-INSTALL>" >&2
	exit 1
	;;
esac

exit 0
