# installation script for sendmail.cf and related files.
# get domainname
#	different than on from line?
# get uname
# get configuration
#	is another machine a gateway
#		yes - change named.hosts
#		no  - nothing
#	are there local uucp connections
#		yes - add lines to sendmail.cf
#		no  - nothing
#
#

uname=$1
ldomain=$2
sendmailcf=/usr/lib/sendmail.cf
namedhosts=/usr/local/domain/named.hosts


# This routine was taken from setup_driver
# Prompt for yes or no answer - returns non-zero for no
askyn() {
	while	echo "$* (y/n) \c">&2
	do	read yn rest
		case $yn in
		     [yY]) return 0 		                  ;;
	             [nN]) return 1		                  ;;
	             [q])  exit 1		                  ;;
		        *) echo "\nPlease answer y, n, or q" >&2  ;;
		esac
	done
}

# This routine was taken from setup_driver
# input - $1 = "must" or other,  $2 = String to display
# output - value in "$ansvalue"
askvalue() {
	isitok=
	while [ "$isitok" = ""  ]
	do
		echo "Please enter $2 > \c"
		read ansvalue
		if [ "$ansvalue" = "" ]
		then
			if [ "$1" = "must" ]
			then
				echo "\tYou must give an answer"
			else
				isitok=ok
			fi
		else
			echo "\t$2 = $ansvalue"
			askyn "\tIs this OK?" && {
				# answered yes
				isitok=ok
			}
		fi
	done
}


#
# Setup various network files
#

# echo "\nHost's official name is $uname\n"
# 
# # get domain name
# while
# 	test "$ldomain" = ""
# do
# 	echo "enter name of domain the host is in: \c"
# 	read ldomain
# done
# 
# echo "The host's domain name is: $ldomain"
# askyn "\tIs this correct?"  || {
# 	askvalue "must" "domain name" && {
# 		LODOMAIN=$ansvalue
# 	}
# }

#
# Find names of locally connected uucp machines and  put them in the 
# sendmail.cf file
#
other=
tmpuul=/tmp/uulocal
rm $tmpuul > /dev/null 2>&1

#
# first look in the uucp Systems file
# look for machine names which are not on the Ethernet.
# Hosts on the Ethernet will use the tcp connection for mail.
#
#@echo "\nLooking for locally uucp connected machines\c"
egrep -v "^#|Ether" /usr/lib/uucp/Systems | while read uulocal rest
do
	if echo $uulocal $rest | > /dev/null 2>&1 
	then
		if [ "$uulocal" != "" ]
		then
			echo "\nDo you want mail to be routed to $uulocal \c"
			echo "via uucp? \c"
			askyn ""  < /dev/tty && {
				echo "CU$uulocal" >> $tmpuul
				echo $uulocal will be added to $sendmailcf
				other=" other"
			}
		fi
	fi
done

#
# See if we need to add mail gateway machines.
# These names will be added to the named.hosts file as mail forwarders
# We will not try to be too ambitious here and add specific gateways
# for specific networks or hosts.
#
#ansvalue=
#
#while [ "$ansvalue" = ""  ]
#do
#	echo "\nDo you want mail to be routed to any$other hosts via uucp?\c"
#	askyn "" && {
#		askvalue "other" "locally connected uucp host" && {
#		echo "CU$ansvalue" >> $tmpuul
#		other=" other"
#		ansvalue=
#		}
#	} || ansvalue=ok
#done
#
#
# find names of other mail gateways on the net.
#
tmpmgw=/tmp/mgw
rm $tmpmgw > /dev/null 2>&1

ansvalue=
while [ "$ansvalue" = ""  ]
do
	echo "\nIs another local machine a mail gateway"
	echo "\tto other machines/networks? \c"
	askyn "" && {
		askvalue "other" "mail gateway machine" && {
	    echo "*		IN	MX	0	$ansvalue" >> $tmpmgw
		ansvalue=
		}
	} || ansvalue=ok
done

#
# Done getting the info, now edit the sendmail.cf file and the
# named.hosts file.
#
if [ -f "$tmpuul" ]
then
cp $sendmailcf $sendmailcf.sv
ed - $sendmailcf  > /dev/null 2>&1 <<!
1,\$s/XHOSTNAME/$uname/
1,\$s/XDOMNAME/$ldomain/
/^CU
.r $tmpuul
w
q
!
rm $tmpuul
fi

if [ -f "$tmpgw" ] 
then
	cp $namedhosts $namedhosts.sv
	cat $tmpmgw >> $namedhosts
	rm $tmpmgw
fi

if [ ! -f /usr/lib/mailx/mailx.rc ]
then
cat <<! >> /usr/lib/mailx/mailx.rc
set sendmail=/usr/lib/sendmail
!
fi

if [ ! -f /usr/lib/aliases ]
then
cat <<! > /usr/lib/aliases
MAILER-DAEMON:root
postmaster:root
#ADD USERS HERE TO BE ALIASED 
# eg: joe: bob@386
!
cp /dev/null /usr/lib/aliases.dir
cp /dev/null /usr/lib/aliases.pag
chmod 644 /usr/lib/sendmail.cf /usr/lib/aliases* /usr/lib/mailx/mailx.rc
fi

