#!/bin/sh
# $OpenBSD: INSTALL,v 1.11 2003/11/13 05:56:00 kevlo 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"
OPENWEBMAILGROUP=_mail
ID=524

# Function: set up _mail group
#
do_accts()
{
    groupinfo -e $OPENWEBMAILGROUP
    if [ $? -eq 0 ]; then
	echo "===>  Using $OPENWEBMAILGROUP group for openwebmail"
    else
	echo "===>  Creating $OPENWEBMAILGROUP group for openwebmail"
    groupadd -g $ID $OPENWEBMAILGROUP
    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()
{
    chgrp -R $OPENWEBMAILGROUP $CGI_DIR/*
    for file in $SCRIPTFILES
    do
	chmod 4555 $CGI_DIR/$file
    done
    chmod 771 $CONFIG_DIR/{sessions,users}
    chgrp -R $OPENWEBMAILGROUP $CONFIG_DIR
    install -o root -g $OPENWEBMAILGROUP -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
}

do_warning()
{
    echo "+---------------"
    echo "| WARNING!"
    echo "|"
    echo "| This port has changed its user from 'mail' to '_mail'. If you"
    echo "| installed this port before, ensure all permissions are set"
    echo "| correctly and then \"groupdel mail\"."
    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
	do_warning
	;;
    *)
	echo "usage: $0 distname { PRE-INSTALL | POST-INSTALL }" >&2
	exit 1
	;;
esac

exit 0
