:
#      @(#)sendmail-init	5.1.1.3 Lachman System V STREAMS TCP  source
#      SCCS IDENTIFICATION
#
#	Copyright (C) The Santa Cruz Operation, 1988.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation, and Lachman Associates Inc. 
#	and should be treated as Confidential.
#
#
: ${OK=0} ${FAIL=1}
PATH=/bin:/usr/bin:/etc
tmp=/tmp/tcp$$
plist=/etc/perms/tcprt			# permlist of this product
sederror=
movelist="./bin/rmail ./usr/bin/lmail ./usr/bin/mailstats ./usr/bin/mconnect
./usr/lib/sendmail ./usr/lib/mail/execmail ./usr/lib/mail/aliases"
holddir=./usr/lib/sendmail.d		# dir where replacement files are held
savedir=/usr/lib/tcprt/save		# dir where old versions get saved

# Function Definitions

#
# A shell function substitute for dirname (not available in the basic package)
#
fdirname() {
	#
	# dirname_X is a sed command which will delete trailing
	# instances of "/..." from a string
	#
	# dirname_Y is the result string
	#
	dirname_X='s!\(.*\)/[^/][^/]*!\1!'
	dirname_Y=`echo $1 | sed -e ${dirname_X}`

	if [ -z "${dirname_Y}" -o "$1" = "/" ]
	then
		echo /
	elif [ "$1" = "${dirname_Y}" ]
	then
		echo .
	else
		echo ${dirname_Y}
	fi
}

# Print an error message
# Usage: error "message"
# Argument is a quoted error message
error() {
	echo "\nError: $*" >&2
	return 1
}

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

# Find and replace a string in the product permlist
# Usage: sedperm ident find replace
# where ident is a unique string identifying the line to act on,
# and find is the string to replace and replace is the new string
sedperm() {
	ident=$1    find=$2    replace=$3
	if ( sed "/${ident}/s!${find}!${replace}!" $plist > $tmp.plist )
	then
		# Permlist edit was successful, so safely update permlist
		safecopy $tmp.plist $plist && rm -f $tmp.plist
		return 0
	else				
		return 1
	fi	
}

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

# main()
cd /
#
# Clean up and exit after signals
trap "cleanup 1" 1 2 3 15

if [ -f /tmp/tcprt_upgrade ]
then
	#
	# In inplace upgrade, do not prompt user to abort.  The 
	# Sendmail init script is usually the only file that will
	# call "mkdev sendmail-init" during an inplace upgrade.
	#
	echo ""
	echo "Configuring the mail system to use sendmail instead of MMDF..."
	echo "Using the preserved sendmail.cf file..."
else
	#
	# Give them a chance to change their mind
	#
	/bin/echo "WARNING: Sendmail and MMDF are mutually exclusive."
	/bin/echo "Continuing to initialize sendmail will disable MMDF."
	/bin/echo "Do you wish to continue (y/n) [n]? \c"
	read answer
	case "$answer" in
	    [yY]* ) ;;
	    "" | * ) cleanup 0 ;;
	esac
fi

#
# Some of the files we are installing replace or get in the way of
# (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.
#
echo "saving the following files:"
for i in $movelist ./etc/inetd.conf
do
	echo $i
done

if [ ! -d $savedir ]
then
	mkdir $savedir
fi

cp /dev/null $savedir/.sndmailpaths
cp /dev/null $savedir/.sndmaillist
for file in $movelist
do
	base=`basename $file`
	#
	# if the file already exists, back it up
	#
	if [ -f $file ]
	then 
	    echo $base $file >> $savedir/.sndmailpaths
	    safemv $file $savedir/$base || {
		safecopy file $savedir/$base
		rm -f $file
	    }
	fi

	#
	# In any case, move the new one into place,
	# edit the perm list file to reflect the new location,
	# and remember what we have done so it can be undone upon removal.
	#
	filedir=`fdirname $file`
	if [ ! -d $filedir ]
	then
		mkdir $filedir
	fi
	safemv $holddir/$base $file
	sedperm $base $holddir/$base $file
	echo $base $file $holddir/$base >> $savedir/.sndmaillist
done
#
# get rid of reference to sendmail.d directory
#
sedperm "D.*sendmail\.d$" "^" "#"
#
# Override MMDF
#
echo inetd ./etc/inetd.conf $holddir/inetd.conf >> $savedir/.sndmaillist
safecopy /etc/inetd.conf $savedir/inetd.conf
if ( sed "/smtp/D" /etc/inetd.conf > $tmp.inetd )
then
	mv $tmp.inetd /etc/inetd.conf
	chown bin /etc/inetd.conf
	chgrp bin /etc/inetd.conf
	chmod 644 /etc/inetd.conf
else
	echo "Unable to remove MMDF configuration from /etc/inetd.conf"
	echo "Sendmail will not work unless the smtp line is removed from"
	echo "or commented out of /etc/inetd.conf"
fi
ln ./usr/lib/sendmail ./usr/bin/mailq
ln ./usr/lib/sendmail ./usr/bin/newaliases
chgrp mail /usr/spool/mail/*
chmod 01777 /usr/spool/mail/*
echo
echo "The mail system will now use sendmail for delivering messages."
[ ! -f /tmp/tcprt_upgrade ] && {
	echo "\nPress <Return> to continue: \c"
	read garbage
	[ -f /usr/lib/mkdev/cf ] && sh /usr/lib/mkdev/cf
}
cleanup 0
