:
#      @(#)mkdev.smtp	5.4.1.1 Lachman System V STREAMS TCP  source
#      SCCS IDENTIFICATION
#
#	System V STREAMS TCP - Release 4.0
#
#	Copyright 1987, 1988, 1989 Lachman Associates, Incorporated (LAI)
#
#	All Rights Reserved.
#
#	The copyright above and this notice must be preserved in all
#	copies of this source code.  The copyright above does not
#	evidence any actual or intended publication of this source
#	code.
#
#	This is unpublished proprietary trade secret source code of
#	Lachman Associates.  This source code may not be copied,
#	disclosed, distributed, demonstrated or licensed except as
#	expressly authorized by Lachman Associates.
#
#	System V STREAMS TCP was jointly developed by Lachman
#	Associates and Convergent Technologies.
#
#
: ${OK=0} ${FAIL=1}
PATH=/bin:/usr/bin:/etc:/etc/conf/bin

tmp=/tmp/smtp$$
MMDFPERM=/etc/perms/ext			# OS (link kit) permlist
set="MMDF SMTP Channel"			# appears in prompts
movelist="/etc/inetd.conf /usr/mmdf/mmdftailor"
holddir=./tmp/backupmmdf	# dir where replacement files are held
savedir=/usr/lib/custom/save	# dir where old versions get saved

##############################################################################
#
# Remove temp files and exit with the status passed as argument
#
cleanup() {
	trap '' 1 2 3 15
	[ "$tmp" ] && rm -f $tmp*
	exit $1
}

##############################################################################
#
# Prompt with mesg, return non-zero on q. Also allows setting -x and +x,
# shell escapes and the passing of default values
#
# Usage: prompt "message" [default]
# Argument is a quoted message
# Dependencies: expr
# Notes: returns 1 if the user answers q or Q, returns 0 otherwise
#	 if $quit is not set, it is set to "quit"
#	 $cmd is set to whatever is input by the user, and then can
#		subsequently be used in the shell script.
#
prompt() {
	_mesg=$1
	while	echo "${_mesg} [$2]: \c" >&2
	do	read cmd
		case $cmd in
		+x|-x)	set $cmd					;;
		Q|q)	return 1					;;
		!*)	eval `expr "$cmd" : "!\(.*\)"`			;;
		"")	# If there is an second argument use it
			# as the default else loop until 'cmd' is set
			[ "$2" ] && { 
				cmd=$2
				return 0
			}
			: continue
			;;
		*)	unset _mesg
			return 0					;;
		esac
	done
}

##############################################################################
#
# usage: safemv file1 file2
# move file1 to file2 without chance of interruption
#
safemv() {
	trap "" 1 2 3 15
	mv $1 $2
	trap 1 2 3 15
}

##############################################################################
#
# 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
}

##############################################################################
#
# Prompt for yes or no answer with message passed as argument - 
# returns 1 for no; 2 for quit
#
getyn() {
	while	echo "\n$* (y/n/q) \c">&2
	do	read yn rest
		case $yn in
			[yY]) return 0				;;
			[nN]) return 1				;;
			[qQ]) return 2				;;
		*)	echo "Please answer y,n or q" >&2	;;
		esac
	done
}

##############################################################################
#
# Test to see if the MAIL Package is installed
#
chkmmdf() {
	until fixperm -i -d MAIL $MMDFPERM 2> /dev/null
	do
		ANSWER=$?
		[ -n "$_no_prompt" ] && return 1
		case $ANSWER in
		3|4) echo "The MAIL Package is not installed." >&2
		     ;;
		5)   echo "The MAIL Package is only partially installed." >&2
		     ;;
		*)   echo "Error testing for MAIL Package. Exiting."
		     cleanup $FAIL
		     ;;
		esac
		# Not fully installed. Do so here
		getyn "Do you wish to install it now?"
		ANSWER=$?
		case $ANSWER in
			0) custom -o -i MAIL
			     break
			     ;;
			1) echo "The SMTP Channel cannot be installed without the Operating System MAIL package."
			     cleanup $FAIL	# Answer = no
			     ;;
			*) cleanup $FAIL	# Answer = quit
			     ;;
		esac
	done
	return 0
}

##############################################################################
#
# Some of the files we are installing replace or modify
# (possibly) existing XENIX programs, or other
# precious files that the user may not want clobbered.
# Move them out of the way here, and move ours into place.
#
move_files()
{
	if [ -n "$movelist" ] ; then
		[ -z "$_no_prompt" ] && echo "\nSaving files in $savedir ...\c"
		if [ ! -d $savedir ] ; then
			/bin/su root -c "mkdir $savedir"
		fi
		cp /dev/null $savedir/.mmdfpaths
		linecount="3"
	fi

	for file in $movelist
	do
		base=`basename $file`
		#
		# if the file already exists, back it up
		#
		if [ -f $file ] ; then
			linecount=`expr $linecount + 1`
			if [ $linecount = "4" ] ; then 
				linecount="1"
				[ -z "$_no_prompt" ] && echo "\n" $file "\c"
			else
				[ -z "$_no_prompt" ] && echo "\t" $file "\c"
			fi
		    	echo $base $file >> $savedir/.mmdfpaths
		    	safecopy $file $savedir/$base || {
				safecopy $file $savedir/$base
				rm -f $file 2> /dev/null
			}
		fi
	done

	if [ -n "$movelist" ] ; then
		[ -z "$_no_prompt" ] && echo "\n"
		rmdir $holddir 2> /dev/null
	fi
}

