#!/bin/sh  
#
# /etc/rc.d/init.d/atm  -  Bring up/down basic ATM daemons
#
# chkconfig:	2345 10 91
# 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
[ "$ATM" != "yes" ] && exit 0

# Check that networking is up.
if [ "$NETWORKING" != "yes" ]; then 
    echo "Networking is down - skipping ATM subsystem"
    exit 1
fi

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

# shutting down has to be forced

load_atmdevice()
{
	if [ "`cat /proc/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
}

# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
	if [ -f /var/lock/subsys/atm ]; then
		echo "ATM is already running"
		exit 1
	fi
	load_atmdevice
	if [ "$SIGNALLING" = "yes" ]; then
		show "ATM: starting signalling daemon"
		daemon atmsigd -b -c /etc/atm/atmsigd.conf \
		  `[ "$SIGNALLING_DEBUG" = "yes" ] && echo " -d "`
	fi
	if [ "$ILMI" = "yes" ]; then
		show "ATM: starting ILMI daemon"
		if [ -f /etc/atm/ilmi.conf ]; then
			. /etc/atm/ilmi.conf
			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 "`
		else
			echo "ILMI config file not found - using defaults"
			daemon ilmid -b -l syslog
		fi
	fi
	if [ "$LANE" = "yes" ] ; then
		modprobe lec
		for i in /etc/sysconfig/interfaces/ifcfg-lec* ; do
			ifup `basename $i | sed 's/ifcfg-//'`
		done
	fi
	if [ "$CLIP" = "yes" ]; then
		show "ATM: starting CLIP daemon"
		daemon atmarpd -b
		for i in /etc/sysconfig/interfaces/ifcfg-atm* ; do
			ifup `basename $i | sed 's/ifcfg-//'`
		done
		touch /var/lock/subsys/atm-clip
	fi
	sleep 1
	touch /var/lock/subsys/atm
	;;
  stop)
	if [ -f /var/lock/subsys/atm-clip ] ; then
		for i in /etc/sysconfig/interfaces/ifcfg-atm* ; do
			ifdown `basename $i | sed 's/ifcfg-//'`
		done
		show "ATM: terminating CLIP daemon"
		killproc atmarpd
		rm -f /var/lock/subsys/atm-clip
	fi
	if [ "$LANE" = "yes" ] ; then
		for i in /etc/sysconfig/interfaces/ifcfg-lec* ; do
			ifdown `basename $i | sed 's/ifcfg-//'`
		done
		rmmod lec
	fi
	show "ATM: terminating signalling daemon"
	killproc atmsigd	# kill ILMID later, so we don't get complaints
	show "ATM: terminating ILMI daemon"
	killproc ilmid
	rm -f /var/lock/subsys/atm
	;;
  restart)
        $0 stop
        $0 start
        ;;
  *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

exit 0

