#! /bin/sh
#

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/slmodemd
NAME=slmodemd
PIDFILE=/var/run/$NAME.pid
DESC="SmartLink modem daemon"

test -x $DAEMON || exit 0

# You need to define options there there
test -r /etc/default/slmodemd && . /etc/default/slmodemd

# or here
test -r /etc/default/sl-modem-daemon && . /etc/default/sl-modem-daemon

set -e

if test "$DONTSTART" = 1 ; then
   test "$BEQUIET" = 1 || echo "Not starting $DESC (options not set in /etc/default/slmodemd)"
   exit 0
fi

if test "$SLMODEMD_COUNTRY" ; then
   OPTS="$OPTS -c $SLMODEMD_COUNTRY"
fi

if test "$SLMODEMD_DEVICE" ; then
   OPTS="$OPTS $SLMODEMD_DEVICE"
fi


# old module versions did not need the daemon
if test "$FORCESTART" != 1 && grep -q 2.7.10 /lib/modules/`uname -r`/misc/slmdm
o  ; then
   test "$BEQUIET" = 1 || echo "Not starting $DESC (not needed for this driver 
ersion)"
   exit 0
fi

# old module versions did not need the daemon
# do not try to start on a kernel which does not support it
grep 'slamr\..*o' /lib/modules/`uname -r`/modules.dep > /dev/null || { \

  echo "SmartLink modem driver not supported by Kernel `uname -r`."
  echo "(Driver not installed or not needed for this driver version). Exiting ..."
  exit 0
}

start() {
        cat /proc/modules | grep 'slamr' >/dev/null || { 
	  echo -n "Loading SmartLink Modem driver into kernel ... " 
	  modprobe slamr && echo "done." || { i
	    echo "failed."
	    exit -1
	  }
	}
  if test "$SLMODEMD_DEVICE" && ! test -e /dev/$SLMODEMD_DEVICE ; then
     mknod /dev/$SLMODEMD_DEVICE c 212 ${SLMODEMD_DEVICE#slamr}
  fi
 	echo -n "Starting SmartLink Modem driver for: $SLMODEMD_DEVICE"
	start-stop-daemon --start --pidfile $PIDFILE --exec $DAEMON --make-pidfile --background --quiet -- $OPTS
	RETVAL=$?
}

stop() {
	echo -n "Shutting down SmartLink Modem driver normally"
	ps -A | grep $NAME >/dev/null 2>/dev/null && {
          start-stop-daemon --stop --quiet --pidfile $PIDFILE --exec $DAEMON ||
	  {
  	    echo " probably failed."
 	    echo -n "Trying it the hard way (send SIGKILL all $NAME processes): " 
	    killall -KILL $NAME
	    /bin/true
	    RETVAL=0
	  } 
	} || { echo -n " ... no $NAME daemon running"; RETVAL=0; }
	
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload)
	stop
        echo ""
        rm $PIDFILE
	start
	;;
  *)
        echo "Usage: /etc/init.d/$NAME {start|stop|restart}"
	exit 1
esac

									
if [ $? == 0 ]; then
  echo "."
  if [ $1 == "stop" ]; then
    rm $PIDFILE &>/dev/null
  fi
  exit 0
else
  echo " failed."
  exit -1
fi
