#!/bin/sh
# $OpenBSD: INSTALL,v 1.3 2003/06/22 10:05:29 sturm 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}

MMHOME=${PREFIX}/lib/mailman
MMSPOOL=/var/spool/mailman
MM=_mailman
ID=504

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

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

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

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

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

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

	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 $MM:$MM *
	find . -type d -exec chmod 0775 {} \;
	for file in mail/mailman cgi-bin/*; do
		chmod 2755 $file;
	done
}

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

exit 0
