#!/bin/sh
PATH=/sbin:/usr/sbin:/bin:/usr/bin

if [ "$1" != daemon ] ; then
  $0 daemon $*&
  exit 0
fi

shift

. $1

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

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

[ -f /etc/sysconfig/network-scripts/chat-$DEVICE ] || {
  echo "/etc/sysconfig/network-scripts/chat-$DEVICE does not exist"
  echo "ifup-ppp for $DEVICE exiting"
  logger -p daemon.info -t ifup-ppp \
    "/etc/sysconfig/network-scripts/chat-$DEVICE does not exist for $DEVICE"
  exit 1
}

opts="lock"
if [ "${HARDFLOWCTL}" = yes ] ; then
  opts="$opts modem crtscts"
fi
if [ "${ESCAPECHARS}" = yes ] ; then
  opts="$opts asyncmap 00000000"
fi
if [ "${DEFROUTE}" = yes ] ; then
  opts="$opts defaultroute"
fi
if [ -n "${MRU}" ] ; then
  opts="$opts mru ${MRU}"
fi
if [ -n "${IPADDR}${REMIP}" ] ; then
  # if either IP address is set, the following will work.
  opts="$opts ${IPADDR}:${REMIP}"
fi

echo $$ > /var/run/ifup-ppp-$DEVICE.pid
while : ; do
  (logger -p daemon.info -t ifup-ppp \
    "pppd started for $DEVICE on $MODEMPORT at $LINESPEED" &)&
  /usr/sbin/pppd -detach $opts $MODEMPORT $LINESPEED \
    connect "/usr/sbin/chat -f /etc/sysconfig/network-scripts/chat-$DEVICE"
  if [ "$PERSIST" != "yes" ] ; then
    exit 0
  fi
  # FIXME: tunable parameter
  sleep 30 || {
    # sleep was killed
    exit 0
  }
done
