#!/bin/bash 
# $Id: setmailrelay,v 1.12 2004/10/05 20:47:22 andrew Exp $
#
# This script was written originally by Jos Visser http://www.josv.com/~josv/
# and I have modified it a little to make it slightly more generic
#    - Andrew McMillan 10 August 1999
# Postfix support by Chris Halls, August 2001
# Exim support by Auke Jilderda, October 2002
# qmail support by Patrice Neff, October 2003
# exim4 support by Itay Ben-Yaacov, April 2004
#
# 2003-12-09 Chris Halls  Use postconf for postfix configuration
#
# Usage:
#   setmailrelay none   
#       - You have a permanent internet connection, do not use a mail relay
#   setmailrelay <relay>
#       - Relay to specified host
#   setmailrelay queue [<relay>]
#       - Do not send, place on the queue.  When the queue is flushed, mail
#         will be relayed to <relay>, or $DEFAULT_RELAY if not specified


# Where are the config files for your mailer?  Can override in mail-relay.conf
SENDMAIL_CONFFILE=/etc/mail/sendmail.cf
POSTFIX_CONFFILE=/etc/postfix/main.cf
EXIM_CONFFILE=/etc/exim/exim.conf
EXIM4_CONFFILE=/etc/exim4/update-exim4.conf.conf
QMAIL_CONFDIR=/etc/qmail/

# Where we get MAIL_SERVER and DEFAULT_RELAY from
. /etc/whereami/mail-relay.conf

RELAY="${1:-none}"
QUEUE_RELAY="${2:-${DEFAULT_RELAY}}"

# check for a "parameter=" line in a file, add it if it does not exist
#    check_for_line(file, parameter)
check_for_parameter()
{
    FILE="$1"
    PARAM="$2"

    if egrep -q "^$PARAM *=" "$FILE" ; then :
    else
        echo "$PARAM=" >> "$FILE"
    fi
}

if [ "$MAIL_SERVER" = "" ]; then
  # Go for some reasonable choices
  if [ -f $SENDMAIL_CONFFILE ] ; then
    MAIL_SERVER=sendmail
  elif [ -f $POSTFIX_CONFFILE ] ; then
    MAIL_SERVER=postfix
  elif [ -f $EXIM_CONFFILE ] ; then
    MAIL_SERVER=exim
  elif [ -f $EXIM4_CONFFILE ] ; then
    MAIL_SERVER=exim4
# Only supports split configuration
  elif [ -d $QMAIL_CONFDIR ] ; then
    MAIL_SERVER=qmail
  else
    echo "Unsupported mail server configuration check /etc/whereami/mail-relay.conf"
    echo "or advise awm@debian.org, hopefully including a patch!"
    exit 1
  fi
fi

echo "Setting mail relay host ($RELAY)" 


