#! /bin/sh
#
#  @(#)install_cmgr	1.8 6/19/90
#
#  Copyright (c) 1987-1990 Sun Microsystems, Inc.
#  All Rights Reserved.
#
#  Sun considers its source code as an unpublished, proprietary
#  trade secret, and it is available only under strict license
#  provisions.  This copyright notice is placed here only to protect
#  Sun in the event the source is deemed a published work.  Dissassembly,
#  decompilation, or other means of reducing the object code to human
#  readable form is prohibited by the license agreement under which
#  this code is provided to the user or company in possession of this
#  copy.
#
#  RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the
#  Government is subject to restrictions as set forth in subparagraph
#  (c)(1)(ii) of the Rights in Technical Data and Computer Software
#  clause at DFARS 52.227-7013 and in similar clauses in the FAR and
#  NASA FAR Supplement.
#
#
#  install_cmgr is a script to install Calendar Manager Service Daemon from
#  Openwindow directory.  The /etc/inetd.conf may be modified to initiate the
#  daemon by inetd.  After inetd.conf has been changed, a signal HUP is sent
#  to inetd.  This script should be run for the first time installation or
#  after a Deskset upgrade.
#

PATH="/bin:/usr/ucb:/usr/bin:/etc"
export PATH

if [ `whoami` != "root" ]
then
	echo "install_cmgr: Must be run in super-user mode."
	exit 1
fi

if [ "$OPENWINHOME" = "" ]
then
	echo 'Installation failed because $OPENWINHOME is undefined.'
	echo 'Please set $OPENWINHOME and rerun this script.'
	exit 1
fi

DEFAULT=/usr/etc
OPENDIR=$OPENWINHOME/bin/xview
CMSD=$OPENDIR/rpc.cmsd

PID=`ps ax | grep 'rpc.cmsd$' | grep -v grep`
if [ "$PID" != "" ]
then
	echo "Bringing down rpc.cmsd process..."
	echo ""
	set $PID
	kill $1
fi

ENTRY=`grep "^100068/2" /etc/inetd.conf`
if [ $? -eq 0 ]
then
	#
	# inetd.conf has an entry already; probably upgrading the daemon.
	#
	set $ENTRY
	if [ "$CMSD" != "$6" ]
	then
		#
		# The regular expression for expr comes from dirname(1V)
		#
		/bin/echo -n "Do you want to install rpc.cmsd into" `expr ${6-.}'/' : '\(/\)[^/]*//*$' \| ${6-.}'/' : '\(.*[^/]\)//*[^/][^/]*//*$' \| .`"? [y|n] "
		read ANSWER
		echo ""
		case $ANSWER in
		y*|Y*)	echo "Copying $CMSD into $6..."
			cp -p $CMSD $6
			if [ $? -ne 0 ]
			then
				echo "Installation failed."
				exit 1
			fi;;
		*)	echo "Please delete the rpc.cmsd entry from /etc/inetd.conf, remove"
			echo "$6, and then rerun this script."
			exit 1;;
		esac
	fi
else
	#
	# inetd.conf does not have an entry; probably first time installation.
	#
	echo "Where would you like rpc.cmsd installed? [default is 1]"
	echo '	1) Leave in $OPENWINHOME/bin/xview'
	echo "	2) Copy to /usr/etc"
	echo "	3) Copy to a specified directory"
	read ANSWER
	if [ "$ANSWER" = "1" -o "$ANSWER" = "" ]
	then
		DAEMON=$OPENDIR
	elif [ "$ANSWER" != "2" -a "$ANSWER" != "3" ]
	then
		echo "Invalid input.  It must be 1, 2, or 3"
		exit 1
	else
		if [ "$ANSWER" = "2" ]
		then
			DAEMON=$DEFAULT
		else
			echo "Enter the directory name: [default is $DEFAULT]"
			read DAEMON
			if [ "$DAEMON" = "" ]
			then
				DAEMON=$DEFAULT
			elif [ ! -d $DAEMON ]
			then
				echo "Installation failed because $DAEMON is not a directory."
				exit 1
			fi
		fi

		echo ""
		echo "Copying $CMSD into $DAEMON..."
		cp -p $CMSD $DAEMON/rpc.cmsd
		if [ $? -ne 0 ]
		then
			echo "Installation failed."
			exit 1
		fi
	fi

	cat >> /etc/inetd.conf <<ADD_THIS
#
# The Calendar Manager server lets authorized users to browse/edit personal
# calendar.
#
100068/2	dgram	rpc/udp	wait root $DAEMON/rpc.cmsd	rpc.cmsd
ADD_THIS
fi

#
# Notify inetd to read the new inetd.conf
#
PID=`ps ax | grep 'inetd$' | grep -v grep`
if [ "$PID" = "" ]
then
	echo "Unable to notify inetd to read the new inetd.conf."
	echo "Please issue 'kill -HUP pid-of-inetd' manually to reset inetd."
	echo "Installation is partial completed."
else
	set $PID
	kill -HUP $1
	echo "Installation is completed and inetd is set up for the new daemon."
fi
