#!/bin/sh
#
# Copyright (c) 1999-2008 Parallels
# All rights reserved
#

#
# Plesk script
#


p_echo()
{
[ -z "$product_log" ] || echo "$@" >> "$product_log" 2>&1
echo "$@"
}

simply_die()
{
    p_echo "$@" >&2
    exit 1
}

# -*- shell-script -*-
# configures Mailman to use a given mailserver
# Currently Postfix and QMail, both as "Manual" (see comments below)
set_mailman_mailsrv()
{
    local cfg server

    cfg=$1
    server=$2

    if [ -z "$cfg" ]; then
        simply_die "Usage: $0 /path/to/mm_cfg.py (qmail|postfix)"
    fi

    case "$server" in
        qmail)
            server="Manual"
            ;;
        postfix)
            # Although Mailman supports Postfix "directly" (i.e. it has
            # a code to control Postfix system aliases (typically, /etc/aliases)
            # we keep Mailman related system aliases in an additional hashed
            # database (hash:/var/spool/postfix/plesk/aliases)
            # (for various reasons), so we have to manage Mailman-specific alias
            # manually as with QMail
            server="Manual"
            ;;
        *)
            simply_die "Currently only qmail and postfix are supported"
            ;;
    esac

    if [ ! -f "$cfg" ]; then
        simply_die "Usage: no such file or directory \"$cfg\""
    fi

    #declare pymod pydir curmta
    local pydir pymod
    pydir=$(dirname "$cfg")
    [ -z "$pydir" ] && pydir="."
    pymod=$(basename "$cfg")
    pymod=${pymod%%.py}

    local python_bin
    python_bin="/usr/bin/python2.4"

    local currentServer
    currentServer=$(PYTHONPATH="${PYTHONPATH}:$pydir" "${python_bin}" -c "import $pymod; print $pymod.MTA")
    if [ $? -ne 0 ]; then
        simply_die "Unable to load current settings"
    fi

    if [ "x${currentServer}" = "x${server}" ]; then
        # nothing to do
        return 0
    fi

    local tmpcfg bakcfg

    bakcfg="${cfg}.saved_by_plesk"
    tmpcfg=$(mktemp "${cfg}.XXXXXX")
    if [ $? != 0 ]; then
        simply_die "Unable to creare a temporary file for ${cfg}"
    fi
    trap "mailman_trap_handler \"$tmpcfg\"" HUP PIPE INT TERM EXIT
    awk -v server="$server" 'BEGIN { found=0; }
/^[[:space:]]*MTA[[:space:]]*=/ {
    print "### ", $0;
    if (!found) {
        str=$0;
        sub(/=.*/, "='"'"'" server "'"'"'", str);
        $0=str;
        found=1;
    }
}
{ print $0; }
END {
    if (!found) {
        print "MTA='"'"'" server "'"'"'";
    };
    exit 0;
}' "$cfg" >>"$tmpcfg"
    rc=$?
    if [ 0 -eq $rc ]; then
        p_echo "updating configuration file $cfg"
        rm -f "$bakcfg" 2>/dev/null; # errors will be reported later
        ln "$cfg" "$bakcfg" &&             mv -f "$tmpcfg" "$cfg" || simply_die "unable to save new configuration"
        rm -f "$tmpcfg"
        trap - HUP PIPE INT TERM EXIT
    else
        simply_die "unable to process \"$cfg\""
    fi
    return 0
}

mailman_trap_handler()
{
    trap - EXIT
    rm -f -- "$1"
    exit
}
# -*- shell-script -*-
# A quick script to fix MTA configuration setting in
# Mailman configuration file /path/to/mm_cfg.py

# Usage: mailman_conf_init /path/to/mm_cfg.py
# Requirements: coreutils, awk, python

if ! set_mailman_mailsrv "$1" postfix; then
    simply_die "unable to configure Mailman to work with Postfix"
fi

# Hopefully, Postfix doesn't need mm_wrapper at all and
# thus don't need to do black magic with its permissions
