#!/bin/sh
# $OpenBSD: INSTALL,v 1.1.1.1 2001/08/29 02:13:06 shell Exp $
#
# Pre/post-installation setup of mailman

# use a sane path and install prefix
#
PATH=/bin:/usr/bin:/sbin:/usr/sbin
PREFIX=${PKG_PREFIX:-/usr/local}

MMEXAMPLES=${PREFIX}/share/examples/mailman
MMHOME=${PREFIX}/lib/mailman
MMSPOOL=/var/spool/mailman

# add user/group mailman if they don't already exist
do_accts()
{
	groupinfo -e mailman
	if [ $? -eq 0 ]; then
		echo "===>  Using existing group 'mailman'"
	else
		echo "===>  Creating group 'mailman'"
		groupadd mailman
	fi

	userinfo -e mailman
	if [ $? -eq 0 ]; then
		echo "===>  Using existing account 'mailman'"
	else
		echo "===>  Creating user 'mailman'"
		useradd -g mailman \
			-c "Mailing List Manager" \
			-m -d $MMHOME \
			-s /sbin/nologin \
			-p \* \
			mailman
	fi        
}

# create installation directories
do_dirs()
{
	INSTALL_DIR="install -d -o mailman -g mailman -m 0775"

	if [ ! -d $MMSPOOL ]; then
		$INSTALL_DIR $MMSPOOL
	fi

	cd $MMSPOOL
	for dir in archives archives/private archives/public data \
		   filters lists locks logs qfiles spam; do
		if [ ! -d $dir ]; then
			$INSTALL_DIR $dir
		fi
	done
	chmod 0771 archives/private

	cd $MMHOME
	for dir in Mailman bin cgi-bin cron icons mail scripts \
		   templates; do
		if [ ! -d $dir ]; then
			$INSTALL_DIR $dir
		fi
	done

	cd Mailman
	for dir in Archiver Bouncers Cgi Handlers Logging pythonlib; do
		if [ ! -d $dir ]; then
			$INSTALL_DIR $dir
		fi
	done
}

do_files()
{
	INSTALL="install -o mailman -g mailman -m 664"

	if [ ! -f $MMSPOOL/filters/bowa-strip ]; then
	    $INSTALL $MMEXAMPLES/bowa-strip $MMSPOOL/filters;
	fi
	if [ ! -f $MMSPOOL/data/pending_subscriptions.db ]; then
	    $INSTALL $MMEXAMPLES/pending_subscriptions.db $MMSPOOL/data;
	fi
	if [ ! -f $MMHOME/Mailman/mm_cfg.py ]; then
	    $INSTALL $MMHOME/Mailman/mm_cfg.py.dist $MMHOME/Mailman/mm_cfg.py;
	fi
}

do_perms()
{
	cd ${MMHOME} 
	chown -R mailman:mailman *
        for file in mail/wrapper cgi-bin/*; do
                chmod 2755 $file;
        done
}

do_notice()
{
	echo ""
	echo "+---------------"
	echo "| The $1 package needs quite some configuration. Have"
	echo "| a look at $PREFIX/share/doc/mailman/README.OpenBSD to find out"
	echo "| what you have to do, to get mailman up and running."
	echo "+---------------"
	echo ""
}

# 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 $1
	do_dirs $1
	;;
    POST-INSTALL)
	do_files $1
	do_perms $1
	do_notice $1
	;;
    *)
	echo "usage: $0 distname { PRE-INSTALL | POST-INSTALL }" >&2
	exit 1
	;;
esac

exit 0
