#!/bin/sh  
#
# /etc/rc.d/init.d/atm  -  Bring up/down ATM daemons and interfaces
#
# chkconfig:	2345 11 90
# description:  ATM services
#

# Source function library
. /etc/rc.d/init.d/functions

# Get network config
. /etc/sysconfig/network

# Get basic ATM config
[ -f /etc/sysconfig/atm ] && . /etc/sysconfig/atm
is_yes "$ATM" || exit 0

# Check that networking is up.
if is_no "${NETWORKING}" ; then 
	msg_network_down ATM
	exit 1
fi

if [ ! -f /proc/net/atm/devices ]; then
	echo "Error: /proc not mounted or no ATM support in kernel"
	exit 1
fi

load_atmdevice()
{
	if [ "`cat /proc/net/atm/devices | wc -l`" -lt 2 ]; then
		modprobe atm0 2>/dev/null >/dev/null
		ATMLOAD=$?
		modprobe lec0 2>/dev/null >/dev/null
		LECLOAD=$?
		if [ $ATMLOAD -ne 0 -a $LECLOAD -ne 0 ]; then
			echo "Error: No ATM devices present and cannot load modules"
			exit 1
		fi
	fi
}

interfaces_atm_boot=$((cd /etc/sysconfig/interfaces && ls -1 ifcfg-atm* | \
egrep 'ifcfg-[a-z0-9]+$' | xargs egrep -l "ONBOOT=[^n][^o]" | \
awk ' { gsub(/ifcfg-/,NIL); print $0 } ') 2> /dev/null)

interfaces_lec_boot=$((cd /etc/sysconfig/interfaces && ls -1 ifcfg-lec* | \
egrep 'ifcfg-[a-z0-9]+$' | xargs egrep -l "ONBOOT=[^n][^o]" | \
awk ' { gsub(/ifcfg-/,NIL); print $0 } ') 2> /dev/null)

interfaces_nas_boot=$((cd /etc/sysconfig/interfaces && ls -1 ifcfg-nas* | \
egrep 'ifcfg-[a-z0-9]+$' | xargs egrep -l "ONBOOT=[^n][^o]" | \
awk ' { gsub(/ifcfg-/,NIL); print $0 } ') 2> /dev/null)

# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
	if [ -f /var/lock/subsys/atm ]; then
		msg_already_running ATM
		exit 1
	fi
	load_atmdevice
	if is_yes "$SIGNALLING" ; then
		msg_starting "ATM signalling"
		daemon atmsigd -b -c /etc/atm/atmsigd.conf \
		  `[ "$SIGNALLING_DEBUG" = "yes" ] && echo " -d "`
	fi
	if is_yes "$ILMI" ; then
		for i in `seq 0 \`expr ${ATM_NICS_NUMBER:-1} - 1\`` ; do
			msg_starting "ATM ILMI on NIC $i"
			if [ -f /etc/atm/ilmi.conf ]; then
				. /etc/atm/ilmi.conf
				eval ILMI_LOGFILE="\$ILMI_LOGFILE_${i}"
				eval ILMI_QOS="\$ILMI_QOS_${i}"
				eval ILMI_DEBUG="\$ILMI_DEBUG_${i}"
				eval ILMI_UNI="\$ILMI_UNI_${i}"
				eval ILMI_LOCAL_IP="\$ILMI_LOCAL_IP_${i}"
				daemon ilmid -b -l $ILMI_LOGFILE \
				  `[ -z "$ILMI_QOS" ] || echo "-q $ILMI_QOS "` \
				  `[ "$ILMI_DEBUG" = "yes" ] && echo "-d "` \
				  `[ -z "$ILMI_UNI" ] || echo "-u $ILMI_UNI "` \
				  `[ -z "$ILMI_LOCAL_IP" ] || echo "-i $ILMI_LOCAL_IP "` \
				  $i
			else
				daemon ilmid -b -l syslog $i
			fi
		done
	fi
	if is_yes "$LANE" ; then
		modprobe lec
		if is_yes "$LANE_SERVER_SERVICES" ; then
			msg_starting "ATM LES"
			daemon les -f /etc/atm/lane.conf &
			msg_starting "ATM BUS"
			daemon bus -f /etc/atm/lane.conf &
			msg_starting "ATM LECS"
			daemon lecs -f /etc/atm/lecs.conf &
		fi
		for i in $interfaces_lec_boot ; do
			run_cmd -a "$(nls 'Bringing up interface') $i" /sbin/ifup $i boot
		done
	fi
	if is_yes "$CLIP" ; then
		msg_starting "ATM CLIP"
		daemon atmarpd -b
		for i in $interfaces_atm_boot ; do
			run_cmd -a "$(nls 'Bringing up interface') $i" /sbin/ifup $i boot
		done
		touch /var/lock/subsys/atm-clip
	fi
	if is_yes "$BR2684" ; then
		_modprobe single br2684
		for i in $interfaces_nas_boot ; do
			run_cmd -a "$(nls 'Bringing up interface') $i" /sbin/ifup $i boot
		done
	fi
	touch /var/lock/subsys/atm
	;;
  stop)
	if is_yes "$BR2684" ; then
		for i in $interfaces_nas_boot ; do
			run_cmd -a "$(nls 'Shutting down interface') $i" /sbin/ifdown $i boot
		done
	fi
	if [ -f /var/lock/subsys/atm-clip ] ; then
		for i in $interfaces_atm_boot ; do
			run_cmd -a "$(nls 'Shutting down interface') $i" /sbin/ifdown $i boot
		done
		msg_stopping "ATM CLIP"
		killproc atmarpd
		rm -f /var/lock/subsys/atm-clip
	fi
	if is_yes "$LANE" ; then
		for i in $interfaces_lec_boot ; do
			run_cmd -a "$(nls 'Shutting down interface') $i" /sbin/ifdown $i boot
		done
		/sbin/rmmod lec
	fi
	if is_yes "$LANE_SERVER_SERVICES" ; then
		msg_stopping "ATM LECS"
		killproc lecs
		msg_stopping "ATM BUS"
		killproc bus
		msg_stopping "ATM LES"
		killproc les
	fi
	msg_stopping "ATM signalling"
	killproc atmsigd
	msg_stopping "ATM ILMI"
	killproc ilmid
	rm -f /var/lock/subsys/atm
	;;
  restart)
        $0 stop
        $0 start
        ;;
  *)
        msg_usage "$0 {start|stop|restart}"
        exit 1
esac

exit 0
