#!/sbin/sh
#    Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
#      All Rights Reserved

#    THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
#    The copyright notice above does not evidence any
#    actual or intended publication of such source code.

#
#    Copyright (c) 1994, Sun Microsystems Inc.
#

#ident @(#)S00sunatm	1.20 97/07/07 

PATH=/sbin:/usr/bin:/usr/sbin:/etc/opt/SUNWatm/bin
export PATH

umask 022

if [ "$1" = "stop" ]; then
	echo ""
	echo "Cannot stop ATM interfaces."
	echo ""
	echo "To disable an ATM interface, remove its entry"
	echo "in /etc/atmconfig, and reboot the system."
	echo ""
	exit
fi

if [ -f /etc/hostname.sa* ]; then
    rm /etc/hostname.sa*
fi

if [ -f /etc/hostname.ba* ]; then
    rm /etc/hostname.ba*
fi

pkginfo SUNWatm > /dev/null 2>&1
if [ $? != 0 ]; then
    if [ ! -f /kernel/drv/ba ]; then
        echo "Please install <SUNWatm>."
        exit
    fi
    if [ ! -d /etc/opt/SUNWatm/bin ]; then
        echo "Please install <SUNWatm>."
        exit
    fi
fi

if [ ! -d /etc/opt/SUNWatm/bin ]; then
    echo "Cannot find ATM utilities in /etc/opt/SUNWatm/bin; \c"
	echo "exiting S00sunatm."
    exit
fi

if [ ! -f /etc/atmconfig ]; then
    echo "Cannot find atmconfig file in /etc; exiting S00sunatm."
    exit
fi

modload /kernel/mod/sscop
modload /kernel/mod/atmip


grep -v \^# /etc/atmconfig | grep "ba[0-9]" > /dev/null 2>&1
if [ $? = 0 ]; then
	# do plumbing and ifconfig up classical ip interfaces
	/etc/opt/SUNWatm/bin/atmsetup -p /etc/atmconfig

	# return value of atmsetup -p indicates type of interfaces plumbed
	case $? in
	1)
		cip=1
		lane=0
		;;
	2)
		cip=0
		lane=1
		;;
	3)
		cip=1
		lane=1
		;;
	*)
		cip=0
		lane=0
		;;
	esac

    # get the OS release
    is26=0
    if [ `/usr/bin/uname -r` = "5.6" ]; then
        is26=1
    fi
 

	# start setup programs and daemons

	# the contents of /etc/opt/SUNWatm/snmp/.agent_status
	# indicate whether we should run as an agent (i.e. bind
	# to port 161).
	# if /etc/opt/SUNWatm/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/SUNWatm/snmp/.agent_status ]; then
		grep "is_agent" /etc/opt/SUNWatm/snmp/.agent_status > /dev/null 2>&1
		if [ $? = 0 ]; then
            PORT=`grep "port" /etc/opt/SUNWatm/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/SUNWatm/snmp/atm.rsrc \
                            /etc/snmp/conf
                    fi
                    if [ ! -f /etc/snmp/conf/atm.reg ]; then
                        /usr/bin/cp /etc/opt/SUNWatm/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/SUNWatm/bin/atmsnmpd -p $PORT
			if [ $? -ne 0 ]; then
				echo "WARNING: could not start atmsnmpd."
			fi
		else
			/etc/opt/SUNWatm/bin/atmsnmpd -n
			if [ $? -ne 0 ]; then
				echo "WARNING: could not start atmsnmpd."
			fi
		fi
	else
		/etc/opt/SUNWatm/bin/atmsnmpd -n 
		if [ $? -ne 0 ]; then
			echo "WARNING: could not start atmsnmpd."
		fi
	fi

	/etc/opt/SUNWatm/bin/ilmid
	if [ $? -ne 0 ]; then
		echo "WARNING: could not start ilmid."
	fi

	if [ $lane -eq 1 ]; then
		/etc/opt/SUNWatm/bin/lanesetup
		if [ $? -ne 0 ]; then
			echo "WARNING: lanesetup failed; \c"
			echo "could not configure LAN Emulation interfaces."
			lane=0
		fi
	fi

	if [ $cip -eq 1 ]; then
		/etc/opt/SUNWatm/bin/aarsetup
		if [ $? -ne 0 ]; then
			echo "WARNING: aarsetup failed; \c"
			echo "could not configure classical IP interfaces."
		fi
	fi

	# if we have lane interfaces to configure, do the lane ifconfigs
	if [ $lane -ne 0 ]; then
		/etc/opt/SUNWatm/bin/atmsetup -i /etc/atmconfig
	else
		echo ""
	fi
fi
