:
#	@(#) cfg.sh 23.6 91/12/09 
#
#	Copyright (C) 1990-1991 The Santa Cruz Operation, Inc.
#		All Rights Reserved.
#	The information in this file is provided for the exclusive use of
#	the licensees of The Santa Cruz Operation, Inc.  Such users have the
#	right to use, modify, and incorporate this code into other products
#	for purposes authorized by the license agreement provided they include
#	this notice and the associated copyright notice with any such product.
#	The information in this file is provided "AS IS" without warranty.
# 
#	/usr/lib/mkdev/mmdf: Configure MMDF

#
# Some useful variables
#
PATH=/bin:/usr/bin:/usr/mmdf/bin
HOST=host			# location of program to query nameserver
HOSTPART='\([^.]*\)\..*'	# regexp for host part of FQDN
DOMAINPART='[^.]*\.\(.*\)'	# regexp for domain part of FQDN
#
# names of things
#
locmachine=""
locdomain=""
locname=""
myname=""
mydomain=""
myFQDN=""
uuname=""
postmasteralias=""
badhostname=""
badhostchannel=""
badhosttable=""
badhostconf=""
badusername=""
baduserchannel=""
badusertable=""
baduserconf=""
#
# Configuration options
#
havesmtp=no
havemicnet=no
haveuucp=no
havebadhost=no
havebaduser=no
usenameserver=no
qnameserver=no
generatedDomainLine=no
#
# Files
#
TAILORFILE=mmdftailor
TABLEDIR=table
AUSER=alias.user
ALIAS=alias.ali
LALIAS=alias.list
LOCAL=local.chn
LOCDOM=local.dom
SMTPCHN=smtp.chn
DOMAINDOM=domain.dom
MNCHN=micnet.chn
MNDOM=micnet.dom
UUCHN=uucp.chn
UUDOM=uucp.dom
LIST=list.chn
ROOTDOM=root.dom
UUHOSTS=/tmp/mm$$.uuh
SMTPHOSTS=/tmp/mm$$.smh
MICHOSTS=/tmp/mm$$.mih
ALLHOSTS="$UUHOSTS $SMTPHOSTS $MICHOSTS"
SPOOLDIR=/usr/spool/mmdf

cleanup() {
	rm -f /tmp/mm$$*
	exit $1
}

trap 'cleanup 0' 0
trap 'cleanup 1' 1 2 3 15 19

#
# Some useful functions
#
lowerize() {
	echo "$*" | tr '[A-Z]' '[a-z]'
}

requireConfig() {
	echo "$1 must be configured before mail"
	cleanup 0
}

makeDirectory() {
	case $TESTCONFIG in
	yes)	return 0
		;;
	esac

	for dir
	do
		test -d "$dir" || mkdir -p "$dir"
	done
}

saveFile() {
	if [ -f "$1" -a ! -f "$1-" ]
	then
		cp "$1" "$1-"
	fi
}


getDefault() {
	read value

	case "$value" in
	"")	echo "$1"
		;;
	*)	echo "$value"
		;;
	esac
}

getyn() {
	until
		echo "$1? [$2] \c"
		yes=`getDefault "$2"`
		case "$yes" in
		[yYnN]*)	true
				;;
		*)		false
				;;
		esac
	do
		echo "Please answer yes or no.\n" >&2
	done

	case "$yes" in
	[yY]*)	true
		;;
	*)	false
		;;
	esac
}

getFQDN() {
	# getFQDN host returns fully qualified domain name

	hostname="$1"
	if [ "$usenameserver" = yes -o "$qnameserver" = yes ] &&
		 set -- `$HOST $hostname 2>/dev/null`
	then
		echo $1
	else
		case "$hostname" in
		*.*)	echo "$hostname"
			;;
		*)	awk "\$1 == \"$hostname\" {print \$2}" $ALLHOSTS
			;;
		esac
	fi
}


getDomainNames() {
	# getDomainNames protocol hostfile [default domain]
	case $# in
	3)	defaultDomain=".$3"
		;;
	*)	defaultDomain=""
		;;
	esac

	until
		until
			echo "\nEnter the $1 site name (blank to terminate): \c"
			read shortname
			[ "$shortname" = "" ] ||
				grep -s "^$shortname$" $2 >/dev/null 2>&1
		do
			if grep -s "$shortname" $2 >/dev/null 2>&1
			then
				echo "You have already specified a domain name for that host." >&2
			else
				echo "You do not use $1 to talk to that host!" >&2
			fi
		done
		[ "$shortname" = "" ]
	do
		echo "What is $shortname's fully qualified name? [$shortname$defaultDomain] \c"
		longname="`getDefault $shortname$defaultDomain"
		longname="`lowerize $longname`"

		ed - $2 <<-END_OF_SCRIPT
			/^$shortname$/s//& $longname/
			w
			q
		END_OF_SCRIPT
		if grep -v -s ' ' $2 >/dev/null 2>&1
		then
			: some still to go
		else
			echo "Done with $1."
			break
		fi
	done
}

