: use /bin/sh
#
#  mkconfig - to help set up smail on a new linux box
#
#  written by Ian Kluft for use with the Linux binary distribution of smail
#
#  As with the rest of this distribution of smail, this is distributed under
#  the GNU Public license.  Run "smail -bC" to see a copy of the license.
#
#  The smail/conf/EDITME and smail/conf/os/linux files in this directory
#  are included in case you want to experiment with the smail 3.1.28 sources
#  yourself.
#

# print starting banner
echo Linux smail setup
echo
echo "by Ian Kluft"
echo "Send problem reports for this script to ikluft@wattres.sj.ca.us"
echo
echo "Send bug reports for smail to its author (see the smail(1) man page)"
echo

# determine host name
echo "Determine hostname"
if [ ! -z "$(uname -n)" ] 2>/dev/null
then
	hostname=`uname -n`
else
	echo "Your system's hostname has not been set up yet." >&2
	echo "Edit /etc/rc.local to include a line like the following" >&2
	echo "  /bin/hostname sysname" >&2
	echo "(replace sysname with a 2-8 letter system 'host' name)" >&2
	exit 1
fi
echo hostname: $hostname
echo

# determine e-mail domain
echo "Determine domain name"
resolvconf=""
for i in /etc/inet/resolv.conf /etc/resolv.conf 
do
	if [ -r $i ]
	then
		resolvconf=$i
	fi
done
if [ ! -z "$resolvconf" ]
then
	# a nameserver is set up so we'll use that domain
	# (if this fails, you may have the original Linux sed...
	# replace it with GNU sed)
	domain=`grep "^[ \010]*domain" $resolvconf | sed '2,$d;s/^[ \010]*domain[ \010]*\(.*\)/\1/'`
	[ -z "$domain" ] && unset resolvconf  # detect bad DNS configuration
fi
if [ -z "$resolvconf" ]
then
	echo "Since you do not have domain name services installed, you"
	echo "need to pick an e-mail domain name for your system."
	echo
	echo "If you are part of a registered Internet domain, use that"
	echo "name (without a hostname - the was already determined)."
	echo "Typical Internet domain names end in .com, .edu, .gov, .net,"
	echo ".org, .us, .ca, .uk, .fi, .de, .dk, .fr, .au, .nz, .jp, etc..."
	echo "If you do not recognize this, you probably don't have a domain."
	echo
	echo "If you are not part of a registered Internet domain, you can"
	echo "just hit ENTER and smail will use the default domain 'uucp'"
	echo "for you."
	echo
	echo "enter your e-mail domain name:"
	read domain
fi
echo "domain: ${domain:-<use smail default value>}"
echo

# determine more_hostnames
more_hostnames="$hostname.${domain:-uucp}"
echo "Determine other hostnames"
echo
echo "If there are any other names you would like your system to recognize"
echo "as itself, you should enter them here.  This may include your domain"
echo "name if your machine is the primary e-mail hub for the domain."
echo
echo "This script will automatically add '$more_hostnames' as an alternate"
echo "name.  You should add the names of all the domains you act as a"
echo "gateway for."
echo
echo "If your system has no other names than the hostname determined above,"
echo "you may safely hit ENTER and leave this blank."
echo
echo "Enter any extra hostnames for your system on separate lines.  Type"
echo "ENTER for a blank line when done."
while read tmpname && [ ! -z "$tmpname" ]
do
	if [ -z "$more_hostnames" ]
	then
		more_hostnames=$tmpname
	else
		more_hostnames="$more_hostnames:$tmpname"
	fi
done
echo
echo "more_hostnames: ${more_hostnames:-<none>}"
echo

# determine visible_name
visible_name=""
echo "Determine visible name"
echo
echo "You can change the name that mail originating on your system will"
echo "appear to come from.  You can enter nothing and smail will use a"
echo "combination of your hostname and domain name, which is probably"
echo "correct.  However, on a departmental network or as a domain's e-mail"
echo "hub, you may want it just to use the domain name without the host"
echo "name.  (i.e. mail would appear to be from 'here.org' instead of"
echo "'box.here.org')"
echo ""
echo "Do not use the name of a domain which you are not a member of or"
echo "no one will be able to successfully reply to your users' mail."
echo ""
echo "It is safe to leave this blank if you don't have a reason to change"
echo "your host's visible name.  (smail will use $hostname.$domain)"
echo ""
echo "enter your machine's visible name:"
read visible_name
echo
echo "visible_name: ${visible_name:-<smail will use $hostname.$domain>}"
echo

