#! /bin/sh
#ident "@(#)opensrc:src/sendmail/SCO/manage_sendmail /main/uw714mp/3"
#
# Copyrighted as an unpublished work.
# (c) Copyright 1987-1994 Lachman Technology, Inc.
# All rights reserved.
#
#      SCCS IDENTIFICATION
#
#	Copyright (C) The Santa Cruz Operation, 1988-2000.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation, and Lachman Technology, Inc.
#	and should be treated as Confidential.
#
#
# mkdev cf:  makes sendmail configuration file "sendmail.cf"
#
# initialize variables
PATH=/bin:/usr/bin:/etc
UUGW="n"
SMART="n"
NIS="n"
MASQAS="n"
MH=`uname -n`
H=$MH
ZZNAMELISTZZ=$H
ZZUUCPZZ=$MH
OS=`uname -v`
if [ "$OS" = "7.1.4" ]
then
	OS="uw714"
	MMDF="mmdftailor-"
else
	OS="osr6"
	MMDF="mmdftailor"
fi

if [ -f /etc/mail/sendmail.ans ] ; then
	. /etc/mail/sendmail.ans
fi
[ -z "$ZZSMARTZZ" ] && ZZSMARTZZ="n"
[ -z "$ZZMASQASZZ" ] && ZZMASQASZZ="n"
[ -z "$ZZUUGWZZ" ] && ZZUUGWZZ="n"
[ -z "$ZZNISZZ" ] && ZZNISZZ="n"
[ "$ZZNISDOMAINZZ" = "n" ] && unset ZZNISDOMAINZZ

#
#
# EDUUCP() edit UUCP connections list
eduucp() {
clear
echo "Edit UUCP connections list\n"
echo "If you have a UUCP Gateway on the net, Enter the list manually, and"
echo "include the machines the UUCP Gateway connects to in the list.\n"
echo "	1. Enter list manually"
echo "	2. Get list from UUCP configuration files\n\n"
echo "Enter choice: \c"
read choice
case $choice in
	1)	clear
		echo "Enter UUCP connections list manually\n"
		echo "Enter machine names separated by spaces (i.e. > foo bar uunet ): "
		echo "> \c"
		U=`line` ;;
	2)	echo "Retrieving names using uuname..."
		u=`uuname`
		U=`echo $u` ;;
esac
echo "\n\nYour UUCP connections list looks like this:"
echo $U
ZZUUCPHOSTZZ=$U
echo "\n\n\n\nPlease hit RETURN to continue\c"
read choice
}

# usage: safecopy file1 file2
# copies file1 to file2 without chance of interruption
safecopy() {
	trap "" 1 2 3 15
	cp $1 $2
	trap 1 2 3 15
}

eddom() {
clear
echo "Edit DOMAIN \n"
echo "	1. Enter DOMAIN manually"
echo "	2. Get DOMAIN from \"hostname\" utility\n\n"
echo "Enter choice: \c"
read choice
case $choice in
	1)	clear
		echo "Enter DOMAIN manually"
		echo "> \c"
		read D ;;
	2)	echo "Retrieving DOMAIN using hostname..."
		hn=`hostname`
		D=`echo $hn | sed -e "s/^$MH\.\(.*\)/\1/"`;;
esac
if [ -z "$D" ]
then
	D="local.domain"
fi
echo "\n\nYour host's DOMAIN looks like this:"
echo $D
ZZDOMAINZZ=$D
cp /usr/mmdf/$MMDF /tmp/oy$$
ed - /tmp/oy$$ <<-EOF > /dev/null 2>&1
/^MLNAME/
.d
.i
MLNAME $MH
.
/^MLDOMAIN/
.d
.i
MLDOMAIN $D
.
w
q
EOF
safecopy /tmp/oy$$ /usr/mmdf/$MMDF
echo "\n\n\n\nPlease hit RETURN to continue\c"
read choice
}

ednis() {
clear
echo "NIS Configuration\n"
echo "Do you wish to use NIS aliases with sendmail? \c"
getyn
if [ $yn = y ]; then
	if [ ! -x /usr/bin/domainname ]; then
		echo "NIS does not appear to be installed."
		echo "Please install NIS and re-run \"mkdev cf\"."
		echo "\n\n\n\nPlease hit RETURN to continue\c"
		read choice
		return
	fi
	ZZNISDOMAINZZ=`/usr/bin/domainname`
	if [ -z "$ZZNISDOMAINZZ" ]
	then
		echo "NIS does not appear to be configured."
		echo "Please configure NIS and re-run \"mkdev cf\"."
		echo "\n\n\n\nPlease hit RETURN to continue\c"
		read choice
		return
	fi
	echo "\n\nYour NIS domain is: " $ZZNISDOMAINZZ
	NIS=y
else
	NIS=n
fi
echo "\n\n\n\nPlease hit RETURN to continue\c"
read choice
}