generateDefaultDomains() {
	# generateDefaultDomains file default domain

	case $2 in
	.*|"")	defaultDomain="$2" # it is ok already
		;;
	*)	defaultDomain=".$2"
		;;
	esac
	defaultDomain="`lowerize $defaultDomain`"

	ed - $1 <<-END_OF_SCRIPT
		g/^uunet$/s//& &.uu.net/
		v/ /s/.*/& &$defaultDomain/
		w
		q
	END_OF_SCRIPT
}


buildSMTPfromNS () {
	for i in mx a cname; do
		$HOST -t $i -l $mydomain
	done | sed -n	-e '/127.0.0.1$/d' \
			-e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' \
			-e '/has address/s/\([^.]*\)\([^ ]*\).*/\1 \1\2/p' \
			-e '/nickname/s/\..* is a nickname for / /p' \
			-e '/handled by/s/\([^.]*\)\([^ ]*\).*handled by \(.*\)/\1 \1\2 \3/p'
}						
#
# MAIN MAIN MAIN MAIN MAIN MAIN
#

# Check out the situation
set -- `id`
user=`expr $1 : '.*(\(.*\))'`
group=`expr $2 : '.*(\(.*\))'`

case $user in
mmdf)	case `pwd` in
	/usr/mmdf)
		: feeling groovy
		;;
	*)	more <<\HERE
The configuration generated will not be installed to be run by
default, because the script is not being run in the mmdf
configuration directory.

HERE
		if getyn 'Do you wish to continue' n
		then
			: Keep going
		else
			exit 0
		fi
		TESTCONFIG=yes
		;;
	esac
	;;
*)	case `pwd` in
	/usr/mmdf)
		more <<\HERE
Only the user "mmdf" may build the default configuration in
/usr/mmdf.  If you wish to build a test configuration, then run
this program in another directory.
HERE
		exit 1
		;;
	*)	more <<\HERE
The configuration generated will not be installed by default.
The user "mmdf" will have to copy the configuration files
generated here into the standard mmdf configuration heirarchy,
and rebuild before this configuration will take effect.

HERE
		if getyn 'Do you wish to continue' y
		then
			: Keep going
		else
			exit 0
		fi
		TESTCONFIG=yes
		;;
	esac
	;;
esac

if [ -d table ]
then
	: Never mind
elif mkdir table >/dev/null 2>&1
then
	: Never mind
else
	echo "Could not create table directory" >&2
	exit 1
fi

# Confirm assumptions
more <<\End_of_message
In order to simplify the mail configuration files built, this program
makes several assumptions about the environment in which it is being
run.  If any of these assumptions are invalid for this machine, then the
program will either build a correct but inefficient configuration, or a
configuration containing the wrong information about how a host is to be
reached.  In the latter case, you will have to edit the generated
configuration files to correct the problems noted.

Here is the list of assumptions made:
	- All the various communications methods which will be used for the
	  exchange of mail are already configured.  If this is not the
	  case, then exit this script and configure the communication
	  subsystems which will be used.  At present this script can
	  configure mail to be exchanged via UUCP, Micnet, and TCP/IP.

	- If mail is exchanged with a host, then only one method of
	  communication is used to perform the exchange.  If a host is
	  accessible by more than one of the protocols, then MMDF will use
	  the first in the following list that is applicable: TCP/IP,
	  MICNET, then UUCP.  If the protocol chosen in not correct for
	  this installation, then you will have to delete the line for the
	  offending host from the incorrect ".chn" files.

	- If a communications channel (UUCP, MICNET, or TCP/IP) is
	  configured, then mail is exchanged via that channel with every
	  host configured on it.  If this is not true for some host
	  accessible via a given channel, then you will have to delete the
	  information about that host from the appropriate ".chn" file, and
	  also from any ".dom" files that it may appear in, if it does not
	  receive mail at all.

	- If this host exchanges mail with two other hosts, "a" and "b"
	  say, then the host "a" may send mail to "b" via this host.  If
	  you do not want that, then this program will properly configure
	  the system, except for the authorization information necessary to
	  prevent the transfer.

	- All the hosts accessible via a particular communications method
	  (UUCP, for example) will be listed in the same channel.  This is
	  only a problem if you will be using the authorization facilities
	  provided by MMDF.

End_of_message

if getyn 'Do you wish to continue the configuration process at this time' y
then
	: Go for it
else
	echo "Bye."
	exit 0
fi
#
# Initialize some files
#
>$UUHOSTS
>$SMTPHOSTS
>$MICHOSTS

#
# Get MMDF release information
#
if [ ! -f /etc/default/mail ]
then
	echo "There is no file /etc/default/mail, assuming level 32 MMDF."
	MMDFREL=32
else
	. /etc/default/mail
	case "$MMDFREL" in
	"")	echo "There is no MMDFREL string in /etc/default/mail, assuming level 32 MMDF."
		MMDFREL=32
		;;
	*)	echo "This machine is running level $MMDFREL MMDF."
		;;
	esac
fi

