#!/bin/sh
#
### BEGIN INIT INFO
# Provides:                   slmodemd
# Required-Start:             $remote_fs
# X-UnitedLinux-Should-Start: $syslog
# Required-Stop:              $remote_fs
# X-UnitedLinux-Should-Stop:  $syslog
# Default-Start:              2 3 5
# Default-Stop:               0 1 6
# Short-Description: SmartLink Modem Driver
# Description:       User space part of winmodem driver

### END INIT INFO

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_status -s     display "skipped" and exit with status 3
#      rc_status -u     display "unused" and exit with status 3
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
#      rc_active	checks whether a service is activated by symlinks
#      rc_splash arg    sets the boot splash screen to arg (if active)
. /etc/rc.status

prog=slmodemd
prog_path=/usr/sbin/$prog
RETVAL=0

# Default configuration
SLMODEMD_DEVICE=slamr0
SLMODEMD_COUNTRY=GERMANY

# Source configuration
CONFIG=/etc/sysconfig/$prog
if [ -f $CONFIG ]; then
        . $CONFIG
fi

# uncomment this if you want this feature (if necessary edit module pattern):
# do not try to start on a kernel which does not support it
# grep -q 'slamr\.o' /lib/modules/`uname -r`/modules.dep || exit 0

start() {
	echo -n "Starting SmartLink Modem driver: "
		OPTS=" --country=$SLMODEMD_COUNTRY"
	if [ "$SLMODEM_USE_ALSA" = yes ];then
		OPTS="$OPTS -a $SLMODEMD_DEVICE"
	else
		OPTS="$OPTS /dev/$SLMODEMD_DEVICE"
		modprobe slamr
		modprobe slusb
		for i in `seq 0 3`; do mknod -m 0600 /dev/slamr$i c 242 $i; done &>/dev/null
		for i in `seq 0 15`; do mknod -m 0600 /dev/slusb$i c 243 $i; done &>/dev/null
		sleep 3
	fi
	startproc -l /var/log/$prog -s $prog_path $OPTS
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
	return $RETVAL
}

stop() {
	echo -n "Shutting down SmartLink Modem driver: "
	killproc $prog
	RETVAL=$?
	if [ "$SLMODEM_USE_ALSA" != yes ];then
		modprobe -r slamr slusb
	fi
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	echo -n "Status of SmartLink Modem driver: "
	checkproc $prog_path
	rc_status -v
	rc_exit
	;;
  restart|reload)
	stop
	start
	RETVAL=$?
	;;
  condrestart)
        if [ -f /var/lock/subsys/$prog ]; then
		stop
		start
		RETVAL=$?
	fi
	;;
  *)
	echo "*** Usage: $prog {start|stop|status|restart|condrestart}"
	exit 1
esac

rc_status -v
rc_exit
