#!/sbin/sh

#pragma ident  "@(#)start_atmsnmpd 1.2   98/08/19 SMI"

# Copyright (C) 1998 Sun Microsystems

# get the OS release; Solaris 2.6 (and above) will have a
# bundled snmp agent, so if the snmpd configuration directory
# is there, we have to run as a sub-agent.

is26=0
if [ -d /etc/snmp/conf ]; then
	is26=1
fi

# the contents of /etc/opt/SUNWconn/atm/snmp/.agent_status
# indicate whether we should run as an agent (i.e. bind
# to port 161).
# if /etc/opt/SUNWconn/atm/snmp/.agent_status does not exist,
# don't bind (run with -n option), otherwise get the user
# specified port (if any) and start the agent.

if [ -f /etc/opt/SUNWconn/atm/snmp/.agent_status ]; then
	grep "is_agent" /etc/opt/SUNWconn/atm/snmp/.agent_status > /dev/null 2>&1
	if [ $? = 0 ]; then
       		PORT=`grep "port" /etc/opt/SUNWconn/atm/snmp/.agent_status | \
			cut -f2 -d' '`
        	change=0
        	if [ "aa$PORT" != "aa" ]; then
				if [ $PORT -lt 0 -o $PORT -gt 65535 ]; then
					change=1
				fi
				if [ $is26 -eq 1 -a $PORT -eq 161 ]; then
					change=1
				fi  
        	else 
				change=1
        	fi

        	if [ $change -eq 1 ]; then
				if [ $is26 -eq 1 ]; then 
					PORT=1000 
				else        
					PORT=161 
				fi
        	fi  

		# if it's a 2.6 system we need to copy
		# over the registration and resource files,
		# and update the registration file with
		# the correct port.
		if [ $is26 -eq 1 ]; then
    		if [ -d /etc/snmp/conf ]; then
				if [ ! -f /etc/snmp/conf/atm.rsrc ]; then
            				/usr/bin/cp \
						/etc/opt/SUNWconn/atm/snmp/atm.rsrc \
						/etc/snmp/conf
        		fi
        		if [ ! -f /etc/snmp/conf/atm.reg ]; then
            			/usr/bin/cp \
					/etc/opt/SUNWconn/atm/snmp/atm.reg \
					/etc/snmp/conf
        		fi

				more /etc/snmp/conf/atm.reg | \
				awk 'BEGIN { FS = ""; RS = "\n" } \
					$0 ~ /port/ \
					{ printf ("\t\tport = %d\n", \
					'$PORT') } \
					$0 !~ /port/ \
					{ print $0 } ' \
					>> out
				mv out /etc/snmp/conf/atm.reg
			fi
		fi

		# now start the agent
		/etc/opt/SUNWconn/atm/bin/atmsnmpd -p $PORT
		if [ $? -ne 0 ]; then
			echo "WARNING: could not start atmsnmpd."
		fi
	else
		/etc/opt/SUNWconn/atm/bin/atmsnmpd -n
		if [ $? -ne 0 ]; then
			echo "WARNING: could not start atmsnmpd."
		fi
	fi
else
	/etc/opt/SUNWconn/atm/bin/atmsnmpd -n 
	if [ $? -ne 0 ]; then
		echo "WARNING: could not start atmsnmpd."
	fi
fi
