#! /bin/sh
#set -x
#
# @(#)install_dni_agent.sh 7.5 91/02/11 SMI
#
# installs the DNI agent for SunNet Manager on this local host, the network 
# management gateway. 
#
# usage:
#       install_dni_agent
#
# return values: 
#
# 	exit 0: ok
#	exit with any other positive number: failure


#** Copyright (c) 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.


#
# Here are some defaults
#
# default root place for agent
DEFPLACE="/usr/sunlink/dni/agent"
# default root of place to run SunNet Manager
DEFDIR="/usr/snm"
# default view to put new systems.  Change this to wherever you
# want icons for newly defined systems to be placed.
DEFVIEW="Home"
# agent file we check for installation
CHECK="na.dni"

PATH=/usr/ucb:/bin:/usr/bin:/etc:/usr/etc
export PATH
 
# version of DNI
VERS="7.0"
 
ARCH=`arch -k`
HOSTNAME=`hostname`
CHANGED="N"
 
ME=`whoami`
if [ "$ME" != "root" ]
then
    echo "You need to be root to execute this script."
    exit 1
fi
 
#
# say hello
#
echo
echo " -- SunLink DNI $VERS agent installation -- "
echo "Copyright (c) 1990 by Sun Microsystems, Inc."
echo
echo
echo "There are two parts to the DNI agent installation:"
echo
echo " - part one: installing the agent on this local host ($HOSTNAME) and"
echo
echo " - part two: including DNI agent information on the SunNet Manager"
echo "   station itself."
echo 
echo
echo
echo
echo "Now we are starting part one of the agent installation script."
echo
echo "****************************************************************"
echo
echo "The agent files are located in $DEFPLACE. If you want to copy all"
echo -n "these files to a different location, specify it now: [$DEFPLACE] "
read PLACE
if [ "$PLACE" = "" ]
then
  PLACE="$DEFPLACE"
else
  CHANGED="Y"
fi

echo
echo

if [ ! -d "$PLACE" ]
then
    echo -n "$PLACE doesn't exist, should I create it? [y] "
    read ANS
    if [ "$ANS" != "n" ]
    then
      install -d $PLACE
      if [ ! -d "$PLACE" ]
      then
        echo "Hmm.  I couldn't make directory $PLACE."
        echo "Nothing was changed."
        exit 2
      else
        echo "OK, $PLACE was made."
      fi
    else
      echo "OK."
      exit 3
    fi
fi
 
if [ "$CHANGED" = "Y" ]
then
	if [ -r "$PLACE/$CHECK" ]
	then
   	  echo
    	  echo "The agent is already in $PLACE on $HOSTNAME."
    	  echo -n "Do you want to scribble over it? [n] "
    	  read ANS
    	  if [ "$ANS" != "y" ]
    	  then
      	    echo "OK, we'll leave them alone.  Nothing was changed."
      	    exit 4
    	  else
      	    echo "OK, we'll install new ones."
    	  fi
	fi
    	cp -p $DEFPLACE/na.* $PLACE
    	cp -p $DEFPLACE/*.schema $PLACE
    	cp -p $DEFPLACE/*.icon* $PLACE
    	cp -p $DEFPLACE/lib* $PLACE
    	cp -p $DEFPLACE/choice* $PLACE
fi

if [ ! -r "/etc/snm.conf" ]
then
	cp -p $DEFPLACE/snm.conf /etc/snm.conf
fi

if [ ! -r "$PLACE/$CHECK" ]
    then
      echo
      echo "I couldn't copy $CHECK so I assume the whole copy failed."
      if [ "$CHANGED" = "Y" ]
      then
        rm -rf $PLACE
        echo "I removed $PLACE."
      else
        echo "Check $PLACE to see what's in there now."
      fi
      exit 5
fi

#
# Set up shared libraries
#

ranlib $PLACE/libnetmgt.sa.1.0 2>&1 /dev/null
ranlib $PLACE/libnice.sa.$VERS 2>&1 /dev/null

if [ ! -f /usr/lib/libnetmgt.sa.1.0 -a ! -h /usr/lib/libnetmgt.sa.1.0 ]
then
  # make links into /usr/lib
  echo
  echo -n "Creating library links into /usr/lib ... "
  ln -s $PLACE/libnetmgt.sa.1.0 /usr/lib/libnetmgt.sa.1.0
  ln -s $PLACE/libnetmgt.so.1.0 /usr/lib/libnetmgt.so.1.0
  ln -s $PLACE/libnice.so.$VERS /usr/lib/libnice.so.$VERS
  ln -s $PLACE/libnice.sa.$VERS /usr/lib/libnice.sa.$VERS
  if [ ! -f /usr/lib/libnetmgt.sa.1.0 -a ! -h /usr/lib/libnetmgt.sa.1.0 ]
  then
    echo
    echo "The agent requires the libraries in /usr/lib.  They aren't there"
    echo "and I wasn't able to create links there, so you'll have to do it"
    echo "yourself, then say 'ldconfig'."
    echo
    echo "I didn't remove anything under $PLACE."
    exit 6
  fi
else
  echo
  echo "WARNING: I didn't expect to find /usr/lib/libnetmgt.s{a,o}.1.0, but"
  echo "they're there.  Please make sure you've got the right versions."
fi
 
# reload ld.so's library cache
ldconfig

#
# Make /var/adm/snm directory for administrative and spool files - users
# can change it later by editing the appropriate entries in /etc/snm.conf.
#
# We create the directory mode 777 so anyone can use the application tools
# which write logs into the admin directory.
#
echo
echo -n "Creating /var/adm/snm directory ... "
install -d -m 777 /var/adm/snm
echo "done."

#
# Add agent to /etc/rpc.  Hosts running NIS (via ypbind) don't look at
# this file, they use the rpc.bynumber map.  But we update the file anyway.
#
echo
echo "Saving your old /etc/rpc into /etc/rpc.$$"
cp /etc/rpc /etc/rpc.$$

echo -n "Adding agent definitions to /etc/rpc ... "
ex - /etc/rpc <<SNMEOF >/dev/null
g/na\.dni/d
$
a
DNInode		100124	na.dni	DNInet
.
w
q
SNMEOF
echo "done."

#
# Edit /etc/inetd.conf to add instructions on how to start agents
#
echo
echo "Saving your old /etc/inetd.conf into /etc/inetd.conf.$$"
cp /etc/inetd.conf /etc/inetd.conf.$$

echo -n "Adding agents to /etc/inetd.conf ... "
ex - /etc/inetd.conf <<SNMEOF >/dev/null
g/na\.dni/d
$
a
na.dni/10       dgram 	rpc/udp wait root $PLACE/na.dni	na.dni
.
w
q
SNMEOF
echo "done."

#
# Tell inetd it's got new customers
#
echo
echo -n "Asking inetd to re-read its config file... "
INETD=`ps ax | egrep inetd | egrep -v egrep | head -1 | awk '{print $1}'`
kill -HUP $INETD
echo "done."

echo "We are done with the first part of the agent installation."
echo
./install_dni_manager

exit 0
