#!/bin/sh
# Begin /etc/rc.d/init.d/slip

# Could be called by binary, make sure context is right
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/lib

. /etc/profile
. /etc/rc.d/init.d/network_functions
. /etc/sysconfig/network

SLIP_DEV=/dev/wirgrid-modem
SLIP_BAUDRATE=115200
SLIP_ITF=sl0

if [ -f /tmp/slip.ok ]; then
  rm -f /tmp/slip.ok
fi
  
if [ -f /tmp/slip.ko ]; then
  rm -f /tmp/slip.ko
fi

start_slip()
{
  if [ -e $SLIP_DEV ]; then
    if [ -e /sys/class/net/sl0 ]; then
      touch /tmp/slip.ko
    else
      /sbin/slattach -p slip -s $SLIP_BAUDRATE $SLIP_DEV -F &
      /sbin/ifconfig $SLIP_ITF 172.16.0.1 pointopoint 172.16.0.2 up

      touch /tmp/slip.ok
    fi
  else
    touch /tmp/slip.ko
  fi
}

stop_slip()
{
  if [ -e /sys/class/net/sl0 ]; then
    /sbin/ifconfig $SLIP_ITF down
    kill -9 `ps | grep /dev/wirgrid-modem | awk '{print $1}' | head -n1`
    touch /tmp/slip.ok
  else
    touch /tmp/slip.ko
  fi
}

case "$1" in
  start)
    start_slip
    ;;

  stop)
    stop_slip
    ;;

  restart)
    stop_slip
    start_slip
    ;;

  reload)
    start_slip
    ;;

  *)
    echo "Usage: $0 {start|stop|restart|reload}"
    exit 1
    ;;
esac


# End /etc/rc.d/1init.d/slip
