#!/bin/sh

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

. $1

# Source ISDN configuration, if available
if [ -f /etc/sysconfig/isdn ] ; then
    . /etc/sysconfig/isdn
fi

# Check that ISDN is up
lsmod |grep isdn >&/dev/null || exit

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

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

echo "Starting isdn4linux on 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

# define local MSN (phone number):
isdnctrl eaz $DEVICE ${I4L_LOCALMSN}

# add numbers(s) to call out:
test -z "${I4L_REMOTE_OUT1}" ||
    isdnctrl addphone $DEVICE out ${I4L_REMOTE_OUT1}
test -z "${I4L_REMOTE_OUT2}" ||
    isdnctrl addphone $DEVICE out ${I4L_REMOTE_OUT2}

# add numbers(s) allowed to call in:
test -z "${I4L_REMOTE_IN1}" ||
    isdnctrl addphone $DEVICE in  ${I4L_REMOTE_IN1}
test -z "${I4L_REMOTE_IN2}" ||
    isdnctrl addphone $DEVICE in  ${I4L_REMOTE_IN2}

# the  layer-2 protocol:
isdnctrl l2_prot $DEVICE $I4L_L2

# the  layer-3 protocol:
isdnctrl l3_prot $DEVICE $I4L_L3

# encapsulation
isdnctrl encap $DEVICE $I4L_ENCAP

# secure?, on = only defined numbers are allowed to call in
isdnctrl secure $DEVICE $I4L_SECURE

# automatic hangup if idle:
isdnctrl huptimeout $DEVICE ${I4L_IDLETIME}

# hangup before  next  charge  info?
isdnctrl chargehup $DEVICE $I4L_CHARGEHUP

# set the number of dial atempts for each number
test -z "$I4L_DIALMAX" ||
    isdnctrl dialmax $DEVICE $I4L_DIALMAX

# callback features
isdnctrl callback $DEVICE $I4L_CALLBACK
isdnctrl cbdelay $DEVICE $I4L_CBDELAY
isdnctrl cbhup $DEVICE $I4L_CBHUP

# start iprofd
test "$I4L_IPROFD" = "yes" &&
    iprofd /etc/isdnlog/modemsettings
                
# enable debugging
test -z "$I4L_DEBUG" || 
    telesctrl ${I4L_ID} 1 ${I4L_DEBUG}

ifconfig ${DEVICE} ${IPADDR} netmask ${NETMASK} broadcast ${BROADCAST}

# start isdnlog
isdnlog -D -v7 -l1015 -x4087 -M -n -W80 /dev/isdnctrl0 

exit 0