case "$MAIL_SERVER" in
  sendmail)
        /etc/init.d/sendmail stop

        if [ "$RELAY" = "none" ]; then 
                MODE=background 
                QUEUE_RUN="-q5m" 
                RELAY=""
        elif [ "$RELAY" = "queue" ]; then 
                MODE=deferred 
                QUEUE_RUN="" 
                RELAY="smtp:${DEFAULT_RELAY}"
        else 
                MODE=background 
                QUEUE_RUN="-q5m" 
                RELAY="smtp:$RELAY" 
        fi


        sed -e "s/^DS.*\$/DS$RELAY/" \
            -e "s/^O DeliveryMode=.*\$/O DeliveryMode=$MODE/" \
            <$SENDMAIL_CONFFILE >$SENDMAIL_CONFFILE.N

        # Preserve the ownership and permissions
        chmod --reference=$SENDMAIL_CONFFILE $SENDMAIL_CONFFILE.N
        chown --reference=$SENDMAIL_CONFFILE $SENDMAIL_CONFFILE.N

        # Make a backup copy
        mv -f $SENDMAIL_CONFFILE $SENDMAIL_CONFFILE.whereami

        mv $SENDMAIL_CONFFILE.N $SENDMAIL_CONFFILE

        /etc/init.d/sendmail start
        if [ "$QUEUE_RUN" = "-q5m"]; then
                /etc/init.d/sendmail runq
        fi
    ;;

  postfix)
        if [ "$RELAY" = "none" ]; then 
                DEFER=""
                DISABLE_LOOKUPS="no"
                RELAY=""
        elif [ "$RELAY" = "queue" ]; then 
                DEFER="smtp"
                DISABLE_LOOKUPS="yes"
                RELAY="$DEFAULT_RELAY"
        else 
                DEFER=""
                DISABLE_LOOKUPS="yes"
                MODE=background 
                RELAY="$RELAY"
        fi

        postconf -ev defer_transports=$DEFER disable_dns_lookups=$DISABLE_LOOKUPS \
	         "relayhost=$RELAY"

        # Need to reload after changing things
        /etc/init.d/postfix reload

        if [ "$RELAY" != "queue" ]; then
            # Run mail queue
            /etc/init.d/postfix flush
        fi
    ;;

  exim)
        if [ "$RELAY" = "none" ]; then 
                DEFER="# queue_remote_domains = *"
                RELAY="$DEFAULT_RELAY"
        elif [ "$RELAY" = "queue" ]; then 
                DEFER="queue_remote_domains = ! *"
                RELAY="$DEFAULT_RELAY"
        else 
                DEFER="# queue_remote_domains = *"
                RELAY="$RELAY"
        fi

        # add parameters to configuration file if they do not exist
        #check_for_parameter $EXIM_CONFFILE "queue_remote_domains"
        #check_for_parameter $EXIM_CONFFILE "route_list"

        sed -e "s/^.*queue_remote_domains *=.*\$/$DEFER/" \
            -e "s/route_list *=.*\$/route_list = \"* $RELAY bydns_a\"/" \
            < $EXIM_CONFFILE > $EXIM_CONFFILE.N

        # Preserve the ownership and permissions
        chmod --reference=$EXIM_CONFFILE $EXIM_CONFFILE.N
        chown --reference=$EXIM_CONFFILE $EXIM_CONFFILE.N

        # make a backup copy of configuration file
        mv -f $EXIM_CONFFILE $EXIM_CONFFILE.whereami
        mv $EXIM_CONFFILE.N $EXIM_CONFFILE

        # reload configuration
        /etc/init.d/exim reload

        if [ "$RELAY" != "queue" ]; then
            # flush queued mails
            exim -qff
        fi
    ;;

    exim4)
        if [ "$RELAY" = "none" ]; then
                DEFER="no"
                RELAY="$DEFAULT_RELAY"
        elif [ "$RELAY" = "queue" ]; then 
                DEFER="yes"
                RELAY=""
        else 
                DEFER="no"
        fi

        sed -e "s/dc_smarthost=.*/dc_smarthost=\'$RELAY\'/" \
            < $EXIM4_CONFFILE > $EXIM4_CONFFILE.N

        # Preserve the ownership and permissions
        chmod --reference=$EXIM4_CONFFILE $EXIM4_CONFFILE.N
        chown --reference=$EXIM4_CONFFILE $EXIM4_CONFFILE.N

        # make a backup copy of configuration file
#        mv -f $EXIM4_CONFFILE $EXIM4_CONFFILE.whereami
        mv $EXIM4_CONFFILE.N $EXIM4_CONFFILE

        # reload configuration
        /etc/init.d/exim4 reload

        if [ "$DEFER" = "no" ]; then
            # flush queued mails
            exim4 -qff
        fi
    ;;

  qmail)
        QMAIL_CONFFILE="${QMAIL_CONFDIR}smtproutes"
        QMAIL_CONFFILE_NEW="${QMAIL_CONFFILE}.setmailrelay"
	        
        if [ "$RELAY" = "none" ]; then 
                DELIVERIES="20"
                RELAY="$DEFAULT_RELAY"
        elif [ "$RELAY" = "queue" ]; then 
                DELIVERIES="0"
                RELAY="$DEFAULT_RELAY"
        else 
                DELIVERIES="20"
                RELAY="$RELAY"
        fi

        if [ -e $QMAIL_CONFFILE ]; then
          grep ':[^:]*' $QMAIL_CONFFILE >$QMAIL_CONFFILE_NEW
        else
	        >$QMAIL_CONFFILE_NEW
        fi
        [ -n "$RELAY" ] && echo "$RELAY" >>$QMAIL_CONFFILE_NEW

        # Preserve the ownership and permissions
        chmod --reference=$QMAIL_CONFFILE $QMAIL_CONFFILE_NEW
        chown --reference=$QMAIL_CONFFILE $QMAIL_CONFFILE_NEW

        mv $QMAIL_CONFFILE_NEW $QMAIL_CONFFILE
        echo $DELIVERIES >"${QMAIL_CONFDIR}concurrencyremote"
        /etc/init.d/qmail restart
    ;;

  *)
      echo "Unrecognised mail server type"
    ;;
esac