##############################################################################
#
# Let's add our smtp listener to inetd.conf
#
add_smtp() {
	file=/etc/inetd.conf
	if [ -r $file -a -w $file ]
	then
		grep smtp $file > /dev/null 2>&1 || \
		echo "smtp\tstream\ttcp\tnowait\tmmdf\t/usr/mmdf/chans/smtpd smtpd /usr/mmdf/chans/smtpsrvr smtp" >> $file
		[ -z "$_no_prompt" ] && echo "Listener Configured."
	else
		if [ -f $file ]
		then
			echo "MMDFTCP: $file: Permission Denied."
		else
			echo "MMDFTCP: $file: No such file or directory."
		fi
		cleanup $FAIL
	fi
}

##############################################################################
#
# Let's edit /usr/mmdf/mmdftailor to add in the smtp channel.
#
edit_mmdf() {
	file=/usr/mmdf/mmdftailor
	if [ -r $file -a -w $file ]
	then
		# Okay, let's sed in our changes
		grep smtp $file > /dev/null 2>&1 || \
		    if sed \
    '/^MTBL	*local/a\
MTBL	smtpchn,	file="smtp.chn",	show="SCO SMTP Channel"
    /^	pgm=local,/a\
MCHN	smtp, show="SCO SMTP Delivery", que=smtp, tbl=smtpchn, ap=822,\
	pgm=smtp, mod=imm' $file > /tmp/tailor$$
		   then
			mv /tmp/tailor$$ $file
			chown mmdf $file
			chgrp mmdf $file
			chmod 644 $file
			#
			# Rebuild the mmdf database
			#
			cd /usr/mmdf/table
			[ -z "$_no_prompt" ] && echo "Rebuilding the mmdf database ... \c"
			./dbmbuild || cleanup $FAIL
       			[ -z "$_no_prompt" ] && echo "done."
			cd /
		    else
			echo "MMDFTCP: sed of mmdftailor file failed." 
			cleanup $FAIL
		    fi
	else
		if [ -f /usr/mmdf/mmdftailor ]
		then
			echo "MMDFTCP: /usr/mmdf/mmdftailor: Permission Denied."
			echo "MMDFTCP: Cannot edit /usr/mmdf/mmdftailor."
		else
			echo "MMDFTCP: /usr/mmdf/mmdftailor: No such file or directory."
			echo "MMDFTCP: Cannot edit /usr/mmdf/mmdftailor."
		fi
		cleanup $FAIL
	fi
	[ -z "$_no_prompt" ] && echo "MMDF System Configured."
}

##############################################################################
#
# main()
#
_no_prompt=
_option=

for i in $*
do
	if [ $i = -n -o $i = -N ] ; then
		_no_prompt=TRUE
	fi
	if [ $i = -d -o $i = -D ] ; then
		_option=d
	fi
	if [ $i = -i -o $i = -I ] ; then
		_option=i
	fi
done
[ -n "$_no_prompt" -a -z "$_option" ] && cleanup $FAIL
	
if [ -z "$_no_prompt" ] ; then
	prompt "\n\nDo you wish to install or delete the MMDF SMTP channel (i/d/q)" "i"
	_option=$cmd
fi
	
case $_option in
	i|I)	
		# See if MMDF is installed
		cd /
		chkmmdf
		if [ $? ] ; then
			# Save any files that are in danger of being overwritten
			move_files

			# Let's add our smtp listener to inetd.conf
			add_smtp

			# Let's edit /usr/mmdf/mmdftailor to add smtp channel
			edit_mmdf
		else
			cleanup $FAIL
		fi
		cleanup $OK
		;;

	d|D)
		# Deconfigure MMDFTCP
		grep -v smtp /etc/inetd.conf > /tmp/inetd
		cp /tmp/inetd /etc/inetd.conf
		rm /tmp/inetd
		grep -v smtp /usr/mmdf/mmdftailor > /tmp/tailor
		cp /tmp/tailor /usr/mmdf/mmdftailor
		rm /tmp/tailor
		CWD=`pwd`
		cd /usr/mmdf/table
		if [ -f ./dbmbuild ] ; then
			[ -z "$_no_prompt" ] && echo "\nRebuilding mmdf database to remove SMTP channel ... \c"
			./dbmbuild
			[ -z "$_no_prompt" ] && echo "done."
		fi
		cd $CWD
		cleanup $OK
		;;

	*)	cleanup $FAIL
		;;
esac
