#!/bin/sh
#
# postfix plugin
# Author: Shun-ichi TAHARA <jado@flowernet.gr.jp>
# Time-stamp: <04/10/21 02:02:13 jado@sheira>
#
# Copyright (c) 2004 Shun-ichi TAHARA
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#

# *** Please change below as suitable ***
POSTFIXCONF=/etc/postfix/main.cf

# Additional settings going into /etc/network.conf:
#
# SMTPOUTER : Outer SMTP server  [+ (from DHCP)|mx|server]
# SMTPHELO  : SMTP HELO nickname [hosename]

POSTFIXRESOLV=/etc/resolv.conf
POSTFIXSUM=$PLUGINSTATE/postfix.resolv.md5
POSTFIXDEV=$PLUGINSTATE/postfix.devices

postfix_check ()
{
    if [ -s $POSTFIXDEV ]; then
	if [ -s $POSTFIXDEV.N ]; then
	    echo "online"
	else
	    echo "turnoff"
	fi
    else
	if [ -s $POSTFIXDEV.N ]; then
	    echo "turnon"
	else
	    echo "offline"
	fi
    fi
}

postfix_update ()
{
    local CHECK= RELOAD= FLUSH=

    if [ ! -x /usr/sbin/postfix -o ! -r $POSTFIXCONF ]; then
	echo "Please install and setup Postfix to work with SMTPOUTER."
	return
    fi

    case "$1" in
    turnon)
	CHECK=y
	RELOAD=y
	FLUSH=y
	;;
    online)
	CHECK=y
	RELOAD=y
	;;
    turnoff)
	RELOAD=y
	;;
    esac

    /usr/bin/sed -e '/# The rest is added by Planet.../,$d' \
	$POSTFIXCONF > $POSTFIXCONF.N
    echo "# The rest is added by Planet..." >> $POSTFIXCONF.N

    cat $POSTFIXDEV | /usr/bin/awk '
	BEGIN{
	    helo = "";
	    smtp = "";
	}
	/^helo +[^ ]+/{
	    if (helo == "") {
		helo = $2
	    }
	}
	/^smtp +[^ ]+/{
	    if (tolower($2) == "mx" || smtp == "") {
		smtp = $2;
	    }
	}
	END{
	    if (helo != "") {
		printf("smtp_helo_name = %s\n", helo);
	    }
	    if (smtp == "") {
		printf("defer_transports = smtp\n");
	    } else if (tolower(smtp) != "mx") {
		printf("relayhost = %s\n", smtp);
	    }
	}
    ' >> $POSTFIXCONF.N

    mv $POSTFIXCONF.N $POSTFIXCONF

    if /usr/libexec/postfix/master -t > /dev/null 2>&1; then
	/usr/bin/md5sum $POSTFIXRESOLV > $POSTFIXSUM
	/usr/sbin/postfix start > /dev/null 2>&1
	sleep 1
	CHECK=
	RELOAD=
    fi

    if [ -n "$CHECK" ]; then
	if [ ! -r $POSTFIXSUM ] ||
	   ! /usr/bin/md5sum --status --check $POSTFIXSUM; then
	    /usr/bin/md5sum $POSTFIXRESOLV > $POSTFIXSUM
	    /usr/sbin/postfix stop > /dev/null 2>&1
	    /usr/sbin/postfix start > /dev/null 2>&1
	    sleep 1
	    RELOAD=
	fi
    fi

    if [ -n "$RELOAD" ]; then
	/usr/sbin/postfix reload > /dev/null 2>&1
    fi

    if [ -n "$FLUSH" ]; then
	/usr/sbin/postfix flush > /dev/null 2>&1 &
    fi
}

# Variables definition

postfix_VARS="SMTPOUTER:smtp_server SMTPHELO"

# Script on starting

postfix_start ()
{
    touch $POSTFIXDEV
    /usr/bin/sed -e "/# $1 begin/,/# $1 end/d" $POSTFIXDEV > $POSTFIXDEV.N

    if [ -n "$SMTPOUTER" ]; then
	echo "# $1 begin" >> $POSTFIXDEV.N
	echo "smtp $SMTPOUTER" >> $POSTFIXDEV.N
	if [ -n "$SMTPHELO" ]; then
	    echo "helo $SMTPHELO" >> $POSTFIXDEV.N
	fi
	echo "# $1 end" >> $POSTFIXDEV.N
    fi

    local MODE=`postfix_check`
    mv $POSTFIXDEV.N $POSTFIXDEV
    postfix_update $MODE
}

# Script on stopping

postfix_stop ()
{
    /usr/bin/sed -e "/# $1 begin/,/# $1 end/d" $POSTFIXDEV > $POSTFIXDEV.N

    local MODE=`postfix_check`
    mv $POSTFIXDEV.N $POSTFIXDEV
    postfix_update $MODE
}
