#!/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"
modprobe="$(cat /proc/sys/kernel/modprobe 2>/dev/null || echo modprobe)"

test -x $DAEMON || exit 0

# there may be old options there...
test -r /etc/default/slmodemd && . /etc/default/slmodemd

# but most likely they are here
test -r /etc/default/sl-modem-daemon && . /etc/default/sl-modem-daemon

ALSADEVPREFIX=modem
driverlist="snd-intel8x0m snd-via82xx-modem snd-atiixp-modem"

# must match the names in /proc/modules with the underscores etc., includes slamr too
unloadllist="snd_intel8x0m snd_via82xx_modem snd_atiixp_modem slamr"

alsaload() {

    unset line;

    # first check to not do unneccessary modprobe calls. Some people even put
    # it into the kernel image

    if ! line=$(grep -i "\[Modem   " /proc/asound/cards 2>/dev/null) ; then
        for x in $driverlist ; do $modprobe $x 2>/dev/null ; done

        for start_reps in `seq 100` ; do
            test -e /proc/asound/cards && line=$(grep -i "\[Modem   " -i /proc/asound/cards) && break
            sleep 0.1
        done
    fi

    if test "$line" ; then
        # ALSA driver is loaded, use it

        set $line
        SLMODEMD_DEVICE=$ALSADEVPREFIX:$1
        ALSA=yes
        return 0
    fi

    return 1
}

config() {
   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

   ALSA=`echo $SLMODEMD_DEVICE | grep :`
   
   if [ "`echo $SLMODEMD_DEVICE | grep auto`" ] ; then

       # carefully look for signs of loaded drivers
       if grep -i "\[Modem   " -i /proc/asound/cards ; then
           # already loaded, just pick up the device there
           alsaload;
       else
           # ALSA not loaded yet, try the smartlink driver first, then try ALSA
           # though (forced loading)
           if grep -q 'slamr\..*o' /lib/modules/`uname -r`/modules.dep ;
           then
               if $modprobe slamr ; then
                   SLMODEMD_DEVICE=slamr0
               else
                   alsaload;
               fi
           else
               alsaload;
           fi
       fi
   fi

   if [ "$ALSA" ] ; then
       # either detected or choosed by user, the driver must already be loaded
       # and the device name be picked up before

       OPTS=" --alsa $OPTS $SLMODEMD_DEVICE"

   else

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

      # installed as module so just load it or bail out
      if ! $modprobe slamr ; then
          echo "SmartLink modem driver not available for this Kernel. Please read README.Debian"
          echo "or try to install the package sl-modem-modules-`uname -r`. Exiting..."
          exit 1
      fi
   fi
}

start() {

    echo -n "Starting SmartLink Modem driver for: $SLMODEMD_DEVICE"
    start-stop-daemon --start --pidfile $PIDFILE --exec $DAEMON --make-pidfile --background --quiet -- $OPTS
    RETVAL=$?
    if [ "$RETVAL" = 0 ] && [ "$NOSYMLINK" != 1 ] ; then
        echo "."
        echo "Creating /dev/modem symlink, pointing to: /dev/ttySL0."
        ln -sf ttySL0 /dev/modem
    fi
}

stop() {
    echo -n "Shutting down SmartLink Modem driver normally"
    RETVAL=0
    if [ "`pidof $NAME`" ] ; then 
        if start-stop-daemon --stop --quiet --pidfile $PIDFILE --exec $DAEMON --retry 1 ; then
            echo .
        else
            echo " probably failed."
            echo "Trying it the hard way (send SIGKILL all $NAME processes): " 
            killall -KILL $NAME || RETVAL=1
        fi
    else
        echo " ... no $NAME daemon running."
    fi
    test "$RETVAL" -ne 0 || rm -f "$PIDFILE"
    echo -n "Unloading modem driver from kernel ... " 
    msg="none found."
    for x in $unloadllist ; do 
        if grep -l -q "^$x " /proc/modules ; then
            $modprobe -r $x 2>/dev/null && msg="$x." || msg="failed."
        fi
    done
    echo $msg
}

status() {
    echo -n "Status of $DESC: "
    if [ ! -r "$PIDFILE" ]; then
        echo "$NAME is not running."
        exit 3
    fi
    if read pid < "$PIDFILE" && ps -p "$pid" > /dev/null 2>&1; then
        echo "$NAME is running."
        exit 0
    else
        echo "$NAME is not running but $PIDFILE exists."
        exit 1
    fi
}

# See how we were called. Consider udev action too.
case "$1$ACTION" in
    
    start)
    config
    start
    ;;

    add)
    ALSA=""
    # slusb via udev part
    if [ "$DEVICE" ] ; then 
       SLMODEMD_DEVICE="$DEVICE"
    else
       SLMODEMD_DEVICE="$DEVNAME"
    fi

    if [ -r "$SLMODEMD_DEVICE" ]
    then
       # check if $GROUP really exists
       if getent group $GROUP > /dev/null; then
          chmod 660 "$SLMODEMD_DEVICE"
          chown root:$GROUP "$SLMODEMD_DEVICE"
       fi
    else
       exit 1
    fi
    start
    ;;

    stop|remove)
    stop
    ;;
    
    restart|reload|force-reload)
    stop
    config
    start
    ;;
    
    status)
    status
    ;;

    *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|status}"
    exit 1
esac

