#!/bin/sh

cat <<-.EOF
MailStripper configurator for Sendmail MTA based systems
Copyright (C) `date +2002-%Y,` Eridani Star System
All rights reserved.

Searching for Sendmail configuration file...
	.EOF

INFILE=`find /etc -name sendmail.cf 2>/dev/null | head -1`

if [ -z "${INFILE}" ]; then
    cat <<-.EOF

	The Sendmail configuration file was not found, Sendmail does not
	appear to be your Mail Transport Agent (MTA). You will need to
	manually reconfigure your MTA to listen to port ${PORT}.

	.EOF
    exit 1
fi

if [ -n "$1" ]; then
    PORT="$1"
else
    PORT=24
fi

cat <<-.EOF
	The Sendmail Mail Transport Agent (MTA)  configuration file found
	at ${INFILE} will be configured for port ${PORT} instead of port 25.
	.EOF

CURRENT=`fgrep 'DaemonPortOptions=' "${INFILE}"`
if [ -n "${CURRENT}" ]; then
    cat <<-.EOF

	The Sendmail configuration file has the appropriate line, so I am
	patching it to listen on port ${PORT} instead of port 25.

	.EOF
    if [ ! -f "${INFILE}.orig" ]; then
	cp "${INFILE}" "${INFILE}.orig"
    fi
    if echo "${CURRENT}" | fgrep -iq 'port=' ; then
	sed '/DaemonPortOptions=/s:$:,:' < "${INFILE}.orig" \
	    | sed "/DaemonPortOptions=/s:[Pp]ort=[^,]*,:Port=${PORT},:" \
	    | sed '/DaemonPortOptions=/s:,$::' \
	    | sed '/O DaemonPortOptions=/s:^#::' > "${INFILE}"
    else
	sed "/O DaemonPortOptions=/s:\$:, Port=${PORT}:" < "${INFILE}.orig" \
	    | sed '/O DaemonPortOptions=/s:^#::' > "${INFILE}"
    fi
else
    cat <<-.EOF

	The Sendmail config file ${INFILE} does not appear to have the
	appropriate line. I am adding the relevant line to the end of
	this file to try to compensate, configuring it to listen on
	port ${PORT} instead of port 25.

	.EOF
    cat >> "${INFILE}" <<-.EOF

	# Specify that we listen on a non-standard port.
	O DaemonPortOptions=Name=MTA, Port=${PORT}

	# EOF.
	.EOF
fi
