#!/bin/sh
#
# slmodemd:    Starts the SmartLink Modem Daemon
#
# chkconfig: - 90 10
# description: SmartLink Modem driver daemon - also loads/unloads driver
#              Also loads the modem driver
#              Sample taken from example debian script and hacked by wdw
#              3 October, 2004
# processname: slmodemd

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

NAME=slmodemd
DRIVER=slamr
DAEMON=/usr/sbin/slmodemd
PIDFILE=/var/run/$NAME.pid
RETVAL=0

# Default configuration
SLMODEMD_DEVICE=slamr0
SLMODEMD_COUNTRY=USA

# print failure and exit w/error
failandexit() {
        failure $"$NAME"
        echo
        exit 1
}

if [ $UID != 0 ] && [ $1 != "status" ]
then
        echo "$0 $1 requires root privileges! "
        failandexit
fi

# Test presence of daemon binary
if ! [ -x $DAEMON ]
then
        echo "$0: $DAEMON not found!"
        failandexit
fi

start() {
        echo -n $"SmartLink modem: loading $DRIVER, starting $NAME: "
        cat /proc/modules | grep $DRIVER >/dev/null
        RETVAL=$?
        if [ $RETVAL -eq 0 ]
        then
           echo
           echo "warning: $DRIVER already loaded! "
        else
           /sbin/modprobe $DRIVER
           RETVAL=$?
           if [ $RETVAL -ne 0 ]
           then
              echo
              echo "modprobe failed!"
              failandexit
           fi
        fi
        pidofproc $NAME >/dev/null 2>/dev/null
        RETVAL=$?
        if [ $RETVAL -eq 0 ]
        then
           echo
           echo "warning: $NAME already running! "
        else
           $DAEMON >/dev/null 2>/dev/null &
           # sample tested retval here - but that is always
           # 0 when started in background
           # testing for a running process (pidofproc) works
           # except at boot time - takes too long to start ??
        fi
        touch /var/lock/subsys/$NAME
        success $"$NAME"
        echo
        return 0
}

# remove and unload what we can...
# don't exit on failure - not fatal so allow restart
# to continue
stop() {
        FAILANDRETURN=0
        echo -n "SmartLink Modem: stopping $NAME, unloading $DRIVER: "
        killproc $NAME >/dev/null 2>/dev/null
        RETVAL=$?
        if [ $RETVAL -ne 0 ]
        then
           echo
           echo "warning: couldn't kill $NAME! (not running ??)"
           echo "attempting to remove $DRIVER anyway! "
           FAILANDRETURN=1
        fi
        /sbin/rmmod $DRIVER
        RETVAL=$?
        if [ $RETVAL -ne 0 ]
        then
           echo
           echo "rmmod $DRIVER failed! "
           FAILANDRETURN=1
        fi
        rm -f /var/lock/subsys/$NAME
        if [ $FAILANDRETURN -ne 0 ]
        then
           failure $"$NAME"
           echo
           return 1
        fi
        success $"$NAME"
        echo
        return 0
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart|reload)
        stop
        start
        RETVAL=$?
        ;;
condrestart)
        if [ -f /var/lock/subsys/$NAME ]
        then
            stop
            start
            RETVAL=$?
        fi
        ;;
status)
        status $NAME
        RETVAL=$?
        ;;
  *)
        echo "Usage: /etc/init.d/$NAME {start|stop|restart|condrestart|status}"
        exit 1
esac

exit $RETVAL