#
# Get host name information
#
# Get host and domain names
if [ -x /usr/bin/hostname ]
then
	myname=`/usr/bin/hostname`; myname=`lowerize "$myname"`
	case "$myname" in
	*.*)	mydomain=`expr "$myname" : "$DOMAINPART"`
		myname=`expr "$myname" : "$HOSTPART"`
		;;
	*)	mydomain="UUCP"
		;;
	esac
else
	# use uname to get hostname
	myname=`uname -n`;
	mydomain="UUCP"
fi
myFQDN="$myname.$mydomain"

case "$myname" in
""|scosysv)
	echo "\nThe name of this host has not been configured yet.
What will you be calling this host? \c"
	myname=`getDefault scosysv`
	;;
esac
while [ "$myname" = scosysv ]
do
	echo "\nYou must specify a host name before the configuration can be completed."
	if getyn "Do you wish to continue the configuration at this time?" n
	then
		echo "What is the name of this host? \c"
		myname="`getDefault scosysv`"
	else
		requireConfig "The host name"
	fi
done

until
	# confirm hostname and domainname
	getyn "\nIs your fully qualified host name '$myFQDN'" y
do
	echo "What is the correct host name? [$myname] \c"
	myname=`getDefault "$myname"`

	echo "What is the correct domain name? [$mydomain] \c"
	mydomain=`getDefault "$mydomain"`

	myFQDN="$myname.$mydomain"
done
myname=`lowerize "$myname"`
mydomain=`lowerize "$mydomain"`
myFQDN=$myname.$mydomain

# Is the local host to be hidden?
# We cannot hide a host if it is a member of a top level domain
# e.g. we cannot hide foo.com or bar.UUCP
#
if expr "$mydomain" : '.*\..*' >/dev/null
then
	more <<END_OF_MESSAGE

At many sites, it is common for mail to be addressed as being from
"person@site" instead of from "person@machine.site".  This allows people
to be moved between machines internally without requiring them to notify
all their external correspondents about the address change.  This
configuration does, however, require a complete user alias table (see
below) containing mappings from user names to the host that they actually
plan to read their mail on.  If you are not sure, then you should
probably answer "yes" if there are two or more machines in the domain
"$mydomain", and "no" otherwise.

END_OF_MESSAGE
	if getyn "Do you wish to hide '$myFQDN' behind '$mydomain'" y
	then
		locname="`expr $mydomain : $HOSTPART`"
		locdomain="`expr $mydomain : $DOMAINPART`"
		locmachine="$myname"
	else
		locmachine="$myname"
		locname="$myname"
		locdomain="$mydomain"
	fi
else
	locmachine="$myname"
	locname="$myname"
	locdomain="$mydomain"
fi

#
# Get aliasing information
#

# Get special aliasing information
more <<\END_OF_MESSAGE

It is now time to get information about "mail aliases" for some special
accounts.  A mail alias is a way of making mail addressed to a particular
local address be delivered somewhere else.  This is commonly used to
redirect mail addressed to root to an account belonging to a real person.

The three addresses that will be dealt with at this time are "root,"
"mmdf," and "postmaster."  There is no account for postmaster, but it is a
standard address (which must be provided by all sites) to which problems
with the mail system can be reported.

END_OF_MESSAGE
if getyn 'Do you wish to have mail for root redirected to a real user' y
then
	echo "To whom should root's mail be sent? \c"
	read rootalias
fi

if getyn '\nDo you wish to redirect mail addressed to mmdf' y
then
	mmdfalias=${rootalias:-root}
	echo "To whom should mmdf's mail be sent? [$mmdfalias] \c"
	mmdfalias=`getDefault $mmdfalias`
fi

postmasteralias=${mmdfalias:-${rootalias:-mmdf}}
echo "\nTo whom should mail addressed to postmaster be sent? [$postmasteralias] \c"
postmasteralias=`getDefault $postmasteralias`
# Get user aliasing information (optional)
more <<ALIAS_MESSAGE

It is common on large sites to create an alias file on every machine that
contains the name of the machine that a user actually reads his mail on.
In this way, some one on any machine can send mail to "fred" and know that
it will be delivered to the correct computer for fred to read it.  If you
want to have this arrangement, see the information about aliases in the
chapter discussing MMDF configuration in your system administrator's
guide.  There will be a fully configured (but empty) file called
"alias.user" in the mmdf table directory to hold this information.

Note:  If you are converting from XENIX to UNIX, then there is a utility
to convert a XENIX style mail alias file into the correct format for MMDF.
See the subsection of "Setting up Electronic Mail" entitled "Converting an
Alias file."

ALIAS_MESSAGE

# Check communications configurations
# UUCP
if [ ! -x /usr/bin/uuname ]
then
	echo "UUCP not installed, skipping ..."