# determine smart path
echo "determine smart_path (smarthost)"
echo
echo "smail can use the concept of a smart host which knows how to do e-mail"
echo "routing better than your system does.  A department mail hub or a UUCP"
echo "neighbor who is on the Internet would be good choices for smarthosts."
echo ""
echo "This applies to mail which is destined for another system.  If smail"
echo "can find it on the network or among UUCP neighbors, it can get it"
echo "ther itself.  If not, smail will assume that the smarthost can deliver"
echo "it.  (The smarthost might decide it can't be delivered and return it.)"
echo ""
echo "This does not apply to users which are not recognized.  Those can be"
echo "handled by the smart_user configuration entry, which will come next."
echo ""
echo "You may leave this blank if you do not know the answer.  Be sure to"
echo "go back and edit /usr/lib/smail/config when you find out who to use"
echo "as a smarthost."
echo
echo "enter the name of your smarthost:"
read smart_path
echo
echo "smart_path: ${smart_path:-<no smarthost chosen at this time>}"
echo
if [ ! -z "$smart_path" ]
then
	if uuname | grep "$smart_path" >/dev/null 2>&1
	then
		echo "smarthost delivery method: assuming UUCP"
		smart_transport="uux"
	else
		echo "smarthost delivery method: assuming TCP/IP"
		smart_transport="smtp"
	fi
	echo "Edit the smart_transport line in /usr/lib/smail/config if"
	echo "this assumption is wrong."
fi
echo

# determine smart user
echo "determine smart_user"
echo
echo "if smail does not recognize a local mail address (on this system) it"
echo "is possible to have it sent to another machine for delivery.  This is"
echo "useful in a departmental network where users may often send mail to"
echo "people in the same building or company.  It is not useful if this is"
echo "the e-mail hub for your domain."
echo
echo "You may leave this blank if mail to unrecognized users should be"
echo "'bounced' back to the sender as undeliverable."
echo ""
echo "enter the name of the system to send unrecognized mail to:"
read smart_user
echo
echo "smart_user: ${smart_user:-<none>}"
echo

# determine postmaster
postmaster=""
echo "determine postmaster address"
echo
echo "Every site, including yours, should have a 'postmaster' where mail can"
echo "be sent to report problems.  This can deliver into a file or to a user."
echo "You should probably have it direct mail to you, assuming you will be"
echo "the person maintaining smail on this system."
echo
echo "enter the name of the user that should receive mail for postmaster:"
read postmaster
while [ -z "$postmaster" -a -z "$smartuser" ]
do
	echo "You cannot leave this entry blank because you do not have"
	echo "a system designated for the smartuser director."
	echo
	echo "enter the name of the user who will be postmaster:"
	read postmaster
done
echo

# other config items
smtp_accept_max=10	# Linux is not a mainframe OS so we'll limit
                        #    simulateous SMTPs, remote hosts will try again
smtp_banner='"$primary_name Linux Smail$version #$compile_num ready at $date"'
received_field='"Received: \
	${if def:sender_host \
		{from $sender_host by $primary_name \
		${if def:sender_proto: with $sender_proto}\
		\n\t(Linux Smail$version #$compile_num) }\
	else{by $primary_name ${if def:sender_proto:with $sender_proto }\
		(Linux Smail$version #$compile_num)\n\t}}\
	id $message_id; $spool_date"'


# generate the configuration
[ -f /usr/lib/smail/config ] && mv /usr/lib/smail/config /usr/lib/smail/Oconfig 
{
	echo "#"
	echo "#	smail configuration for $hostname"
	echo "# (see smail(5) man page for details and other options)"
	echo "#"
	echo "hostname=$hostname"
	[ ! -z "$domain" ] && echo "visible_domain=$domain"
	[ ! -z "$visible_name" ] && echo "visible_name=$visible_name"
	[ ! -z "$more_hostnames" ] && echo "more_hostnames=$more_hostnames"
	[ ! -z "$smart_path" ] && echo "smart_path=$smart_path"
	[ ! -z "$smart_transport" ] && echo "smart_transport=$smart_transport"
	[ ! -z "$smart_user" ] && echo "smart_user="'${user}'"@$smart_user"
	[ ! -z "$postmaster" ] && echo "postmaster=$postmaster"
	echo "smtp_accept_max=$smtp_accept_max"
	echo "smtp_banner=$smtp_banner"
	echo "received_field=$received_field"
} > /usr/lib/smail/config 

echo "checking /etc/rc.local for smail startup..."

# add a startup line to rc.local
if egrep 'smail|sendmail' /etc/rc.local >/dev/null 2>&1
then
	echo "smail startup code appears to exist in your /etc/rc.local"
	echo "If not, make sure there is a line with '/usr/lib/sendmail -q10m'"
else
	{
		echo
		echo "# start smail"
		echo "/usr/lib/sendmail -q10m"
	} >> /etc/rc.local
	echo "smail startup has been added to your /etc/rc.local."
	echo "smail will start automatically next time you reboot."
	echo "to start it now, run '/usr/lib/sendmail -q10m' as root"
fi
echo

# we're done
echo "smail configuration done - see /usr/lib/smail/config"
