#! /bin/sh
#set -x
#
# @(#)install_dni_manager.sh 7.6 91/02/11 SMI
#
# installs the DNI agent information on SunNet Manager Station. 
#
# usage:
#       install_dni_manager
#
# 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="components.schema"

PATH=/usr/ucb:/bin:/usr/bin:/etc:/usr/etc
export PATH
 
# version of DNI
VERS="7.0"
 
LOCAL=`hostname`
LOC="local"
 
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 "Now we are starting part two of the agent installation: we will"
echo "enable the management station to recognize the network management"
echo "gateway."
echo
echo
echo "Since this is a standalone script, you may have to repeat some answers"
echo "that you gave already in part one."
echo
echo "*******************************************************************"
echo
echo "Please type the IP hostname of the machine, that will run the proxy agent: "
echo -n "[$LOCAL]: "
read HOSTNAME
if [ "$HOSTNAME" = "" ]
then
        HOSTNAME="$LOCAL"
fi
echo
echo "Please type the IP hostname of the machine, that is the management station: "
echo -n "[$LOCAL]: "
read MANSTATION
if [ "$MANSTATION" = "" ] 
then
        MANSTATION="$LOCAL" 
fi
echo
echo "Please type the directory location of the agent files: "
echo -n "[$DEFPLACE]: "
read PLACE
if [ "$PLACE" = "" ]
then
	PLACE="$DEFPLACE"
fi
echo

echo
echo

if [ ! -r "$PLACE/$CHECK" ]
then
	echo "I could not find $PLACE/$CHECK. Please, make sure that"
	echo "the files are at the specified location [$PLACE]."
	exit 1
fi

if [ "$MANSTATION" = "$HOSTNAME" ]
then
	if [ "$MANSTATION" = "$LOCAL" ]
	then
		INST="local"
	else
		echo "The hostnames you gave me can not be correct."
		exit 2
	fi
else
	if [ "$MANSTATION" = "$LOCAL" ]
	then
		INST="local"
	else
		INST="remote"
	fi
fi

if [ "$INST" = "remote" ]
then
	rsh $MANSTATION -n "echo 0 > /dev/null"
	if [ "$?" -ne 0 ]
	then
 		echo
    		echo "I can't talk to $MANSTATION.  If it's a permission problem, remember"
    		echo "$MANSTATION needs to have an entry for $HOSTNAME in its /.rhosts"
    		echo "file to allow root access."
		echo
		echo "You can try again or decide to do the installation locally on the"
		echo "management station. In the last case you have to copy the files"
		echo "	*.schema"
		echo "	*.icon*"
		echo "	choice*"
		echo "	install_dni_*"
		echo "to that machine and run install_dni_manager from there."
    		echo
		echo -n "Try again: "
		read MANSTATION
        	rsh $MANSTATION -n "echo 0 > /dev/null" 
        	if [ "$?" -ne 0 ] 
        	then 
        		echo 
			echo "There is still a problem. You can run install_dni_manager"
			echo "later. There is no reboot necessary after running this script."
			exit 3
		fi
	fi
fi

if [ "$INST" = "remote" ]
then
	rcp $MANSTATION:/etc/rpc /var/tmp/rpc
	rcp $MANSTATION:/etc/snm.conf /var/tmp/snm.conf
else
	cp /etc/rpc /var/tmp/rpc
	cp /etc/snm.conf /var/tmp/snm.conf
fi

#
# modify /etc/snm.conf
#
grep "$PLACE" /var/tmp/snm.conf > /dev/null
if [ $? != 0 ]
then
        ./install_awkfile :$PLACE
fi

#
# Modify component.schema file
#
echo
echo -n "Modifying the components.schema file ... "
cp $PLACE/components.schema /var/tmp/components.schema
ex - /var/tmp/components.schema <<COMEOF 
g/elementGlyph/.,\$d
$
a
instance elementCommand (
        (component.vax          "Control"        "$PLACE/choice.script %Name")
        (component.dnisun       "DNI Control"    "$PLACE/choice.script %Name")
        (component.dnisun       "Performance"    "perfmeter %Name")
        (component.nmg          "DNI Control"    "$PLACE/choice.script %Name")
        (component.nmg          "Performance"    "perfmeter %Name")
)

instance elementGlyph(
        ( component.vax                $PLACE/vax.icon)
        ( component.nmg                $PLACE/nmg.icon)
)

instance elementGlyph(
        ( view.decnet                  $PLACE/decnet.icon )
)
.
w
q
COMEOF
echo "done."

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


if [ "$INST" = "remote" ]
then

	if [ ! -d "$MANSTATION:$PLACE" ]
	then
		install -d $MANSTATION:$PLACE
		if [ ! -d "$MANSTATION:$PLACE" ]
		then
			echo " Hmm. I couldn't make directory $MANSTATION:$PLACE."
			exit 4
		fi
	fi
	rcp /var/tmp/rpc $MANSTATION:/etc/rpc
	rcp /var/tmp/snm.conf $MANSTATION:/etc/snm.conf
	rcp /var/tmp/components.schema $MANSTATION:$PLACE
	rcp $PLACE/*.schema $MANSTATION:$PLACE
	rcp $PLACE/*.icon* $MANSTATION:$PLACE
else
        cp /var/tmp/rpc /etc/rpc 
        cp /var/tmp/snm.conf /etc/snm.conf 
        cp /var/tmp/components.schema $PLACE/components.schema 
fi

rm /var/tmp/rpc
rm /var/tmp/snm.conf
rm /var/tmp/components.schema

echo
echo "We are all done with the agent installation. To manage DECnet hosts, just"
echo "create the respective objects in the management database."
echo
exit 0