elif getyn "Are you going to be using UUCP for mail" y
then
	haveuucp=yes
	uuname=`uuname -l`
	until getyn "\nIs this host known as '$uuname' for UUCP" y
	do
		echo "What is this host's UUCP name? [$uuname] "
		uuname="`getDefault $uuname`"
	done

	uuname | sort -u -o $UUHOSTS
	if [ -s $UUHOSTS ]
	then
		if echo "scolon\nsosco" | cmp -s - $UUHOSTS
		then
			requireConfig UUCP
		elif egrep "(scolon|sosco)" $UUHOSTS >/dev/null 2>&1
		then
			ed - $UUHOSTS <<-END_OF_SCRIPT
				g/^scolon$/d
				g/^sosco$/d
				w
				q
				END_OF_SCRIPT
		fi
	else
		requireConfig UUCP
	fi
	#
	# Get domain information related to the uucp hosts.
	#
	more <<DOMAIN_MESSAGE
	
Because UUCP does not maintain information about domain names, it will be
necessary for you to provide the domain names of any of the hosts with
which you communicate via UUCP.

DOMAIN_MESSAGE
	if getyn "Do any of your UUCP hosts have full domain names" n
	then 
		getDomainNames UUCP $UUHOSTS
	fi
	generateDefaultDomains $UUHOSTS .uucp
fi # using uucp


# MICNET
if [ ! -s /usr/lib/mail/top ]
then
	echo "\nMicnet not configured, skipping ...\n"
elif getyn "\nAre you going to be using MICNET for mail" y
then
	havemicnet=yes
	awk '/^[^#]/ {
			if (NF == 5) {
				print $1
				print $3
			}
	}' /usr/lib/mail/top | sort -u | grep -v $myname >$MICHOSTS

	if getyn "Are all the MICNET hosts in the domain $mydomain" y
	then
		: This is what we are expecting
	else
		getDomainNames MICNET $MICHOSTS $mydomain
	fi
	generateDefaultDomains $MICHOSTS $mydomain
fi


# TCP/IP
if [ ! -f /etc/inetd ]
then
	echo "\nTCP/IP not installed, skipping...\n"
elif getyn "\nAre you going to be using SMTP for mail" y
then
	havesmtp=yes
	if [ -f /etc/resolv.conf -o -f /etc/named.boot ]
	then
		# Running the name server
		case "$MMDFREL" in
		*NS)	echo "A domain name server is running, mail will be configured to use it."
			usenameserver=yes
			qnameserver=yes
			>$SMTPHOSTS
			;;
		*)	more <<END_OF_MSG

A domain name server is configured on this system, but MMDF cannot get
configuration information from it directly.  Information about the local
domain will be extracted from the name server and configured into MMDF, but
any changes to the name server database will have to be reflected in the
MMDF configuration files.

Getting SMTP configuration information...
END_OF_MSG
			qnameserver=yes
			buildSMTPfromNS >$SMTPHOSTS;
			;;
		esac
	elif expr "$MMDFREL" : '.*NS$' >/dev/null &&
		getyn 'The name server is not currently configured on this machine.
Do you plan to configure a name server on the local network' n
	then
		requireConfig "name server"
	else
		# use /etc/hosts
		tr '[A-Z]' '[a-z]' </etc/hosts \
		| awk '/^#/ || /^[ \t]*$/ || /^127/ {next}
			{
				if ($2 !~ /\./) {
					print $2, $2 "." mydomain
					$2 = $2 "." mydomain
				} else
					print $2, $2

				for (i = 3; i <= NF; ++i) {
					if ($i != $2)
						print $i, $2
				}
			}' mydomain=$mydomain >$SMTPHOSTS
	fi
fi


#
# Only ask for a smart host if we can talk to it.
#
if [ $havemicnet = yes -o $haveuucp = yes -o $havesmtp = yes ]
then
	more <<END_OF_SCRIPT

Many sites do not have complete information about the entire mail network,
but rely on another "smarter" host to determine the correct route that
mail messages should follow to reach their destinations.  Any mail that
the local machine is incapable of correctly handling is passed to the
smart host for further processing.  In MMDF, this is called the "badhost"
channel.

END_OF_SCRIPT
	if getyn "Do you have such a \"smart\" host" y
	then
		until
			until
				echo "What is its name? (q if you have changed your mind) \c"
				read smarthost
				case "$smarthost" in
				"")	false
					;;
				*)	true
					;;
				esac
			do
				echo "\nPlease enter a name!"
			done
			case "$smarthost" in
			q)	break
				;;
			esac
	
			([ "$usenameserver" = yes -o "$qnameserver" = yes ] &&
				$HOST $smarthost >/dev/null 2>&1;) ||
			grep -s "^$smarthost " $ALLHOSTS >/dev/null 2>&1 ||
			grep -s " $smarthost$" $ALLHOSTS >/dev/null 2>&1
		do
			echo "This machine does not communicate with $smarthost."
			echo "Please enter the name of a machine directly connected to $myname."
		done
		case "$smarthost" in
		q)	: do nothing
			;;
		*)
			badhosthost="`getFQDN $smarthost`"
			if {
				[ "$usenameserver" = yes -o "$qnameserver" = yes ] &&
				 $HOST $smarthost >/dev/null 2>&1
			} || grep -s $smarthost $SMTPHOSTS >/dev/null 2>&1
			then
				badhostpgm=smtp
				badhostconf="$myFQDN"
				badhosttable=smtpchn
			elif grep -s $smarthost $UUHOSTS >/dev/null 2>&1
			then
				badhostpgm=uucp
				badhosttable=uuchn
			elif grep -s $smarthost $MICHOSTS >/dev/null 2>&1
			then
				badhostpgm=micnet
				badhosttable=mnchn
			fi
		esac
	fi

	#
	# Now ask about badusers
	#
	more <<END_OF_SCRIPT

