#!/bin/sh
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

WITHOUT_RC_COMPAT=1

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

cd /etc/sysconfig/network-scripts
. network-functions

CONFIG=$1
source_config

if [ "$2" = boot ] && ! is_yes "$ONBOOT"; then
	exit
fi

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

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

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

while :; do
  echo > /var/run/sl-$DEVICE.dev
  (logger -p daemon.info -t ifup-sl \
    "dip started for $DEVICE on $MODEMPORT at $LINESPEED" &)&
  runas /usr/sbin/dip dip-$DEVICE $DIPSCRIPT
  if ! is_yes "$PERSIST" || [ ! -f /var/run/sl-$DEVICE.dev ]; then
    exit 0
  fi
  while [ -f /var/run/LCK..$DEVICE && killall -0 dip ] ; do
        sleep 10
  done
  rm -f /var/run/sl-$DEVICE.dev

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