#!/bin/sh
# $OpenBSD: INSTALL,v 1.1.1.1 2004/04/23 15:24:54 robert Exp $
#
# Pre/post-installation setup of amavisd-new

PATH=/bin:/usr/bin:/sbin:/usr/sbin
PREFIX=${PKG_PREFIX:-/usr/local}
CONFIG_DIR=${SYSCONFDIR}
WORK_DIR=/var/amavisd
USER=_vscan
GROUP=_vscan
ID=530

# Function: set up amavisd user/group accounts.
#
do_accts()
{
    groupinfo -e ${GROUP}
    if [ $? -eq 0 ]; then
	echo "===> Using ${GROUP} group for amavisd-new"
    else
	echo "===> Creating ${GROUP} group for amavisd-new"
	groupadd -g ${ID} ${GROUP}
    fi

    userinfo -e ${USER}
    if [ $? -eq 0 ]; then
	echo "===> Using ${USER} user for amavisd-new"
    else
	echo "===> Creating ${USER} user for amavisd-new"
	useradd \
	    -g ${GROUP} \
	    -c "Amavisd-new Daemon" \
	    -d /var/empty \
	    -s /sbin/nologin \
	    -u ${ID} ${USER}
    fi
}

# Function: install the amavisd-new configuration files from the samples
#
do_configs()
{
    if [ -f $CONFIG_DIR/amavisd.conf ]; then

	echo ""

	echo "+---------------"
	echo "| The existing configuration file $CONFIG_DIR/amavisd.conf has been preserved."
	echo "| You may want to compare it to the current sample files,"
	echo "| ${PREFIX}/share/doc/amavisd-new/ , and update your configuration as needed."
    	echo "+---------------"
	echo ""
    else
	install -o root -g wheel -m 644 ${PREFIX}/share/doc/amavisd-new/amavisd.conf $CONFIG_DIR

	echo "+---------------"
	echo "| Configuration file has been installed to $CONFIG_DIR/amavisd.conf"
	echo "| Please update this file to meet your needs."
	echo "+---------------"
    fi

    if [ -d ${WORK_DIR} ]; then

	echo ""
	echo "+---------------"
	echo "| The working directory ${WORK_DIR} has been preserved."
	echo "+---------------"
	echo ""
    else
	install -o ${USER} -g ${GROUP} -m 755 -d ${WORK_DIR}

	echo "+---------------"
	echo "| Work directory ${WORK_DIR} has been created."
	echo "| amavisd-new will use this as a staging area, and for "
	echo "| its temporary files."
	echo "+---------------"
	echo ""
    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_configs $1
	;;
    *)
	echo "Usage: `basename $0` distname <PRE-INSTALL|POST-INSTALL>" >&2
	exit 1
	;;
esac

exit 0