edalt() {
clear
echo "Edit Alternate host names\n"
echo "	1. Enter Alternate host names manually"
echo "	2. Get Alternate host names from \"uname\" \n\n"
echo "Enter choice: \c"
read choice
case $choice in
	1)	clear
		echo "Enter Alternate host names manually"
		echo "Remember to fully qualify them."
		echo "> \c"
		read H ;;
	2)	echo "Retrieving Alternate host name using uname..."
		A=`uname -n`
		H=
		for i in $A; do
			H=$H" "$i" "$i.$ZZDOMAINZZ
		done
esac
echo "\n\nYour host's Alternate names look like this:"
echo $H
ZZNAMELISTZZ=$H
echo "\n\n\n\nPlease hit RETURN to continue\c"
read choice
}
edmis() {
while true
do
clear
echo "Edit Miscellaneous Information\n"
echo "	1. Is there a UUCP gateway on your network? [$ZZUUGWZZ] "
echo "	2. Is there a smart mailhost on your network? [$ZZSMARTZZ] "
echo "	3. Masquerade as a different host? [$ZZMASQASZZ] "
echo "	4. Return to main menu"
echo "\n\nEnter choice: \c"
read choice
case $choice in
	1)	clear
		echo "Is there a UUCP gateway (other than this machine) in the net? \c"
		getyn
		UUGW=$yn
		if [ X$UUGW = Xy ]; then
			echo "If so, what is it called? \c"
			read ZZUUGWZZ
		else
			ZZUUGWZZ="n"
		fi;;
	2)	clear
		echo "Is there a smart mailhost on your network? (y or n) \c"
		getyn
		SMART=$yn
		if [ X$SMART = Xy ]; then
			echo "If so, what is it called? \c"
			read ZZSMARTZZ
		else
			ZZSMARTZZ="n"
		fi;;
	3)	clear
		echo "Do you want outgoing mail to appear as if from another host name? (y or n) \c"
		getyn
		MASQAS=$yn
		if [ X$MASQAS = Xy ]; then
			echo "If so, what is it called? \c"
			read ZZMASQASZZ
		else
			ZZMASQASZZ="n"
		fi;;
	4)	break ;;
esac
done
}
# prompt for yes or no answer - returns non-zero for no
getyn() {
	while	read yn
	do	case $yn in
		[y])	return 0 			;;
		[n])	return 1			;;
		*)	echo "please enter either y or n" 	;;
		esac
	done
}

review() {
ZZFULLNAMEZZ=`hostname`
clear
echo "Sendmail configuration information...\n"
echo "  Machine name:			   	${ZZFULLNAMEZZ:-NOT SET}"
echo "  DOMAIN name:			  	${ZZDOMAINZZ:-NOT SET}"
echo "  UUCP connections:			${ZZUUCPHOSTZZ:-NOT SET}"
echo "  Alternate names:			${ZZNAMELISTZZ:-NOT SET}"
echo "  Is there a UUCP gateway on the net?	${ZZUUGWZZ:-NOT SET}"
echo "  Is there a smart mailhost on the net?	${ZZSMARTZZ:-NOT SET}"
echo "  Masquerade as a different host?		${ZZMASQASZZ:-NOT SET}"
echo "  Use NIS aliases with sendmail?	${ZZNISDOMAINZZ:-NOT SET}"
echo "\n\nPlease review this configuration.  If any of the fields above"
echo "are listed as \"NOT SET\", please configure them."
echo "\n\nPlease hit RETURN to continue"
read junk
}

