#!/bin/sh 
#
#	Copyright (c) 1992 by Sun Microsystems, Inc.
#
#pragma	ident	"@(#)pf_install_agents 1.2     97/12/02 NPI"

PATH="/usr/5bin:/bin:/usr/bin:/sbin:/usr/sbin:/etc:/usr/etc"
export PATH
DATE=`date '+%m%d%y'`
DATE=$$

install_agents()
{
	if [ -d $SNM_PATH/agents ]; then
		cd $NF_PATH
		tar cf -	agents | (cd $SNM_PATH; tar xfp -)
	else
		echo "$SNM_PATH/agents directory not found.  No FDDI agents installed."
		echo "Make sure that you have installed the SunNet Manager agents and libraries"
		echo "on the local machine. Please refer to the documentation."
		exit 3
	fi
}

modify_element_schema()
{
	(
	if [ -d $SNM_PATH/struct ]; then
		cd $SNM_PATH/struct
		if [ -w elements.schema ]; then
			mv elements.schema elements.schema.$DATE
			awk '
			BEGIN {
				state = 0;
			}
			{
				if ($1 == "record" && \
					substr($2, 0, length("component.")) == "component.")
					state = 1;
				else if ($2 == "Name/Mac-Address" && state == 1)
					state = 0;
				else if ($1 == ")" && state == 1) {
					state = 0;
					printf("        string[40]      Name/Mac-Address\n");
				}
				print $0;
			}
			' elements.schema.$DATE > elements.schema
		fi
	fi
	)
}

modify_snm_conf()
{
	if [ -w $SNMCONF ]; then
		cp $SNMCONF $SNMCONF.$DATE
		grep -s "na.fddi.trap-rendez" $SNMCONF >/dev/null
		if [ $? -eq 1 ]; then
			# nomatch
			if [ "aa$hosts" != "aa" ]; then
				echo "" >> $SNMCONF
				echo "na.fddi.trap-rendez		$hosts" >> $SNMCONF
			fi
		else
			# match
			if [ "aa$hosts" = "aa" ]; then
				awk '
				{
					if ($1!="na.fddi.trap-rendez")
						print $0;
				}
				' hosts=$hosts $SNMCONF.$DATE > $SNMCONF
			else
				awk '
				{
					if ($1=="na.fddi.trap-rendez")
						printf("%s		%s\n", $1, hosts);
					else
						print $0;
				}
				' hosts=$hosts $SNMCONF.$DATE > $SNMCONF
			fi
		fi
	else
		echo "" >> $SNMCONF
		echo "# fddi event trap rendez hosts" >> $SNMCONF
		echo "na.fddi.trap-rendez		$hosts" >> $SNMCONF
	fi
}

echo "Enter hostnames (if any) to send SRF Trap reports to: "
echo "(enter blank to terminate)"
while [ 1 ]; do
	echo   "	enter hostname: \c"
	read h
	if [ "aa$h" = "aa" ]; then
		break;
	fi
	if [ "aa$hosts" = "aa" ]; then
		hosts="$h"
	else
		hosts="$hosts:$h"
	fi
done

ETCDIR=/etc/opt/snm
SNM_ROOTPATH=/opt
NF_ROOTPATH=/
if [ ! -d /etc/opt ]; then
	mkdir -m 775  /etc/opt
fi
if [ ! -d /etc/opt/SUNWconn/snm ]; then
	mkdir -m 755  /etc/opt/SUNWconn/snm
fi
if [ ! -h /etc/opt/snm ]; then
	ln -s SUNWconn/snm /etc/opt/snm
fi
SNM_ROOTPATH=`ckstr -Q -d "$SNM_ROOTPATH" \
	-p "What is the root directory under which the SunNet Manager is installed? [$SNM_ROOTPATH]: "`

SNM_PATH=$SNM_ROOTPATH/SUNWconn/snm

NF_ROOTPATH=`ckstr -Q -d "$NF_ROOTPATH" \
	-p "What is the root directory under which the pf FDDI software is installed? [$NF_ROOTPATH]: "`

NF_PATH=$NF_ROOTPATH/etc/opt/SUNWconn/pf
if [ ! -d $NF_PATH ]; then
	echo "FDDI related agents schema files are not found\n"
	echo "SunNet Manager agents for SunFDDI is not installed"
	exit 1
fi
SNMCONF=$ETCDIR/snm.conf

install_agents
if [ "aa$hosts" = "aa" ];  then
	echo  "No hostnames entered for sending SRF Traps"
fi
modify_snm_conf
modify_element_schema
echo "SunNet Manager agents for SunFDDI installed correctly."
