#!/bin/sh
#
# chng-mail_relay.sh - Swap Postfix's default mail relay as Internet
#    connection goes up/down - Copyright (C) 1998 by James S. Seymour
#    (jseymour@jimsun.LinxNet.com) (See "License", below.)
#
# Usage: chng-mail_relay [smtp|uucp]
#
# Common application:
#    Place this script in a super-user bin directory (i.e.:
#    "/usr/local/sbin") as "chng-mail_relay" and make it
#    "root root 500".
#
#    Edit the variables near the beginning of this script to
#    suit your host's setup.  ("uucpRelay", "smtpRelay" for
#    sure, and maybe "configFileDir" and "pfBinDir" as well)
#
#    Place a line like "/usr/local/sbin/chng-mail_relay smtp"
#    in your "/etc/ppp/ip-up" or "/etc/ppp/ip-up.local" file.
#    (Or equivalent.)
#
#    Place a line like "/usr/local/sbin/chng-mail_relay uucp"
#    in your "/etc/ppp/ip-down" or "/etc/ppp/ip-down.local" file.
#    (Or equivalent.)
#
#    When your IP connection comes up, your default mail relay
#    will automatically be changed to the specified SMTP relay
#    host.  When your IP connection is down, the default will
#    be your designated UUCP mail relay.  Obviously, incoming
#    mail relaying, being specified by your upstream provider(s),
#    is unaffected.  Any customization of canonical, virtual,
#    or transport, etc. maps/tables is unaffected.
#
# Caveats:
#    Edits the "main.cf" file in place.  Makes backup copies, but
#    keep this in mind.  If this messes-up or is interrupted, it
#    could leave you with a non-functional MTA!
#
#    Makes no effort to flush anything queued for the SMTP host
#    before making the change.  Don't think it can, really.
#    Unless I'm mistaken: by the time this is run, the IP connection
#    will already have been taken down.
#
#    Error checking and protecting one against oneself is really
#    rather minimal.  It would be best if you knew what you're doing!
#
# License:
#    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.
#    
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    
#    You may have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
#    USA.
#    
#    An on-line copy of the GNU General Public License can be found
#    http://www.fsf.org/copyleft/gpl.html.

uucpRelay=""			# Name of default UUCP relay
smtpRelay=""			# Name of default SMTP relay

configFileDir=/etc/postfix	# Where postfix's config files are
pfBinDir=/usr/sbin		# Where postfix's binaries are

# Shouldn't need changes beyond this point
configFile=${configFileDir}/main.cf
smtpBackup=${configFile}-smtp
uucpBackup=${configFile}-uucp

progName=`basename $0`
readonly uucpRelay smtpRelay configFileDir pfBinDir
readonly configFile smtpBackup uucpBackup progName

# Issue error message to stderr and bail out
error() {
    echo "error: ${progName}: $1" >&2
    exit 1
}

# Issue usage message to stderr and bail out
usage() {
    echo "usage: ${progName} [smtp|uucp]" >&2
    exit 1
}

# Sanity checks...
[ X$uucpRelay = "X" ] && error "didn't config \"uucpRelay\" in \"$progName\""
[ X$smtpRelay = "X" ] && error "didn't config \"smtpRelay\" in \"$progName\""
[ -f $configFile ] || error "no such file: \"$configFile\""
[ -x $pfBinDir/postfix ] || error "cannot execute: \"$pfBinDir/postfix\""

[ $# -eq 1 ] || usage

case $1 in
    uucp)
	cmp -s ${configFile} ${uucpBackup} && exit	# nix to do
	cmp -s ${configFile} ${smtpBackup} || cp -p ${configFile} ${smtpBackup}
	ex ${configFile} >/dev/null <<EndOfUUCP
%s/^relayhost[ 	][ 	]*=[ 	][ 	]*.*$/relayhost = ${uucpRelay}/
%s/^default_transport[ 	][ 	]*=[ 	][ 	]*.*$/default_transport = uucp/
wq
EndOfUUCP
	;;
    smtp)
	cmp -s ${configFile} ${smtpBackup} && exit	# nix to do
	cmp -s ${configFile} ${uucpBackup} || cp -p ${configFile} ${uucpBackup}
	ex ${configFile} >/dev/null <<EndOfSMTP
%s/^relayhost[ 	][ 	]*=[ 	][ 	]*.*$/relayhost = ${smtpRelay}/
%s/^default_transport[ 	][ 	]*=[ 	][ 	]*.*$/default_transport = smtp/
wq
EndOfSMTP
	;;
       *) usage
	;;
esac

# tell postfix
${pfBinDir}/postfix reload
exit 0
