#!/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.
#

#pragma ident  "@(#)S25sunatm 1.1   03/12/12 SMI"

# Use this file to start and stop ATM network manually from multiuser mode.
# 	sunatm <start | stop>
#
# This file is also located in /etc/rcS.d/S25sunatm
# There is another ATM configuration file in /etc/rc2.d/S00sunatm.
# Do not use the sunatm file in rc2.d to start/stop your ATM network interface.

#PATH=/sbin:/usr/bin:/usr/sbin:/etc/opt/SUNWconn/atm/bin
PATH=/sbin:/etc/opt/SUNWconn/atm/bin
export PATH

mode=$1

# If we can't find /usr/bin/id, we're most likely running during
# boot, and /usr isn't mounted yet.  In that case, we're sure to
# be root, so this check is not necessary, as well as not possible!
if [ -f /usr/bin/id ]; then
	set `/usr/bin/id`
	if [ $1 != "uid=0(root)" ]; then
		echo "$0: must be run as root"
		exit 1
	fi
fi

umask 022

usage() {
	echo "  $0 start      (plumb all atm interfaces)"
	echo "  $0 stop       (unplumb all atm interfaces)"
	echo " "
}

case "$mode" in
'start')

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

	if [ ! -f /kernel/drv/ba ]; then
		echo "Please install <SUNWatm>."
		exit
	fi
	if [ ! -d /etc/opt/SUNWconn/atm/bin ]; then
		echo "Cannot find ATM utilities in /etc/opt/SUNWconn/atm/bin; \c"
		echo "exiting S00sunatm."
		exit
	fi
	if [ ! -f /etc/opt/SUNWconn/atm/atmconfig ]; then
		echo "Cannot find atmconfig file in /etc/opt/SUNWconn/atm; \c"
		echo "exiting S00sunatm."
		exit
	fi

	# do plumbing and ifconfig up classical ip interfaces
	/etc/opt/SUNWconn/atm/bin/atmsetup /etc/opt/SUNWconn/atm/atmconfig
	if [ $? -lt 0 ]; then
		echo "WARNING: could not plumb atm interfaces."
	fi

	# if we are at run level 2 or 3, then start up daemons and 
	# setup ATM. This was added to allow a user to invoke a single script 
	# to plumb and setup ATM from a multiuser run level.
	if [ -z "$_INIT_RUN_LEVEL" ]; then
		set -- `/usr/bin/who -r`
	 	_INIT_RUN_LEVEL="$7"
	fi

	if [ $_INIT_RUN_LEVEL -eq 2 -o $_INIT_RUN_LEVEL -eq 3 ]; then
		/etc/rc2.d/S00sunatm start
	fi
	;;

'stop')
	# atmifconfig fails if either of the setup
	# programs is running, so kill them first.
	/usr/bin/pkill aarsetup
	/usr/bin/pkill lanesetup

	iflist=`/etc/opt/SUNWconn/atm/bin/atmgetunits ba -s`
	for ifname in $iflist; do
		/etc/opt/SUNWconn/atm/bin/atmifconfig $ifname unplumb-all
	done

	/usr/bin/pkill ilmid
	/usr/bin/pkill atmsnmpd
	exit 0
	;;

*)
	usage
	exit 1
	;;

esac
