#!/bin/sh
#
# ifup-ippp
# 
# This script is normally called from the ifup script when it detects a ippp device. 
#
# Script from isdn4net, reworked by dam's (damien@mandrakesoft.com)

PATH="/sbin:/usr/sbin:/bin:/usr/bin"

echo "Activating device: $1"

[ -x /usr/sbin/ipppd ] || [ -x /sbin/ipppd ] || {
  echo "/usr/sbin/ipppd does not exist or is not executable"
  echo "Exiting"
  logger -p daemon.info -t ifup-ippp \
    "ipppd does not exist or is not executable"
  exit 1
}

CONFIG=$1

# Device net config
echo "Loading network settings: $CONFIG"
. $CONFIG

echo "Loading module(s) for $DEVICE"
modprobe -k $DEVICE

if [ -n "$FIRMWARE" ]; then
    echo "Loading firmware for $DEVICE"
    hisaxctrl HiSax 9 $FIRMWARE
fi

echo "Adding ippp device $DEVICE"
isdnctrl addif $DEVICE
cat /proc/net/dev | grep $DEVICE >& /dev/null
if test $? -ne 0; then
    echo "$0: $DEVICE doesn't exist in /proc/net/dev"
    echo "Check ISDN hardware configuration"
    echo ""
    exit 1
fi

echo "Configuring device $DEVICE" 
isdnctrl readconf /etc/isdn/isdnctrl.conf

echo "Adding device $DEVICE" 
IFARG=" $DEVICE" 
if [ -n "$IPADDR" ]; then 
	IFARG="$IFARG $IPADDR"
else 	IFARG="$IFARG 0.0.0.0"	; fi
if [ -n "$REMOTEADDR" ]; then
	IFARG="$IFARG pointopoint $REMOTEADDR"
else 	IFARG="$IFARG pointopoint 0.0.0.0" ; fi
if [ -n "$NETMASK" ]; then
	IFARG="$IFARG netmask $NETMASK" ; fi
if [ -n "$BROADCAST" ]; then
	IFARG="$IFARG -arp broadcast $BROADCAST"
else 	IFARG="$IFARG -arp -broadcast" ; fi
ifconfig $IFARG

echo "Starting ppp-daemon if needed"
pppdpid=`ps axww|grep -v grep|grep $DEVICE|awk '{print $1}'`
if [ -z "$pppdid" ] ; then
	echo "Starting PPP-daemon for $DEVICE"
	ipppd /dev/${DEVICE}
fi

# Run the distribution's ifup-post script

/etc/sysconfig/network-scripts/ifup-post $CONFIG

exit 0