Another option, which is often used on large sites is to have a central
machine which contains complete knowledge about all the users on the site,
and only maintaining local lists on each machine.  MMDF provides the
facility to forward mail containing unrecognised local addresses to a
smarter host which will have a complete user data base (via the "baduser"
channel).

END_OF_SCRIPT
	if getyn "Do you have such a \"smart\" host" y
	then
		until
			echo "What is its name? (q if you have changed your mind) [$badhosthost] \c"
			smarthost=`getDefault $badhosthost`
			case "$smarthost" in
			q)	break
				;;
			esac

			([ "$usenameserver" = yes -o "$qnameserver" = yes ] && $HOST $smarthost >/dev/null 2>&1;) ||
			grep -s "^$smarthost " $ALLHOSTS >/dev/null 2>&1 ||
			grep -s " $smarthost$" $ALLHOSTS >/dev/null 2>&1
			
		do
			echo "This machine does not communicate with $smarthost."
			echo "Please enter the name of a machine directly connected to $myname."
		done
		case "$smarthost" in
		q)	: do nothing
			;;
		*)
			baduserhost="`getFQDN $smarthost`"
			if {
				[ "$usenameserver" = yes -o "$qnameserver" = yes ] &&
				 $HOST $smarthost >/dev/null 2>&1
			} || grep -s $smarthost $SMTPHOSTS >/dev/null 2>&1
			then
				baduserpgm=smtp
				baduserconf="$myFQDN"
				badusertable=smtpchn
			elif grep -s $smarthost $UUHOSTS >/dev/null 2>&1
			then
				baduserpgm=uucp
				badusertable=uuchn
			elif grep -s $smarthost $MICHOSTS >/dev/null 2>&1
			then
				baduserpgm=micnet
				badusertable=mnchn
			fi
		esac
	fi
fi

case $MMDFREL in
43*)
	DmnPartialFlags=', flags=partial'
	DmnRouteFlags=', flags=route'
	;;
*NS)
	DmnPartialFlags=', flags=partial'
	;;
esac

#
# Now build all the files necessary
#

saveFile $TAILORFILE
echo "Creating the mmdftailor file: header, host name info\c"
cat >$TAILORFILE <<END_OF_TAILOR
;
;	mmdftailor (`date '+%a, %d %h %y %T'`)
;
; First, we define the local domain and system name.  The default
; domain is UUCP.  If our organization only has one machine, we
; may just name it as in machine.UUCP, for instance.  Otherwise,
; we might have a number of machines, and have the machine name
; be below an organization name as in machine.COMpany.UUCP.  Of
; course, if we have a registered domain name, we use that instead
; of the default (pseudo) domain UUCP.  UUname is used only for
; the uucp channel, and is the short uucp name that we use.

MLDOMAIN $locdomain
MLNAME $locname
END_OF_TAILOR

if [ ! -z "$locmachine" ]
then
	echo "MLOCMACHINE $locmachine" >>$TAILORFILE
fi

if [ -s $UUHOSTS ]
then
	echo "UUname $uuname" >>$TAILORFILE
fi

echo ", support address, alias tables, local domain and channel\c"
cat >>$TAILORFILE <<END_OF_TAILOR

; MSUPPORT is the address to which problem notifications concerning
; the delivery of mail are sent.  It _must_ be a valid address.

MSUPPORT postmaster

;
; Alias configuration
;
MTBL name=alias,	file="$ALIAS",	show="Administrative aliases"
MTBL name=lalias,	file="$LALIAS",	show="Mailing list aliases"
MTBL name=auser,	file="$AUSER",	show="General user aliases"

ALIAS	table=alias,	nobypass,	trusted
ALIAS	table=lalias,	nobypass
ALIAS	table=auser


;
; Local mail configuration info
;
MTBL name=local,	file="$LOCAL",	show="Local Host Aliases"
MTBL name=locdom,	file="$LOCDOM",	show="Local Domain"

MCHN	local, show="Local Delivery", ap=822, mod=imm
MDMN	"$mydomain", show="Local domain", table=locdom

;
; special list processing configuration info
;
MTBL    list,           file="list.chn",        show="List Channel"
MCHN	list, show="List Processing", ap=same, mod=imm,
	host="$myFQDN"
END_OF_TAILOR

if [ -s $SMTPHOSTS -o $usenameserver = yes ]
then
	echo ", SMTP\c"
	generatedDomainLine=yes
	cat >>$TAILORFILE <<END_OF_TAILOR

;
; SMTP Configuration info
;
END_OF_TAILOR
	case "$usenameserver" in
	no)
		cat >>$TAILORFILE <<END_OF_TAILOR
