#!/bin/sh
#
#	$Id: ifup-ppp,v 1.10 2000/04/03 14:50:47 baggins Exp $
#
# ifup-ppp script for pppd-2.3.5 (with persist & demand options) ver 0.2
# Grzegorz Stanislawski <stangrze@open.net.pl>
# Features:
#  - since persist option is included to pppd it''s no need to do werid loops
#    ifup-ppp script. This also makes your logfile grow slower when Your link
#    is down.
#  - chat-ppp? file with script for chat is now parsed by shell, so You can
#    include in it shell variables (for example $PHONE, $USERNAME)
#    You can define any variable in ifcfg-ppp? file and use it in chat-ppp?
#    You only have to add it's name into DATAFORCHAT variable
#    Note, that chat-ppp file is now parsed by shell, so you have to escape
#    with "\" all shell special characters like \$;:)(& etc.
 
PATH=/sbin:/usr/sbin:/bin:/usr/bin

# ifup-post for PPP is handled through /etc/ppp/ip-up
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

CONFIG=$1
. /etc/sysconfig/network
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network-scripts/.functions

source_config
[ -z "$HOLDOFF" ] && HOLDOFF=30

if [ "$2" = "boot" -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 [ "${PERSIST}" = "yes" ]; then
  [ -z "${MAXFAIL}" ] && MAXFAIL=0
  opts="$opts persist holdoff $HOLDOFF maxfail $MAXFAIL"
fi
if [ "${DEMAND}" = "yes" ]; then
#opts="$opts demand active-filter $ACTIVEFILTER idle $IDLE"
opts="$opts demand "
fi
if [ "${HARDFLOWCTL}" = yes ] ; then
  opts="$opts modem crtscts"
fi
if [ "${ESCAPECHARS}" = yes ] ; then
  opts="$opts asyncmap FFFFFFFF"
elif [ "${ESCAPECHARS}" = no ] ; 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 "${MTU}" ] ; then
  opts="$opts mtu ${MTU}"
fi
if [ -n "${IPADDR}${REMIP}" ] ; then
  # if either IP address is set, the following will work.
  opts="$opts ${IPADDR}:${REMIP}"
fi
if [ -n "${PAPNAME}" ] ; then
  opts="$opts name ${PAPNAME}"
fi
if [ "${DEBUG}" = yes ] ; then
  opts="$opts debug"
  chatdbg="-v"
fi
case "${AUTH}" in
  yes) opts="$opts auth"
  ;;
  no) opts="$opts noauth"
  ;;
  *)
  ;;
esac
if [ -n "${REPORTFILE}"  ] ; then
  chatrpt="-r ${REPORTFILE}"
fi
#CHATSCRIPT=/etc/sysconfig/network-scripts/chat-$DEVNAMR
#[ -f $CHATSCRIPT ] || {
#  CHATSCRIPT=/etc/sysconfig/network-scripts/chat-$PARENTDEVNAME
#}
> /var/run/ppp-$DEVICE.dev
(logger -p daemon.info -t ifup-ppp \
    "pppd started for $DEVICE on $MODEMPORT at $LINESPEED" &)&
if [ -n "${CHATSCRIPT}"  ] ; then
  export chatdbg chatrpt CHATSCRIPT
  export $DATAFORCHAT
  /usr/sbin/pppd -detach $opts $MODEMPORT $LINESPEED \
    connect 'eval /usr/sbin/chat $chatdbg $chatrpt \
    `grep -v ^# ${CHATSCRIPT}`' \
    remotename $DEVICE ipparam $DEVICE \
    ${PPPOPTIONS} &
else
  /usr/sbin/pppd -detach $opts $MODEMPORT $LINESPEED \
    remotename $DEVICE ipparam $DEVICE \
    ${PPPOPTIONS} &
fi
 echo "$!" > /var/run/ppp-$DEVICE.pid

