#!/bin/sh
# $OpenBSD: postfix-install,v 1.1 2007/05/31 10:19:00 espie Exp $
#
# Post-installation setup of postfix

PATH=/bin:/usr/bin:/sbin:/usr/sbin
CONFIG_DIR=${SYSCONFDIR}/postfix
SAMPLE_CONFIG_DIR=${PREFIX}/share/examples/postfix
SPOOL_DIR=/var/spool/postfix

# Function: set up the postfix spool dir / chroot area
#
do_spooldir()
{
    echo "-> Creating Postfix spool directory and chroot area under $SPOOL_DIR"
    [ -d $SPOOL_DIR ] || install -d -o root -g wheel -m 755 $SPOOL_DIR
    [ -d $SPOOL_DIR/etc ] || install -d -o root -g wheel -m 755 $SPOOL_DIR/etc
    [ -d $SPOOL_DIR/dev ] || install -d -o root -g wheel -m 755 $SPOOL_DIR/dev
    for file in localtime services resolv.conf ; do
	install -o root -g wheel -m 644 /etc/$file $SPOOL_DIR/etc
    done
}

# Function: replace sendmail binaries with postfix
#
do_mailwrapper()
{
    echo "-> Creating /etc/mailer.conf.postfix"
    cat <<MAILER  | sed "s@y0y0y0@$PREFIX@g" >/etc/mailer.conf.postfix
sendmail	y0y0y0/sbin/sendmail
send-mail	y0y0y0/sbin/sendmail
mailq		y0y0y0/sbin/sendmail
newaliases	y0y0y0/sbin/sendmail
MAILER
    chown root:wheel /etc/mailer.conf.postfix
    chmod 644 /etc/mailer.conf.postfix
}

# Function: update postfix scripts
#
update_scripts()
{
    rm -f $CONFIG_DIR/{postfix-script,postfix-files,post-install}
    install -o root -g wheel -m 544 \
	$SAMPLE_CONFIG_DIR/{postfix-script,post-install} $CONFIG_DIR
    install -o root -g wheel -m 444 \
	$SAMPLE_CONFIG_DIR/postfix-files $CONFIG_DIR
}

# Function: install the postfix configuration files from the samples
#
do_configs()
{
    PINSTALL="${CONFIG_DIR}/post-install command_directory=${PREFIX}/sbin"

    if [ -d $CONFIG_DIR ]; then
	update_scripts

	echo ""
	$PINSTALL upgrade-configuration
	echo ""

	echo "+---------------"
	echo "| The existing configuration files in $CONFIG_DIR have been preserved."
	echo "| You may want to compare them to the current sample files,"
	echo "| $SAMPLE_CONFIG_DIR, and update your configuration as needed."
	echo "|"
	echo "| $CONFIG_DIR/post{fix-{script,files},-install} has been updated."
    	echo "+---------------"
	echo ""
    else
	install -d -o root -g wheel -m 755 $CONFIG_DIR
	install -o root -g wheel -m 644 $SAMPLE_CONFIG_DIR/* $CONFIG_DIR

	update_scripts

	$PINSTALL first-install-reminder

	echo "+---------------"
	echo "| Configuration files have been installed in $CONFIG_DIR."
	echo "| Please update these files to meet your needs."
	echo "+---------------"
    fi

    $PINSTALL set-permissions create-missing
}

do_mailwrapper
do_spooldir
do_configs
