#!/bin/sh
# $OpenBSD: INSTALL,v 1.8 2003/05/13 22:53:58 sturm Exp $
#
# Pre/post-installation setup of openwebmail

# exit on errors, use a sane path and install prefix
#
PATH=/bin:/usr/bin:/sbin:/usr/sbin
PREFIX=${PKG_PREFIX:-/var/www}
CONFIG_DIR=/var/www/conf/openwebmail
CONFIG_FILE=$CONFIG_DIR/openwebmail.conf
CGI_DIR=/var/www/cgi-bin/openwebmail
SAMPLE_CONFIG_FILE=$CONFIG_FILE.default
SCRIPTFILES="openwebmail-abook.pl openwebmail-advsearch.pl openwebmail-cal.pl \
	    openwebmail-folder.pl openwebmail-main.pl openwebmail-prefs.pl \
	    openwebmail-read.pl openwebmail-send.pl openwebmail-spell.pl \
	    openwebmail-tool.pl openwebmail-vdomain.pl openwebmail-viewatt.pl \
	    openwebmail-webdisk.pl openwebmail.pl"

# Function: set up mail group 
#
do_accts()
{
    groupinfo -e mail
    if [ $? -eq 0 ]; then
	echo "===>  Using group 'mail' for openwebmail"
    else
	echo "===>  Creating mail group"
    groupadd -g 6 mail
    fi
}

# Function: tell the user what they need to do to use the port just installed
#
do_notice()
{
    echo
    echo "+---------------"
    echo "| The existing $1 configuration file,"
    echo "| $CONFIG_FILE,"
    echo "| has NOT been changed.  You may want to"
    echo "| compare it to the current sample file"
    echo "| $SAMPLE_CONFIG_FILE"
    echo "| and update your configuration as needed."
    echo "+---------------"
    echo
}

# Function: install configuration files
#
do_install()
{
    for file in $SCRIPTFILES
    do
	chown root:mail $CGI_DIR/$file
	chmod 4555 $CGI_DIR/$file
    done
    chmod 770 $CONFIG_DIR/sessions
    chmod 770 $CONFIG_DIR/users
    install -o root -g wheel -m 644 $SAMPLE_CONFIG_FILE $CONFIG_FILE
    echo "/var/www/cgi-bin/openwebmail/" | cat > $CONFIG_DIR/openwebmail_path.conf
    perl $CGI_DIR/openwebmail-tool.pl --init --no
    echo
    echo "+---------------"
    echo "| The $1 configuration file,"
    echo "| $CONFIG_FILE,"
    echo "| has been installed.  Please view this file and"
    echo "| change the configuration to meet your needs."
    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
	;;
    POST-INSTALL)
	if [ -f ${CONFIG_FILE} ]; then
	    do_notice "$1"
	else
	    do_install "$1"
	fi
	;;
    *)
	echo "usage: $0 distname { PRE-INSTALL | POST-INSTALL }" >&2
	exit 1
	;;
esac

exit 0
