#!/bin/sh
#
#	$Id: ifup-sl,v 1.15 1999/10/13 21:28:40 kloczek Exp $
#
PATH=/sbin:/usr/sbin:/bin:/usr/bin

# ifup-post can't be done for slip :-( Solution: use PPP

# become a daemon in case we have to persist.
if [ "$1" != daemon ] ; then
  # disconnect stdin, out, err to disassociate from controlling tty
  # so that no HUPs will get through.
  $0 daemon $*& </dev/null >/dev/null 2>/dev/null
  exit 0
fi
shift

. /etc/rc.d/init.d/functions
. /etc/sysconfig/network-scripts/.functions

CONFIG=$1
source_config

if [ "foo$2" = "fooboot" -a ${ONBOOT} = "no" ]
then
  exit
fi

if [ -z "$RETRYTIMEOUT" ]; then
  RETRYTIMEOUT=30
fi

# Try to load slip module
if [ "`ls -R /lib/modules/\`uname -r\`/ | grep slip.o`" != "" ]; then
	/sbin/modprobe -s -k slip
fi

# we can use dip or direct slip connection via slattach
if [ "$DIRECT_CONNECT" != "yes" ]; then

[ -x /usr/sbin/dip ] || {
  nls "/usr/sbin/dip does not exist or is not executable"
  nls "ifup-sl for $DEVICE exiting"
  logger -p daemon.info -t ifup-sl \
    "`nls '/usr/sbin/dip does not exist or is not executable for'` $DEVICE"
  exit 1
}

DIPSCRIPT=/etc/sysconfig/interfaces/data/dip-$DEVNAME
[ -f $DIPSCRIPT ] || {
  DIPSCRIPT=/etc/sysconfig/interfaces/data/dip-$PARENTDEVNAME
}
[ -f $DIPSCRIPT ] || {
  echo "/etc/sysconfig/interfaces/data/dip-$DEVICE `nls 'does not exist'`"
  echo "ifup-sl for $DEVICE exiting"
  logger -p daemon.info -t ifup-sl \
    "/etc/sysconfig/interfaces/data/dip-$DEVICE `nls 'does not exist for'` $DEVICE"
  exit 1
}

while : ; do
  echo > /var/run/sl-$DEVICE.dev
  (logger -p daemon.info -t ifup-sl \
    "`nls 'dip started for'` $DEVICE `nls 'on'` $MODEMPORT `nls 'at'` $LINESPEED" &)&
  doexec /usr/sbin/dip dip-$DEVICE $DIPSCRIPT
  if [ "$PERSIST" != "yes" -o ! -f /var/run/sl-$DEVICE.dev ] ; then
    exit 0
  fi
  rm -f /var/run/sl-$DEVICE.dev


  sleep $RETRYTIMEOUT || {
    # sleep was killed
    exit 0
  }
done

else

# direct connection via slattach
[ -z "$MTU" ] && MTU=1500
[ -z "$PROTOCOL" ] && PROTOCOL=slip

[ -x /sbin/slattach ] || {
	nls "/sbin/slattach does not exist or is not executable"
        echo "ifup-sl - $DEVICE `nls exiting`"
        logger -p daemon.info -t ifup-sl \
        "`nls '/sbin/slattach does not exist or is not executable for'` $DEVICE"
        exit 1
}

        echo > /var/run/sl-$DEVICE.dev
        (logger -p daemon.info -t ifup-sl \
        "`nls 'slattach started for'` $DEVICE `nls on` $MODEMPORT `nls at` $LINESPEED" &)&

         slattach -p $PROTOCOL -s $LINESPEED $MODEMPORT &
         usleep 1500
	 ip link set $DEVICE mtu $MTU up
	 ip -family inet addr add $IPADDR peer $REMIP dev $DEVICE

	 # Set defaultroute
	 if [ "$DEFROUTE" = "yes" ]; then
		ip -family inet route add default dev $DEVICE
         fi

fi

