#!/bin/bash
#
# /etc/init.d/load-fcdslsl
#
# FRITZ!Card DSL SL loader for SuSE Linux 8.2
#
### BEGIN INIT INFO
# Provides:       isdn-loader
# Required-Start: 
# Required-Stop:  $network
# X-UnitedLinux-Should-Stop: hylafax smpppd capisuite
# Default-Start: 2 3 5
# Default-Stop:  0 1 6
# Description:   CAPI and FRITZ!Card DSL SL loader service
### END INIT INFO

. /etc/rc.status

ctrl=/proc/capi/controller
ware=/usr/lib/isdn/fdssbase.bin
rexp="fcdslsl[ \t]"
cmd=$1

rc_reset


is_running () {
	#
	test -f $ctrl && cat $ctrl | grep "$rexp" > /dev/null
}

		
case $cmd in

start)	echo -n "Starting FRITZ!Card DSL SL loader"
	{
		# Loading CAPI...
		echo "# Dummy capi.conf to load CAPI modules..."
		echo "# (generated by $0)"
	} > /etc/fcdslsl.conf
	if test -f $ware; then
		{
			capiinit -c /etc/fcdslsl.conf start && insmod fcdslsl
			ret=$?
		} > /dev/null 2>&1
		if [ $ret -eq 0 ]; then
			n=`cat $ctrl | grep "$rexp" | awk '{ print $1 }'`
			echo -n " (ctrl $n)"
			avmcapictrl load $ware $n > /dev/null 2>&1
			ret=$?
		fi
		rc_failed $ret
	else
		rc_failed 1
	fi
	rc_status -v
	;;

stop)	echo -n "Stopping FRITZ!Card DSL SL"
	if is_running; then
		n=`cat $ctrl | grep "$rexp" | awk '{ print $1 }'`
		echo -n " (ctrl $n)"
		{
			avmcapictrl reset $n
			avmcapictrl remove $n
			rmmod fcdslsl
			rc_failed $?
		} > /dev/null 2>&1
	else
		rc_failed 1
	fi
	test -f /etc/fcdslsl.conf && rm -f /etc/fcdslsl.conf
	rc_status -v
	;;

status)	ret=1
	if test -f $ctrl; then
		status=`cat $ctrl | grep "$rexp" | awk '{ print $3 }'`
		if [ "$status" == "running" ]; then
			ret=0
		fi
		echo -n "FRITZ!Card DSL SL is $status"
	else
		echo -n "FRITZ!Card DSL SL is not running"
		ret=3
	fi
	rc_failed $ret
	rc_status -v
	;;

	
*)	rc_failed 1
	rc_status -v
	;;

esac

rc_exit