MTBL	smtpchn,	file=$SMTPCHN,		show="SCO SMTP Channel"
MCHN	smtp, show="SCO SMTP Delivery", ap=822, tbl=smtpchn, mod=imm,
	confstr="$myFQDN"

;
; The local domain table
;
MTBL domain, file=$DOMAINDOM, show="Local Ethernet"$DmnPartialFlags
MDMN "$mydomain", show="Local Ethernet", table=domain
END_OF_TAILOR
		;;
	yes)
		cat >>$TAILORFILE <<END_OF_TAILOR
;
; The local domain table
;
MTBL domain, file=$DOMAINDOM, show="Local Ethernet"$DmnPartialFlags
MDMN "$mydomain", show="Local Ethernet", table=domain

MTBL	smtpchn,	flags=ns,		show="SCO SMTP Channel",
	flags=channel
MTBL	smtpdom,	flags=ns,		show="Local Ethernet",
	flags=domain$DmnPartialFlags

MCHN	smtp, show="SCO SMTP Delivery", ap=822, tbl=smtpchn, mod=imm,
	confstr="$myFQDN"
; We need, among other things, the delay channel
MCHN	delay, show="Name server Delay Channel", ap=same, tbl=smtpchn
MDMN	"$mydomain", show="Local Ethernet", table=smtpdom
END_OF_TAILOR
		;;
	esac
fi

case "$havemicnet" in
yes)
	echo ", MICNET\c"
	case "$generatedDomainLine" in
	no)	generatedDomainLine=yes
		cat >>$TAILORFILE <<END_OF_TAILOR
;
; The local domain table
;
MTBL domain, file=$DOMAINDOM, show="Local Domain"$DmnPartialFlags
MDMN "$mydomain", show="Local Domain", table=domain
END_OF_TAILOR
		;;
	esac

        cat >>$TAILORFILE <<END_OF_TAILOR

;
; MICNET Configuration info
;
MTBL    mnchn,          file="micnet.chn",        show="SCO Micnet Channel"
MCHN    micnet, show="SCO MICNET Delivery", tbl=mnchn, ap=same, mod=imm
END_OF_TAILOR
	;;
esac

case "$haveuucp" in
yes)
	echo ", UUCP\c"
	case "$generatedDomainLine" in
	no)	generatedDomainLine=yes
		cat >>$TAILORFILE <<END_OF_TAILOR
;
; The local Domain table
;
MTBL domain, file=$DOMAINDOM, show="Local Domain"$DmnPartialFlags
MDMN    "$mydomain", show="Local Domain", table=domain
END_OF_TAILOR
		;;
	esac
	cat >>$TAILORFILE <<END_OF_TAILOR

;
; UUCP Configuration info
;
MTBL uuchn, file="uucp.chn", show="SCO UUCP Channel"
MTBL uudom, file="uucp.dom", show="SCO UUCP Domain"$DmnRouteFlags$DmnPartialFlags

MCHN uucp, show="SCO UUCP Delivery", tbl=uuchn, ap=same, mod=imm
MDMN "UUCP", show="UUCP Domain", table=uudom
END_OF_TAILOR
	;;
esac

if [ ! -z "$badhostpgm" ]
then
	echo ", badhosts\c"
	cat >>$TAILORFILE <<END_OF_TAILOR

;
; The badhosts channel
;
MCHN badhosts, show="Last-Chance Routing", pgm=$badhostpgm, tbl=$badhosttable, ap=822, mod=imm,
     host="$badhosthost"${badhostconf:+, confstr=$badhostconf}
END_OF_TAILOR
fi

if [ ! -z "$baduserpgm" ]
then
	echo ", badusers\c"
	cat >>$TAILORFILE <<END_OF_TAILOR

;
; The baduser channel
;
MCHN badusers, show="Last-Chance Routing", pgm=$baduserpgm, tbl=$badusertable, ap=822, mod=imm,
     host="$baduserhost"${baduserconf:+, confstr=$baduserconf}
END_OF_TAILOR
fi

echo ", root"
cat >>$TAILORFILE <<END_OF_TAILOR
;
; The root domain table.
;
MTBL rootdom, file="root.dom", show="Root Domain"$DmnRouteFlags
MDMN "", show="Root Domain", table=rootdom

;
;  Logging levels
;
MMSGLOG level=FAT
MCHANLOG level=FAT