generate() {
	review

	if [ -z "$ZZDOMAINZZ" ]
	then
		echo "Can not proceed without setting Domain"
		return
	fi

	echo "Do you wish to change anything (y/n)? \c"
	getyn
	if [ $? -eq 0 ]
	then
		return
	fi

	cd /etc/mail
	chmod 644 cf/cf/sco.$OS.mc
	ed - cf/cf/sco.$OS.mc <<!EOF! >/dev/null 2>&1
		g/confDOMAIN_NAME/s/,\`[^\']*\'/,\`$ZZDOMAINZZ\'/
		w
		q
!EOF!
	if [ "$ZZDOMAINZZ" = "local.domain" ]
	then
		ZZDOMAINZZ=
	fi
	if [ "$SMART" = "y" -a -n "$ZZSMARTZZ" ]
	then
		ed - cf/cf/sco.$OS.mc <<!EOF! >/dev/null 2>&1
			g/SMART_HOST/d
			/^MAILER/i
define(\`SMART_HOST',\`$ZZSMARTZZ')dnl
.
			w
			q
!EOF!
	fi

	if [ "$MASQAS" = "y" -a -n "$ZZMASQASZZ" ]
	then
		ed - cf/cf/sco.$OS.mc <<!EOF! >/dev/null 2>&1
			g/allmasquerade/d
			g/masquerade_envelope/d
			g/MASQUERADE_AS/d
			/^MAILER/i
FEATURE(\`masquerade_envelope')dnl
MASQUERADE_AS(\`$ZZMASQASZZ')dnl
.
			w
			q
!EOF!
	fi
	
	if [ -n "$ZZNAMELISTZZ" ]
	then
		>/etc/mail/local-host-names
		for name in $ZZNAMELISTZZ
		do
			echo "$name" >>/etc/mail/local-host-names
		done
	fi

	if [ "$ZZUUGWZZ" != "n" -o -n "$ZZUUCPHOSTZZ" ]
	then
		ed - cf/cf/sco.$OS.mc <<!EOF! >/dev/null 2>&1
			g/^FEATURE(\`nouucp\')/d
			g/^MAILER(\'uucp\')/d
			$
			?^MAILER(.*smtp.*)?a
MAILER(\`uucp')dnl
.
			w
			q
!EOF!
	fi
	if [ -n "$ZZUUCPHOSTZZ" ]
	then
		>/etc/mail/mailertable
		for name in $ZZUUCPHOSTZZ
		do
			echo "$name	uucp-old:$name" >>/etc/mail/mailertable
		done
	fi
	if [ "$ZZUUGWZZ" != "n" ]
	then
		ed - cf/cf/sco.$OS.mc <<!EOF! >/dev/null 2>&1
		g/^define(\`UUCP_RELAY\'/d
		/^MAILER/i
define(\`UUCP_RELAY',\`$ZZUUGWZZ')dnl
.
		w
		q
!EOF!

	fi
	if [ -n "$ZZNISDOMAINZZ" ]
	then
		ed - cf/cf/sco.$OS.mc <<!EOF! >/dev/null 2>&1
		g/^define(\`UUCP_RELAY\'/d
		/^MAILER/i
define(\`ALIAS_FILE',\`$ZZNISDOMAINZZ')dnl
.
		w
		q
!EOF!
	fi

	make
	echo "# Answers last updated: `date`" > /tmp/sendmail.ans
	echo ZZNISDOMAINZZ=$ZZNISDOMAINZZ >> /tmp/sendmail.ans
	echo ZZDOMAINZZ=$ZZDOMAINZZ >> /tmp/sendmail.ans
	echo ZZFULLNAMEZZ=$ZZFULLNAMEZZ >> /tmp/sendmail.ans
	echo ZZNAMELISTZZ=\"$ZZNAMELISTZZ\" >> /tmp/sendmail.ans
	echo ZZSMARTZZ=$ZZSMARTZZ >> /tmp/sendmail.ans
	echo ZZMASQASZZ=$ZZMASQASZZ >> /tmp/sendmail.ans
	echo ZZUUGWZZ=$ZZUUGWZZ >> /tmp/sendmail.ans
	echo ZZUUCPZZ=$ZZUUCPZZ >> /tmp/sendmail.ans
	echo ZZUUCPHOSTZZ=\"$ZZUUCPHOSTZZ\" >> /tmp/sendmail.ans
	echo UUGW=$UUGW >> /tmp/sendmail.ans
	echo SMART=$SMART >> /tmp/sendmail.ans
	echo MASQAS=$MASQAS >> /tmp/sendmail.ans
	echo NIS=$NIS >> /tmp/sendmail.ans

chmod 644 /etc/mail/sendmail.cf

mv /tmp/sendmail.ans /etc/mail/sendmail.ans

/bin/echo "New sendmail.cf installed in /etc/mail\n"
echo "Hit return to continue\c"
read l
# initialize alias database now that we have a configuration file
if [ ! -f /etc/mail/aliases.db ]; then
	newaliases > /dev/null 2>&1 &
fi

echo "killing sendmail"
sh /etc/init.d/sendmail stop 2>/dev/null

sleep 5

echo "restarting sendmail"
sh /etc/init.d/sendmail start 

}

#
# main loop of mkdev cf
#

while true
do
	clear
	echo "SCO Sendmail Configuration Utility\n"
	echo "	1. Edit UUCP Connections"
	echo "	2. Edit Domain"
	echo "	3. NIS Support"
	echo "	4. Edit Alternate Host Names"
	echo "	5. Miscellaneous Items"
	echo "	6. Review configuration information"
	echo "	7. Generate sendmail.cf file"
	echo "	8. Quit"
	echo "\n\nEnter choice: \c"
	read choice
	case $choice in
		1)	eduucp  ;;
		2)	eddom   ;;
		3)	ednis   ;;
		4)	edalt	;;
		5)	edmis   ;;
		6)	review  ;;
		7)	generate ;;
		8|q|Q)	break ;;
	esac
done
exit 0
