#! /bin/sh
# SCCS file install_querycm rev 1.3 created 1/4/94 at 15:18:58

# Copyright 1993 by Amdahl Corporation, Sunnyvale, CA.
#
#                       All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software
# and its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appears in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of
# Amdahl not be used in advertising or publicity pertaining to
# distribution of the software without specific prior written
# permission. Amdahl Corp. makes no representations or warranty,
# either express or implied, with respect to this software, its
# quality, performance, or about the suitability of this software
# for any purpose.
#
# AMDAHL CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO
# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  AS A
# RESULT, THIS SOFTWARE IS PROVIDED "AS IS" AND YOU THE USER ARE
# ASSUMING THE ENTIRE RISK AS TO ITS QUALITY AND PERFORMANCE.  IN
# NO EVENT SHALL AMDAHL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#################################################################
# PLEASE MODIFY THE FOLLOWING TO INDICATE THE SOURCE AND TARGET #
# DIRECTORIES FOR NEWCM_D                                       #
#################################################################

# SRCDIR should be set to the directory containing the compiled querycm_d.
SRCDIR=/usr/local/src/newcm_d-1.1

# TGTDIR should be set to the directory where querycm_d should be installed.
TGTDIR=/usr/etc


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

QCM=$SRCDIR/querycm_d

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

if [ ! -x $QCM ]; then
  echo The executable $QCM does not exist... Exiting...
  exit 1
fi

echo "querycm_d - Copyright 1993 Amdahl Corporation"
echo "$0 installs the newcm_d assistant daemon in $TGTDIR"
echo "from $SRCDIR, and modifies /etc/inetd.conf to start it up"
echo "upon a request from newcm_d."

cp $QCM $TGTDIR/querycm_d
chmod 0700 $TGTDIR/querycm_d

ENTRY=`grep "^300319/" /etc/inetd.conf`
if [ $? -eq 0 ]
then
#
# inetd.conf has an entry already; probably upgrading the daemon.
# remove the entry and readd the new entry. The next stuff must
# start in the first column.
#
        sed "s/^300319\/1      d/#300319/
t add
s/^300319\/1      s/#300319/
t add2
b noop
:add
a\\
300319/1      dgram   rpc/udp wait root $TGTDIR/querycm_d     querycm_d
d
b noop
:add2
a\\
300319/1      stream  rpc/tcp wait root $TGTDIR/querycm_d     querycm_d
d
:noop" < /etc/inetd.conf > /etc/inetd.conf+
        mv /etc/inetd.conf+ /etc/inetd.conf
else
  cat >> /etc/inetd.conf <<ADD_THIS
#
# querycm_d is a partner daemon to newcm_d which works to trick rpc.cmsd
# into having centralized calendar manager servers.
300319/1      dgram   rpc/udp wait root $TGTDIR/querycm_d    querycm_d
300319/1      stream  rpc/tcp wait root $TGTDIR/querycm_d    querycm_d
ADD_THIS
fi

PID=`ps ax | grep 'querycm_d' | grep -v grep`

if [ "$PID" != "" ]
then
  echo "Bringing down querycm_d process..."
  echo ""
  set $PID
  kill $1
fi

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 partially completed."
else
   set $PID
   kill -HUP $1
   echo "Installation is completed and inetd is set up for the new daemon."
fi