;
; Style of mailbox locking to use.  Options are "xenix" for Xenix
; /tmp/*.mlk lock files, "v7" for the old *.lck lock files, "advisory"
; for OS advisory file locking, or "all" for all locking styles.
; Multiple lock styles may be specified.
;
MLCKTYPE advisory
END_OF_TAILOR

echo "\nBuilding the alias tables (mostly empty)"
saveFile $TABLEDIR/$ALIAS
cat >$TABLEDIR/$ALIAS <<END_OF_ALIAS
#
#	$ALIAS: Administrative aliases (`date '+%a, %d %h %y %T'`)
#

# Following alias is required  by the mail protocol, RFC 822
postmaster: ${postmasteralias}

END_OF_ALIAS
if [ ! -z "$rootalias" -a "$rootalias" != root ]
then
	echo "root: $rootalias" >> $TABLEDIR/$ALIAS
fi

if [ ! -z "$mmdfalias" -a "$mmdfalias" != mmdf ]
then
	echo "mmdf: $mmdfalias" >>$TABLEDIR/$ALIAS
fi

if [ -f $TABLEDIR/$LALIAS ]
then
	echo "A Mailing list alias table ($LALIAS) already exists, skipping"
else
	saveFile $TABLEDIR/$LALIAS
	cat >$TABLEDIR/$LALIAS << END_OF_ALIAS
#
#	$LALIAS: Mailing list aliases (`date '+%a, %d %h %y %T'`)
#

END_OF_ALIAS
fi


if [ -f $TABLEDIR/$AUSER ]
then
	echo "A general user alias table ($AUSER) already exists, skipping"
else
	cat >$TABLEDIR/$AUSER <<END_OF_ALIAS
#
#	$AUSER: General user aliases (`date '+%a, %d %h %y %T'`)
#

END_OF_ALIAS
fi

case $user in
mmdf)
	makeDirectory $SPOOLDIR/lock/home/tmp $SPOOLDIR/lock/home/addr \
		$SPOOLDIR/lock/home/msg >/dev/null 2>&1
	chgrp mmdf $SPOOLDIR/lock
	chmod 750 $SPOOLDIR/lock
	chmod 777 $SPOOLDIR/lock/home
	;;
esac
	echo "\nBuilding channel files"
echo "local\c"
makeDirectory $SPOOLDIR/lock/home/q.local >/dev/null 2>&1
saveFile $TABLEDIR/$LOCAL
cat >$TABLEDIR/$LOCAL <<END

#
#	$LOCAL: Local channel table (`date '+%a, %d %h %y %T'`)
#

$locmachine: $locmachine
$locname.$locdomain: $locmachine
END

if [ "$locmachine" != "$locname" ]
then
	# Host hiding
	echo "$locmachine.$locname.$locdomain: $locmachine" >>$TABLEDIR/$LOCAL
fi

echo ", list\c"
makeDirectory $SPOOLDIR/lock/home/q.list >/dev/null 2>&1
saveFile $TABLEDIR/$LIST
cat >$TABLEDIR/$LIST <<END

#
#	$LIST: List channel table (`date '+%a, %d %h %y %T'`)
#

list-processor: list-processor
END

if [ "$havesmtp" = yes ]
then
	makeDirectory $SPOOLDIR/lock/home/q.smtp >/dev/null 2>&1

	case "$usenameserver" in
	yes)
		# we need to build the delay channel queue directory
		makeDirectory $SPOOLDIR/lock/home/q.delay
		;;
	no)
		saveFile $TABLEDIR/$SMTPCHN
		echo ", SMTP\c"
		cat >$TABLEDIR/$SMTPCHN <<END_OF_FILE
#
#	$SMTPCHN: SMTP channel table (`date '+%a, %d %h %y %T'`)
#

END_OF_FILE

		case "$qnameserver" in
		yes)
			$HOST -t a -l $mydomain |
			sed -e '/ NS /d' \
			    -e '/127\.0\.0\.1/d' \
			    -e 's/ has address /: /' >> $TABLEDIR/$SMTPCHN
			;;
		*)
			awk '/^#/ || /^[ \t]*$/	|| /^127/ {next}
				{
					if ($2 !~ /\./) {
						print $2 "." mydomain ": " $1
					} else
						print $2 ": " $1
				}' mydomain=$mydomain /etc/hosts >>$TABLEDIR/$SMTPCHN
			;;
		esac
	esac
fi

if [ "$havemicnet" = yes ]
then
	makeDirectory $SPOOLDIR/lock/home/q.micnet >/dev/null 2>&1
	echo ", MICNET\c"
	saveFile $TABLEDIR/$MNCHN
	cat >$TABLEDIR/$MNCHN <<END_OF_FILE
#
#	$MNCHN: Micnet channel table (`date '+%a, %d %h %y %T'`)
#

END_OF_FILE
	awk '{printf("%s: %s:%%s\n", $2, $1)}' $MICHOSTS >>$TABLEDIR/$MNCHN
fi

if [ "$haveuucp" = yes ]
then
	makeDirectory $SPOOLDIR/lock/home/q.uucp >/dev/null 2>&1
	echo ", UUCP\c"
	saveFile $TABLEDIR/$UUCHN
	cat >$TABLEDIR/$UUCHN <<END_OF_FILE
#
#	$UUCHN: UUCP channel table (`date '+%a, %d %h %y %T'`)
#

END_OF_FILE
	# Chuck the mock domain .UUCP (it's only used to pick things for the
	# domain table, and is removed there) and generate
	# foo.dom.ain: foo!%s for each line in $UUHOSTS
	sed -n -e '/\.uucp$/s///' \
	    -e 's/^\([^ ]*\) \(.*\)$/\2: \1!%s/p' $UUHOSTS >>$TABLEDIR/$UUCHN
fi

echo ", badusers\c"
if [ ! -z "$baduserpgm" ]
then
	makeDirectory $SPOOLDIR/lock/home/q.badusers >/dev/null 2>&1
fi

echo ", badhosts\c"
if [ ! -z "$badhostpgm" ]
then
	makeDirectory $SPOOLDIR/lock/home/q.badhosts >/dev/null 2>&1
fi
case $user in
mmdf)	chmod 777 $SPOOLDIR/lock/home/* >/dev/null 2>&1
	;;
esac

echo "\n\nBuilding the domain tables"
echo "local\c"
saveFile $TABLEDIR/$LOCDOM
cat >$TABLEDIR/$LOCDOM <<END
#
#	$LOCDOM: Local domain table (`date '+%a, %d %h %y %T'`)
#

$locmachine: $locname.$locdomain
END

#
# We only need to generate domain.dom if it's used in mmdftailor
case "$generatedDomainLine" in
yes)
	echo ", $mydomain\c"
	saveFile $TABLEDIR/$DOMAINDOM
	cat >$TABLEDIR/$DOMAINDOM <<END_OF_FILE
#
#	$DOMAINDOM: $mydomain domain table (`date '+%a, %d %h %y %T'`)
#

END_OF_FILE
	sed -e "/\.$mydomain$/!d" \
	    -e 's/ /: /' $ALLHOSTS >>$TABLEDIR/$DOMAINDOM
	;;
esac

case "$haveuucp" in
yes)
	echo ", UUCP\c"
	saveFile $TABLEDIR/$UUDOM
	cat >$TABLEDIR/$UUDOM <<END_OF_FILE
#
#	$UUDOM: UUCP domain table (`date '+%a, %d %h %y %T'`)
#

END_OF_FILE
	awk 'BEGIN {
			gsub(/\./, "\\.", mydomain)
			mydompat="\\." mydomain "$"
		}

		{
			if ($2 ~ mydompat)
				next
			if (($2 ~ /\.uucp$/) || ($2 !~ /\./)) {
				sub(/\.uucp$/, "", $2)
				printf("%s: %s\n", $1, $2)
			}
		}' mydomain="$mydomain" $ALLHOSTS >>$TABLEDIR/$UUDOM
	;;
esac


echo ", root\c"

saveFile $TABLEDIR/$ROOTDOM
cat >$TABLEDIR/$ROOTDOM <<END
#
#	$ROOTDOM: Root domain table (`date '+%a, %d %h %y %T'`)
#

list-processor: list-processor

$locmachine: $locname.$locdomain
END

if [ $locname != $locmachine ]
then
	# Host hiding is in effect
	echo "$locname.$locdomain: $locname.$locdomain" >>$TABLEDIR/$ROOTDOM
fi

# Delete hosts which do not have domain names, or have the domain name '.uucp'
#	(they are in uucp.dom)
# Delete hosts which are in my domain (they are in 'smtp.dom')
# generate three lines for each of the remaining lines
# foo foo.dom.ain maps into
#
#	foo: foo.dom.ain
#	foo.dom.ain: foo.dom.ain
#	dom.ain: foo.dom.ain
sed -e '/ [^.]*$/d' \
    -e '/\.uucp$/d' \
    -e "/\.`echo "$mydomain" | sed -e 's/\./\\./g'`$/d" \
    -e h \
    -e 's/.*//' -e p \
    -e g -e 's/ /: /' -e p \
    -e 's/.* \(.*\)/\1: \1/' -e p \
    -e g -e 's/^.* \([^.]*\.\)\(.*\)$/\2: \1\2/' $ALLHOSTS >>$TABLEDIR/$ROOTDOM

