#!/bin/sh
#
#	$Id: ifup-ppp,v 1.17 2001/10/09 22:46:24 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

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

source_config

# set all major variables
setup_ip_param

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

if [ "$2" = "boot" ] && is_no "${ONBOOT}"; then
  exit
fi
[ -x /usr/sbin/pppd ] || {
  nls "%s does not exist or is not executable" "/usr/sbin/pppd"
  nls "%s for %s exiting" "ifup-ppp" "$DEVICE"
    logger -p daemon.info -t ifup-ppp \
    "$(nls '%s does not exist or is not executable for %s' "/usr/sbin/pppd" "$DEVICE")"
  exit 1
}

# modprobe ppp in case of kernel with devfs
# (/dev/ppp is missing and ppp fails without this) --misiek
if [ -c /dev/.devfsd ]; then
	modprobe -s -k char-major-108
fi

opts="lock"
if is_yes "${PERSIST}"; then
  if [ -z "${MAXFAIL}" ]; then 
  MAXFAIL="0"
  fi
  opts="$opts persist holdoff $HOLDOFF maxfail $MAXFAIL"
fi
if is_yes "${DEMAND}"; then
  if [ -z "${IDLE}" ]; then
  IDLE="0"
  fi
  opts="$opts demand idle ${IDLE}"
fi
if is_yes "${HARDFLOWCTL}"; then
  opts="$opts modem crtscts"
fi
if is_yes "${ESCAPECHARS}"; then
  opts="$opts asyncmap FFFFFFFF"
elif [ "${ESCAPECHARS}" = no ] ; then
  opts="$opts asyncmap 00000000"
fi
if is_yes "${DEFROUTE}" = yes; then
  # pppd will no longer delete an existing default route
  # so we have to help it out a little here.
  ip route del 0/0 > /dev/null 2>&1
  opts="$opts defaultroute"
fi
if is_yes "${PEERDNS}"; then
  opts="$opts usepeerdns"
fi
if [ -n "${MRU}" ] ; then
  opts="$opts mru ${MRU}"
fi
if [ -n "${MTU}" ] ; then
  opts="$opts mtu ${MTU}"
fi
if is_yes "$IPV4_NETWORKING" && [ -n "${IP4ADDR}${REMIP}" ] ; then
  # if either IP address is set, the following will work.
  opts="$opts ${IP4ADDR}:${REMIP}"
fi
if is_yes "$IPV6_NETWORKING" && is_yes "$IPV6_PPP"; then
	if [ -n "${IP6ADDR}${REMIP6}" ] ; then
	  opts="$opts ipv6 ${IP6ADDR},${REMIP6}"
	elif is_yes "$IPV6_CP_USEV4"; then
	  opts="$opts ipv6cp-use-ipaddr"
	elif is_yes "$IPV6_CP_PERSISTENT"; then
	  opts="$opts ipv6cp-use-persistent"
	fi
else
	opts="$opts noipv6"
fi
if [ -n "${PAPNAME}" ] ; then
  opts="$opts name ${PAPNAME}"
fi
if [ -n "${REMOTENAME}" ] ; then
  opts="$opts remotename ${REMOTENAME}"
fi
if is_yes "${DEBUG}"; 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

(logger -p daemon.info -t ifup-ppp \
    "pppd started for $DEVICE on $MODEMPORT at $LINESPEED" &)&
if [ -n "${CHATSCRIPT}"  ] ; then
  export chatdbg chatrpt CHATSCRIPT
  if [ -n "${DATAFORCHAT}" ]; then
  	export $DATAFORCHAT
  fi
  /usr/sbin/pppd $opts $MODEMPORT $LINESPEED \
    connect 'eval /usr/sbin/chat -E $chatdbg $chatrpt \
    $(grep -v ^# ${CHATSCRIPT})' \
    linkname "${DEVICE}" ipparam "${CONFIG}" \
    ${PPPOPTIONS}
else
  /usr/sbin/pppd $opts $MODEMPORT $LINESPEED \
    linkname "${DEVICE}" ipparam "${CONFIG}" \
    ${PPPOPTIONS}
fi


# This must be last line !
# vi:syntax=sh:tw=78:ts=8:sw=4