echo "\ndone"

case `pwd` in
/usr/mmdf)
	echo "building the database"
	/usr/mmdf/table/dbmbuild -n -v
	;;
*)
	echo "You will have to install the database and run dbmbuild"
	;;
esac
echo ""

more <<END_OF_CONFIG

The configuration is now complete.  If your site does not completely
conform to the assumptions listed at the beginning of the program, then
you may wish to look over the files that have been built to ensure that
they are correct.  This script also created a new "alias.ali" file, and
saved the old one in "alias.ali-".  If you had any aliases in alias.ali
(besides mmdf, root, and postmaster), then you should add them to the new
alias.ali, and rerun table/dbmbuild to rebuild the database.  You may
also have to create an alias.user file, if you decided to hide the names
of you local machines behind your domain name.

There are several configuration options available for MMDF (especially
related to logging).  These options are all documented in the
mmdftailor(F) man page.  In particular, if you are switching from a
sendmail-based system, then you may want to change the MMSGLOG parameter
(default is "FAT" - log fatal errors only) to "FST" in order to get the
equivalent of the mqueue/syslog logging.

Note: This new configuration will have no effect on any deliver daemons
that are currently running.  You will have to kill them and restart them
by hand.  As well, you should make sure that there are deliver daemons
watching all the channels that you have configured by modifying the
/etc/rc scripts appropriately.

END_OF_CONFIG
exit 0
